Skip to content

Commit

Permalink
Migrates examples/data/ to use Preact.
Browse files Browse the repository at this point in the history
Refs #71.
  • Loading branch information
dgp1130 committed Mar 13, 2023
1 parent 74bd499 commit 77cf17e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 62 deletions.
20 changes: 16 additions & 4 deletions examples/data/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
load("//tools/typescript:defs.bzl", "ts_project")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("//:index.bzl", "prerender_pages", "web_resources_devserver")
load("//tools/jasmine:defs.bzl", "jasmine_web_test_suite")
load("//tools/typescript:defs.bzl", "ts_project")

copy_to_bin(
name = "package",
srcs = ["package.json"],
)

prerender_pages(
name = "site",
src = "site.mts",
src = "site.tsx",
tsconfig = "//:tsconfig",
source_map = True,
data = glob(["content/*.txt"]),
# Data dependency on any files needed for prerendering.
data = glob(["content/*.txt"]) + [
# Need `"type": "module"` to load `*.js` files output by `*.tsx`
# compilation.
":package",
],
bundle_js = False, # Optimization: No client-side JavaScript on this site.
lib_deps = [
"//:node_modules/rules_prerender",
"//:node_modules/@rules_prerender/preact",
"//:node_modules/@types/node",
"//:node_modules/preact",
],
)

Expand Down
3 changes: 3 additions & 0 deletions examples/data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
58 changes: 0 additions & 58 deletions examples/data/site.mts

This file was deleted.

60 changes: 60 additions & 0 deletions examples/data/site.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { promises as fs } from 'fs';
import * as path from 'path';
import { PrerenderResource, renderToHtml } from '@rules_prerender/preact';

const content = `${process.env['RUNFILES']}/rules_prerender/examples/data/content`;

export default async function*():
AsyncGenerator<PrerenderResource, void, void> {
// Read all files under `content/` in runfiles.
const entries = await fs.readdir(content, { withFileTypes: true });
const files = entries.filter((entry) => entry.isFile());

// Generate an index page which links to all posts.
yield PrerenderResource.of('/index.html', renderToHtml(
<html>
<head>
<title>Data</title>
<meta charSet='utf8' />
</head>
<body>
<h2>Data</h2>

<ul>
{files.map((file) =>
<li>
<a href={`/posts/${getBaseName(file.name)}.html`}>
{getBaseName(file.name)}
</a>
</li>
)}
</ul>
</body>
</html>
));

// Generate an HTML page with the content of each file.
for (const file of files) {
const baseName = getBaseName(file.name);
const text = await fs.readFile(path.join(content, file.name), {
encoding: 'utf8',
});

yield PrerenderResource.of(`/posts/${baseName}.html`, renderToHtml(
<html>
<head>
<title>{baseName}</title>
<meta charSet="utf8" />
</head>
<body>
<h2>{baseName}</h2>
<article>{text.trim()}</article>
</body>
</html>
));
}
}

function getBaseName(fileName: string): string {
return fileName.split('.').slice(0, -1).join('.');
}

0 comments on commit 77cf17e

Please sign in to comment.