From 73f61b6d13fd7d71998d795e8a102572e914aa05 Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Mon, 13 Mar 2023 15:25:35 -0700 Subject: [PATCH] Renames `PrerenderResource.of()` to `PrerenderResource.fromHtml()`. Refs #71. This more accurately reflects its actual usage and semantics now that `fromText()` and `fromBinary()` exist. --- common/models/prerender_resource.mts | 11 ++++++----- common/models/prerender_resource_test.mts | 8 ++++---- examples/components/site.tsx | 2 +- examples/data/site.tsx | 4 ++-- examples/declarative_shadow_dom/site.tsx | 2 +- examples/external/site.mts | 7 +++++-- examples/javascript/site.mjs | 2 +- examples/minimal/site.tsx | 2 +- examples/multi_page/site.tsx | 8 ++++---- examples/preact/site.tsx | 2 +- examples/resources/site.tsx | 2 +- examples/script_isolation/site.tsx | 6 +++--- examples/scripts/site.tsx | 2 +- examples/shared_chunks/site.tsx | 6 +++--- examples/site/about/about.tsx | 2 +- examples/site/blog/blog.tsx | 4 ++-- .../site/components/counter/counter_test_cases.tsx | 6 +++--- examples/site/counter/counter.tsx | 2 +- examples/site/home/home.tsx | 5 +++-- examples/styles/site.tsx | 2 +- examples/testonly/site.tsx | 2 +- examples/tsconfig/site.tsx | 2 +- examples/tsjs/site.tsx | 10 +++++----- packages/preact/README.md | 2 +- tools/binaries/renderer/renderer_test.mts | 4 ++-- 25 files changed, 55 insertions(+), 50 deletions(-) diff --git a/common/models/prerender_resource.mts b/common/models/prerender_resource.mts index 5d255d3c..7d4aa7c5 100644 --- a/common/models/prerender_resource.mts +++ b/common/models/prerender_resource.mts @@ -30,7 +30,8 @@ export class PrerenderResource { * @param contents A {@link SafeHtml} object to encode as a UTF-8 string. * @returns A {@link PrerenderResource} object representing the resource. */ - public static of(path: string, contents: SafeHtml): PrerenderResource { + public static fromHtml(path: string, contents: SafeHtml): + PrerenderResource { if (!isSafeHtml(contents)) { throw new Error(`Only \`SafeHtml\` objects can be used in \`*.html\` or \`*.htm\` files. Use a rendering engine like \`@rules_prerender/preact\` to render to \`SafeHtml\`.`); } @@ -49,7 +50,7 @@ export class PrerenderResource { * * @param path The path the file will be generated at relative to the final * generated site. Must begin with a `/` character. Must *not* end in - * `.html` or `.htm`. Use {@link PrerenderResource.of} with + * `.html` or `.htm`. Use {@link PrerenderResource.fromHtml} with * {@link SafeHtml} to generate HTML content. * @param contents A UTF-8 encoded string to output at the given path. * @returns A {@link PrerenderResource} object representing the resource. @@ -60,7 +61,7 @@ export class PrerenderResource { // constructed and there could be injection attacks within it. if (path.endsWith('.html') || path.endsWith('.htm')) { throw new Error(`Cannot generate a \`*.html\` or \`*.htm\` file (${ - path}) from a raw string (this would be unsafe!). HTML content should be rendered to \`SafeHtml\` first, and then written to a file in \`PrerenderResource.of()\`.`); + path}) from a raw string (this would be unsafe!). HTML content should be rendered to \`SafeHtml\` first, and then written to a file in \`PrerenderResource.fromHtml()\`.`); } return new PrerenderResource({ @@ -75,7 +76,7 @@ export class PrerenderResource { * * @param path The path the file will be generated at relative to the final * generated site. Must begin with a `/` character. Must *not* end in - * `.html` or `.htm`. Use {@link PrerenderResource.of} with + * `.html` or `.htm`. Use {@link PrerenderResource.fromHtml} with * {@link SafeHtml} to generate HTML content. * @param contents Binary content to associate with the given path. * @returns A {@link PrerenderResource} object representing the resource. @@ -89,7 +90,7 @@ export class PrerenderResource { // constructed and there could be injection attacks within it. if (path.endsWith('.html') || path.endsWith('.htm')) { throw new Error(`Cannot generate a \`*.html\` or \`*.htm\` file (${ - path}) from a raw string (this would be unsafe!). HTML content should be rendered to \`SafeHtml\` first, and then written to a file in \`PrerenderResource.of()\`.`); + path}) from a raw string (this would be unsafe!). HTML content should be rendered to \`SafeHtml\` first, and then written to a file in \`PrerenderResource.fromHtml()\`.`); } return new PrerenderResource({ diff --git a/common/models/prerender_resource_test.mts b/common/models/prerender_resource_test.mts index 927e1eb7..490309b3 100644 --- a/common/models/prerender_resource_test.mts +++ b/common/models/prerender_resource_test.mts @@ -4,7 +4,7 @@ import { PrerenderResource } from './prerender_resource.mjs'; describe('PrerenderResource', () => { describe('of()', () => { it('returns a `PrerenderResource` from `SafeHtml` data', () => { - const res = PrerenderResource.of( + const res = PrerenderResource.fromHtml( '/foo/bar.html', safe`
`); expect(res.path).toBe('/foo/bar.html'); @@ -12,19 +12,19 @@ describe('PrerenderResource', () => { }); it('throws when given non-`SafeHtml` input', () => { - expect(() => PrerenderResource.of( + expect(() => PrerenderResource.fromHtml( '/foo/bar.html', 'unsafe HTML content' as unknown as SafeHtml, )).toThrowError(/Only `SafeHtml` objects can be used in `\*.html` or `\*.htm` files\./); - expect(() => PrerenderResource.of( + expect(() => PrerenderResource.fromHtml( '/foo/bar.html', { getHtmlAsString: () => 'unsafe HTML content' } as SafeHtml, )).toThrowError(/Only `SafeHtml` objects can be used in `\*.html` or `\*.htm` files\./); }); it('throws when given an invalid URL path', () => { - expect(() => PrerenderResource.of( + expect(() => PrerenderResource.fromHtml( 'does/not/start/with/a/slash.ext', safe`Hello, World!`, )).toThrowError(/must start with a "\/"/); diff --git a/examples/components/site.tsx b/examples/components/site.tsx index 1c80a1ea..6a676ce9 100644 --- a/examples/components/site.tsx +++ b/examples/components/site.tsx @@ -3,7 +3,7 @@ import { Component } from './component.js'; /** Renders a page using components. */ export default function*(): Generator { - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml( Components diff --git a/examples/data/site.tsx b/examples/data/site.tsx index acbece2c..37e88e67 100644 --- a/examples/data/site.tsx +++ b/examples/data/site.tsx @@ -11,7 +11,7 @@ export default async function*(): const files = entries.filter((entry) => entry.isFile()); // Generate an index page which links to all posts. - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml( Data @@ -40,7 +40,7 @@ export default async function*(): encoding: 'utf8', }); - yield PrerenderResource.of(`/posts/${baseName}.html`, renderToHtml( + yield PrerenderResource.fromHtml(`/posts/${baseName}.html`, renderToHtml( {baseName} diff --git a/examples/declarative_shadow_dom/site.tsx b/examples/declarative_shadow_dom/site.tsx index 1c0eb0aa..e360cb43 100644 --- a/examples/declarative_shadow_dom/site.tsx +++ b/examples/declarative_shadow_dom/site.tsx @@ -2,7 +2,7 @@ import { PrerenderResource, renderToHtml } from '@rules_prerender/preact'; import { Component } from './component.js'; export default async function*(): AsyncGenerator { - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml( Declarative Shadow DOM diff --git a/examples/external/site.mts b/examples/external/site.mts index e6947168..505cfd6b 100644 --- a/examples/external/site.mts +++ b/examples/external/site.mts @@ -5,7 +5,9 @@ export default function*(): Generator { // TODO: Migrate to Preact once we figure out how to handle // `@rules_prerender/preact`'s peer dep in an external repository with // manual `npm_link_package()` dependencies. - yield PrerenderResource.of('/index.html', unsafeTreatStringAsSafeHtml(` + yield PrerenderResource.fromHtml( + '/index.html', + unsafeTreatStringAsSafeHtml(` @@ -18,5 +20,6 @@ export default function*(): Generator { ${renderComponent()} - `.trim())); + `.trim()), + ); } diff --git a/examples/javascript/site.mjs b/examples/javascript/site.mjs index aa3170df..fe372925 100644 --- a/examples/javascript/site.mjs +++ b/examples/javascript/site.mjs @@ -4,7 +4,7 @@ import { Component } from './component/component.mjs'; /* Renders the page. */ export default function* () { - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml( h('html', {}, [ h('head', {}, [ h('meta', { charSet: 'utf8' }), diff --git a/examples/minimal/site.tsx b/examples/minimal/site.tsx index 0b77a385..9d68c001 100644 --- a/examples/minimal/site.tsx +++ b/examples/minimal/site.tsx @@ -3,7 +3,7 @@ import { PrerenderResource, renderToHtml } from '@rules_prerender/preact'; /** Renders the page. */ export default function*(): Generator { - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml( Minimal diff --git a/examples/multi_page/site.tsx b/examples/multi_page/site.tsx index 8a8545d1..767cb99f 100644 --- a/examples/multi_page/site.tsx +++ b/examples/multi_page/site.tsx @@ -2,7 +2,7 @@ import { PrerenderResource, includeScript, inlineStyle, renderToHtml } from '@ru import { ComponentChildren, VNode } from 'preact'; export default function* (): Generator { - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml(

Multi-Page

@@ -16,17 +16,17 @@ export default function* (): Generator {
, )); - yield PrerenderResource.of('/foo.html', renderToHtml( + yield PrerenderResource.fromHtml('/foo.html', renderToHtml(

Foo

, )); - yield PrerenderResource.of('/bar.html', renderToHtml( + yield PrerenderResource.fromHtml('/bar.html', renderToHtml(

Bar

, )); - yield PrerenderResource.of('/hello/world.html', renderToHtml( + yield PrerenderResource.fromHtml('/hello/world.html', renderToHtml(

Hello, World!

, diff --git a/examples/preact/site.tsx b/examples/preact/site.tsx index 96b4ed63..0f48867d 100644 --- a/examples/preact/site.tsx +++ b/examples/preact/site.tsx @@ -2,7 +2,7 @@ import { PrerenderResource, renderToHtml } from '@rules_prerender/preact'; import { Component } from './component/component.js'; export default function*(): Generator { - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml( Preact diff --git a/examples/resources/site.tsx b/examples/resources/site.tsx index c6837e57..187d671c 100644 --- a/examples/resources/site.tsx +++ b/examples/resources/site.tsx @@ -3,7 +3,7 @@ import { Component } from './component/component.js'; import { Transitive } from './transitive/transitive.js'; export default function*(): Generator { - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml( Resources diff --git a/examples/script_isolation/site.tsx b/examples/script_isolation/site.tsx index 70565902..6a11e6ad 100644 --- a/examples/script_isolation/site.tsx +++ b/examples/script_isolation/site.tsx @@ -1,7 +1,7 @@ import { PrerenderResource, includeScript, renderToHtml } from '@rules_prerender/preact'; export default function*(): Generator { - yield PrerenderResource.of('/index.html', renderToHtml( + yield PrerenderResource.fromHtml('/index.html', renderToHtml( Script Isolation @@ -16,7 +16,7 @@ export default function*(): Generator { )); - yield PrerenderResource.of('/foo.html', renderToHtml( + yield PrerenderResource.fromHtml('/foo.html', renderToHtml( Script Isolation @@ -30,7 +30,7 @@ export default function*(): Generator { )); - yield PrerenderResource.of('/bar.html', renderToHtml( + yield PrerenderResource.fromHtml('/bar.html', renderToHtml( Script Isolation diff --git a/examples/scripts/site.tsx b/examples/scripts/site.tsx index 81ea4769..4b375aeb 100644 --- a/examples/scripts/site.tsx +++ b/examples/scripts/site.tsx @@ -3,7 +3,7 @@ import { Component } from './component/component.js'; /** Render some HTML with a `