Skip to content

Commit

Permalink
Migrates examples/javascript/ 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 b6724c7 commit 60d8d70
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 35 deletions.
15 changes: 13 additions & 2 deletions examples/javascript/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
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.mjs",
lib_deps = ["//:node_modules/rules_prerender"],
# Need `"type": "module"` to load `*.js` files output by `*.tsx` compilation.
data = [":package"],
lib_deps = [
"//:node_modules/@rules_prerender/preact",
"//:node_modules/preact",
],
deps = ["//examples/javascript/component"],
)

Expand Down
8 changes: 5 additions & 3 deletions examples/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
An example which uses JavaScript source files (as opposed to TypeScript) to
render a component and execute client-side scripts.

This example uses CommonJS to load dependencies, but it should be possible to
use ESM with the right Node version and configuration. Doing so is not fully
tested or supported however.
This uses Preact via the `h` function, which doesn't require any transpilation.
`prerender_component()` doesn't provide any JSX support for JavaScript sources
(though it does for TypeScript sources). If using Preact with JavaScript for a
real application, you may want to invest in a more ergonomic, compiler-less API
such as [`htm`](https://github.com/developit/htm/).
3 changes: 2 additions & 1 deletion examples/javascript/component/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ prerender_component(
srcs = ["component.mjs"],
lib_deps = [
":prerender_lib",
"//:node_modules/rules_prerender",
"//:node_modules/@rules_prerender/preact",
"//:node_modules/preact",
],
scripts = [
":component_script",
Expand Down
29 changes: 15 additions & 14 deletions examples/javascript/component/component.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { includeScript } from 'rules_prerender';
import { includeScript } from '@rules_prerender/preact';
import { h } from 'preact';
import { content } from './prerender_lib.mjs';

/** Renders an example component with a script. */
export function renderComponent() {
return `
<div id="component">${content}</div>
<div id="component-replace">
This text to be overwritten by client-side JavaScript.
</div>
${includeScript('./component_script.mjs', import.meta)}
`.trim();
export function Component() {
return h('', {}, [
h('div', { id: 'component' }, [ content ]),
h('div', { id: 'component-replace' }, [
'This text to be overwritten by client-side JavaScript.',
]),
includeScript('./component_script.mjs', import.meta),
]);
}

/**
* Renders an example component with a script. This is never called and should
* not be seen in the output. Used to validate tree-shaking of JS scripts.
*/
export function renderUnused() {
return `
<div>ERROR: Should never be rendered.</div>
${includeScript('./component_script_unused.mjs', import.meta)}
`.trim();
export function Unused() {
return h('', {}, [
h('div', {}, [ 'ERROR: Should never be rendered.' ]),
includeScript('./component_script_unused.mjs', import.meta),
]);
}
3 changes: 3 additions & 0 deletions examples/javascript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
30 changes: 15 additions & 15 deletions examples/javascript/site.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { PrerenderResource } from 'rules_prerender';
import { renderComponent } from './component/component.mjs';
import { PrerenderResource, renderToHtml } from '@rules_prerender/preact';
import { h } from 'preact';
import { Component } from './component/component.mjs';

/* Renders the page. */
export default function* () {
yield PrerenderResource.of('/index.html', `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<title>JavaScript</title>
</head>
<body>
<h2>JavaScript</h2>
${renderComponent()}
</body>
</html>
`.trim());
yield PrerenderResource.of('/index.html', renderToHtml(
h('html', {}, [
h('head', {}, [
h('meta', { charSet: 'utf8' }),
h('title', {}, [ 'JavaScript ']),
]),
h('body', {}, [
h('h2', {}, [ 'JavaScript' ]),
Component(),
]),
]),
));
}

0 comments on commit 60d8d70

Please sign in to comment.