diff --git a/examples/data/BUILD.bazel b/examples/data/BUILD.bazel index ec879cc0..6ea5e72e 100644 --- a/examples/data/BUILD.bazel +++ b/examples/data/BUILD.bazel @@ -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", ], ) diff --git a/examples/data/package.json b/examples/data/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/examples/data/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/examples/data/site.mts b/examples/data/site.mts deleted file mode 100644 index 0d71f47e..00000000 --- a/examples/data/site.mts +++ /dev/null @@ -1,58 +0,0 @@ -import { promises as fs } from 'fs'; -import * as path from 'path'; -import { PrerenderResource } from 'rules_prerender'; - -const content = `${process.env['RUNFILES']}/rules_prerender/examples/data/content`; - -export default async function*(): - AsyncGenerator { - // 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', ` - - - - Data - - - -

Data

- - - - - `.trim()); - - // 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`, ` - - - - ${baseName} - - - -

${baseName}

-
${text.trim()}
- - - `.trim()); - } -} - -function getBaseName(fileName: string): string { - return fileName.split('.').slice(0, -1).join('.'); -} diff --git a/examples/data/site.tsx b/examples/data/site.tsx new file mode 100644 index 00000000..acbece2c --- /dev/null +++ b/examples/data/site.tsx @@ -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 { + // 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( + + + Data + + + +

Data

+ + + + + )); + + // 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( + + + {baseName} + + + +

{baseName}

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