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 `` tag. */
export default function*(): Generator {
- yield PrerenderResource.of('/index.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/index.html', renderToHtml(
Scripts
diff --git a/examples/shared_chunks/site.tsx b/examples/shared_chunks/site.tsx
index a2d46ee1..86893044 100644
--- a/examples/shared_chunks/site.tsx
+++ b/examples/shared_chunks/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(
Shared Chunks
@@ -16,7 +16,7 @@ export default function*(): Generator {
));
- yield PrerenderResource.of('/hello.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/hello.html', renderToHtml(
Shared Chunks
@@ -29,7 +29,7 @@ export default function*(): Generator {
));
- yield PrerenderResource.of('/goodbye.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/goodbye.html', renderToHtml(
Shared Chunks
diff --git a/examples/site/about/about.tsx b/examples/site/about/about.tsx
index 8c4791ce..6a87bbf0 100644
--- a/examples/site/about/about.tsx
+++ b/examples/site/about/about.tsx
@@ -5,7 +5,7 @@ import { srcLink } from '../common/links.mjs';
/** Renders the about page. */
export default function*(): Generator {
- yield PrerenderResource.of(
+ yield PrerenderResource.fromHtml(
'/about/index.html',
renderToHtml(baseLayout('About',
diff --git a/examples/site/blog/blog.tsx b/examples/site/blog/blog.tsx
index 5d7f8dfb..c9f9490e 100644
--- a/examples/site/blog/blog.tsx
+++ b/examples/site/blog/blog.tsx
@@ -47,7 +47,7 @@ export default async function*():
/** Generate and return a resource with the list of posts at the given path. */
function generatePostList(path: string, posts: PostMeta[]): PrerenderResource {
- return PrerenderResource.of(path, renderToHtml(baseLayout('Blog',
+ return PrerenderResource.fromHtml(path, renderToHtml(baseLayout('Blog',
{polyfillDeclarativeShadowDom()}
@@ -76,7 +76,7 @@ async function generatePost({ urlPath, title, fileName }: PostMeta):
});
const link = srcLink(`/examples/site/blog/posts/${fileName}`);
- return PrerenderResource.of(urlPath, renderToHtml(baseLayout(title,
+ return PrerenderResource.fromHtml(urlPath, renderToHtml(baseLayout(title,
{polyfillDeclarativeShadowDom()}
diff --git a/examples/site/components/counter/counter_test_cases.tsx b/examples/site/components/counter/counter_test_cases.tsx
index 3aa7670b..fc6d2570 100644
--- a/examples/site/components/counter/counter_test_cases.tsx
+++ b/examples/site/components/counter/counter_test_cases.tsx
@@ -4,7 +4,7 @@ import { Counter } from './counter_prerender.js';
/** Generates prerendered test cases of the counter. */
export default function* (): Generator {
// Prerendered counter with an initial value of zero.
- yield PrerenderResource.of('/zero.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/zero.html', renderToHtml(
Zero
@@ -16,7 +16,7 @@ export default function* (): Generator {
));
// Prerendered counter with a positive initial value (4).
- yield PrerenderResource.of('/positive.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/positive.html', renderToHtml(
Positive
@@ -28,7 +28,7 @@ export default function* (): Generator {
));
// Prerendered counter with a negative initial value (-7).
- yield PrerenderResource.of('/negative.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/negative.html', renderToHtml(
Negative
diff --git a/examples/site/counter/counter.tsx b/examples/site/counter/counter.tsx
index 05bff087..faee4457 100644
--- a/examples/site/counter/counter.tsx
+++ b/examples/site/counter/counter.tsx
@@ -6,7 +6,7 @@ import { Counter } from '../components/counter/counter_prerender.js';
/** Renders the counter page. */
export default function*(): Generator {
- yield PrerenderResource.of(
+ yield PrerenderResource.fromHtml(
'/counter/index.html',
renderToHtml(baseLayout('Counter',
diff --git a/examples/site/home/home.tsx b/examples/site/home/home.tsx
index f47e8ec7..0e20cad6 100644
--- a/examples/site/home/home.tsx
+++ b/examples/site/home/home.tsx
@@ -5,7 +5,8 @@ import { repo, srcLink } from '../common/links.mjs';
/** Renders the entire home page. */
export default function*(): Generator {
- yield PrerenderResource.of('/index.html', renderToHtml(baseLayout('Home',
+ yield PrerenderResource.fromHtml('/index.html', renderToHtml(baseLayout(
+ 'Home',
{polyfillDeclarativeShadowDom()}
@@ -78,6 +79,6 @@ export default function*(): Generator {
of rules_prerender
!
-
+ ,
)));
}
diff --git a/examples/styles/site.tsx b/examples/styles/site.tsx
index 2bf07038..75bb9360 100644
--- a/examples/styles/site.tsx
+++ b/examples/styles/site.tsx
@@ -2,7 +2,7 @@ import { PrerenderResource, Template, inlineStyle, renderToHtml } from '@rules_p
/** Generates a page with an inline style in a declarative shadow root. */
export default function* (): Generator {
- yield PrerenderResource.of('/index.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/index.html', renderToHtml(
Inline Styles
diff --git a/examples/testonly/site.tsx b/examples/testonly/site.tsx
index f1f47fbb..918b30cf 100644
--- a/examples/testonly/site.tsx
+++ b/examples/testonly/site.tsx
@@ -2,7 +2,7 @@ import { PrerenderResource, Template, inlineStyle, includeScript, renderToHtml }
import { Component } from './component/component.js';
export default function*(): Generator {
- yield PrerenderResource.of('/index.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/index.html', renderToHtml(
Testonly
diff --git a/examples/tsconfig/site.tsx b/examples/tsconfig/site.tsx
index 3d3c3c73..56e7a854 100644
--- a/examples/tsconfig/site.tsx
+++ b/examples/tsconfig/site.tsx
@@ -7,7 +7,7 @@ import { PrerenderResource, renderToHtml } from '@rules_prerender/preact';
* tsconfig file used to compile this file.
*/
export default function*(foo): Generator {
- yield PrerenderResource.of('/index.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/index.html', renderToHtml(
tsconfig
diff --git a/examples/tsjs/site.tsx b/examples/tsjs/site.tsx
index f588617f..d71ff20c 100644
--- a/examples/tsjs/site.tsx
+++ b/examples/tsjs/site.tsx
@@ -4,7 +4,7 @@ import { TsParent } from './ts_parent/ts_parent.js';
export default function*(): Generator {
// Index page to list the various test cases.
- yield PrerenderResource.of('/index.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/index.html', renderToHtml(
TS/JS
@@ -26,7 +26,7 @@ export default function*(): Generator {
));
// Test case for JS depending on TS.
- yield PrerenderResource.of('/js-depends-on-ts.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/js-depends-on-ts.html', renderToHtml(
JS depends on TS
@@ -39,7 +39,7 @@ export default function*(): Generator {
));
// Test case for TS depending on JS.
- yield PrerenderResource.of('/ts-depends-on-js.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/ts-depends-on-js.html', renderToHtml(
TS depends on JS
@@ -52,7 +52,7 @@ export default function*(): Generator {
));
// Test case for client-side TS depending on JS.
- yield PrerenderResource.of(
+ yield PrerenderResource.fromHtml(
'/ts-script-depends-on-js-script.html',
renderToHtml(
@@ -72,7 +72,7 @@ export default function*(): Generator {
);
// Test case for client-side JS depending on TS.
- yield PrerenderResource.of(
+ yield PrerenderResource.fromHtml(
'/js-script-depends-on-ts-script.html',
renderToHtml(
diff --git a/packages/preact/README.md b/packages/preact/README.md
index e8f23fd1..d93831d6 100644
--- a/packages/preact/README.md
+++ b/packages/preact/README.md
@@ -54,7 +54,7 @@ prerender_pages(
import { PrerenderResource, renderToHtml } from '@rules_prerender/preact';
export default function* (): Generator {
- yield PrerenderResource.of('/index.html', renderToHtml(
+ yield PrerenderResource.fromHtml('/index.html', renderToHtml(
My Preact Page
diff --git a/tools/binaries/renderer/renderer_test.mts b/tools/binaries/renderer/renderer_test.mts
index 92f4ffc8..87327d2d 100644
--- a/tools/binaries/renderer/renderer_test.mts
+++ b/tools/binaries/renderer/renderer_test.mts
@@ -150,7 +150,7 @@ describe('renderer', () => {
const { code, stdout, stderr } = await run({
entryModule: async function* () {
- yield PrerenderResource.of(
+ yield PrerenderResource.fromHtml(
'/index.html',
rulesPrerender.unsafeTreatStringAsSafeHtml(`
@@ -237,7 +237,7 @@ describe('renderer', () => {
const { code, stdout, stderr } = await run({
entryModule: async function* () {
- yield PrerenderResource.of(
+ yield PrerenderResource.fromHtml(
'/index.html',
rulesPrerender.unsafeTreatStringAsSafeHtml(`