@opentf/afw-cli 1.38.1 → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -3
- package/package.json +4 -4
- package/src/prerender.js +4 -2
- package/src/shared.js +41 -7
package/README.md
CHANGED
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
],
|
|
28
28
|
"build": {
|
|
29
29
|
"targets": {
|
|
30
|
-
"web": { "entry": "index.html", "
|
|
31
|
-
"site-ssg": { "entry": "ssg.js", "out": "
|
|
30
|
+
"web": { "entry": "index.html", "outDir": "dist" },
|
|
31
|
+
"site-ssg": { "entry": "ssg.js", "out": "dist/ssg.js", "then": "run" }
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
}
|
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
|
|
37
37
|
`esdev build` bundles the site, then runs `ssg.js`, which prerenders every route
|
|
38
38
|
into the staged output. Only when every step succeeds does the new site replace the
|
|
39
|
-
old one.
|
|
39
|
+
old one. esdev 0.29 and later remove the step's own bundle (`dist/ssg.js`) once it
|
|
40
|
+
has run, so it is never deployed.
|
|
40
41
|
|
|
41
42
|
> [!NOTE]
|
|
42
43
|
> **Alpha.** The package keeps its historical name; the old `afw` executable and
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentf/afw-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.40.0",
|
|
4
4
|
"description": "Static generation for AFW apps, run as an esdev build step.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
"esdev": ">=0.16.0"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@opentf/afw-build": "0.
|
|
20
|
-
"@opentf/esdev-plugin-afw": "0.
|
|
19
|
+
"@opentf/afw-build": "0.8.0",
|
|
20
|
+
"@opentf/esdev-plugin-afw": "0.12.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@opentf/afw": "0.
|
|
23
|
+
"@opentf/afw": "0.41.0"
|
|
24
24
|
},
|
|
25
25
|
"keywords": [
|
|
26
26
|
"afw",
|
package/src/prerender.js
CHANGED
|
@@ -19,8 +19,10 @@ import { buildServerBundle } from "./shared.js";
|
|
|
19
19
|
* The rendering itself is `prerender` from `@opentf/afw-build`, shared with the Vite
|
|
20
20
|
* workflow; this builds the esdev server bundle it renders from.
|
|
21
21
|
*/
|
|
22
|
-
export async function runPrerender({ root, pages, afwEntry, afwc, shellHtml, outDir, baseUrl = "", docsPlugins = [], lastUpdated = {}, i18n = null, loaders = null, chunkManifest = null, onCompile, onRender }) {
|
|
23
|
-
|
|
22
|
+
export async function runPrerender({ root, pages, afwEntry, afwc, shellHtml, outDir, baseUrl = "", docsPlugins = [], lastUpdated = {}, i18n = null, loaders = null, chunkManifest = null, onCompile, onRender, appDir }) {
|
|
23
|
+
// Pages whose folder's `_config.js` says `render: "client"` stay out of this bundle; the
|
|
24
|
+
// prerender writes their layouts around an empty region for the browser to fill.
|
|
25
|
+
const { mod, cleanup } = await buildServerBundle({ root, pages, afwEntry, afwc, docsPlugins, i18n, onCompile, appDir });
|
|
24
26
|
try {
|
|
25
27
|
const { count, skipped, failed, siteDescription } = await prerender(esdevHost, {
|
|
26
28
|
render: mod, shellHtml, outDir, publicDir: join(root, "public"), baseUrl, lastUpdated, i18n, loaders,
|
package/src/shared.js
CHANGED
|
@@ -15,7 +15,8 @@ import { exists, findUp, mkdirp, readEntries, readText, resolveFrom, rmrf, write
|
|
|
15
15
|
// The afwc compiler service and the `runtime:build` transform plugin live in
|
|
16
16
|
// `@opentf/esdev-plugin-afw`. Imported here for the server render bundle and
|
|
17
17
|
// re-exported so the SSG entry keeps a single import site.
|
|
18
|
-
import { closeCompilers, afwPlugin, isUnpublishedPage, resolveCompiler } from "@opentf/esdev-plugin-afw";
|
|
18
|
+
import { closeCompilers, afwPlugin, esdevHost, isUnpublishedPage, resolveCompiler, startCompilerServer } from "@opentf/esdev-plugin-afw";
|
|
19
|
+
import { clientRouteFiles, discoverRouteConfigs, readRouteConfigs, resolveRoutePolicies, staticPathConfigs } from "@opentf/afw-build";
|
|
19
20
|
|
|
20
21
|
export { closeCompilers, resolveCompiler };
|
|
21
22
|
|
|
@@ -432,6 +433,7 @@ export async function emitApiBundle({ root, appDir, afwEntry, exclude, i18n, out
|
|
|
432
433
|
platform: "node",
|
|
433
434
|
resolve: { alias: await afwAliases(afwEntry), extensions: EXTENSIONS },
|
|
434
435
|
external: (id) => !id.startsWith(".") && !id.startsWith("/") && !id.startsWith("@opentf/afw"),
|
|
436
|
+
...ENV_COMPILE(root),
|
|
435
437
|
});
|
|
436
438
|
try {
|
|
437
439
|
await bundle.write({ dir: outDir, format: "esm", entryFileNames: "api.js" });
|
|
@@ -442,6 +444,14 @@ export async function emitApiBundle({ root, appDir, afwEntry, exclude, i18n, out
|
|
|
442
444
|
return { routes: discovered.routes, middleware: discovered.middleware };
|
|
443
445
|
}
|
|
444
446
|
|
|
447
|
+
/**
|
|
448
|
+
* `import.meta.env` for a server bundle, as `esdev build` compiles it into the browser one:
|
|
449
|
+
* `MODE`, `DEV`, `PROD` and the `PUBLIC_*` values from the project's `.env`, then the
|
|
450
|
+
* environment (`runtime:build`'s `mode`). `cwd` is the project root: the build's default is
|
|
451
|
+
* the entry's own directory, a temporary one here. Other variables are never compiled in.
|
|
452
|
+
*/
|
|
453
|
+
const ENV_COMPILE = (root) => ({ mode: "production", cwd: root });
|
|
454
|
+
|
|
445
455
|
/**
|
|
446
456
|
* Entry source for the loader bundle (docs/DATA.md): statically import every
|
|
447
457
|
* `loader.{js,ts}` module (keyed by absolute path so the registry derives each
|
|
@@ -485,6 +495,7 @@ export async function emitLoaderBundle({ root, appDir, afwEntry, exclude, i18n,
|
|
|
485
495
|
platform: "node",
|
|
486
496
|
resolve: { alias: await afwAliases(afwEntry), extensions: EXTENSIONS },
|
|
487
497
|
external: (id) => !id.startsWith(".") && !id.startsWith("/") && !id.startsWith("@opentf/afw"),
|
|
498
|
+
...ENV_COMPILE(root),
|
|
488
499
|
});
|
|
489
500
|
try {
|
|
490
501
|
await bundle.write({ dir: outDir, format: "esm", entryFileNames: "loaders.js" });
|
|
@@ -526,24 +537,33 @@ export function cssPlugin() {
|
|
|
526
537
|
// sees real namespaces, enabling `getStaticPaths`) and re-export the render API.
|
|
527
538
|
// Shared by the SSG pre-render and the SSR server — both render through the same
|
|
528
539
|
// SSG-compiled bundle (ARCHITECTURE.md §6: "SSR … shares the SSG path").
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
540
|
+
/**
|
|
541
|
+
* The prerender bundle's entry. `clientFiles` (route files whose folder's `_config.js`
|
|
542
|
+
* says `render: "client"`) are registered as `clientRoute()` and never imported, so their
|
|
543
|
+
* browser-only code stays out of the server bundle; `staticPaths` (`[route, configFile]`)
|
|
544
|
+
* registers each `_config.js`'s `paths`.
|
|
545
|
+
*/
|
|
546
|
+
export function serverEntrySource(pages, i18n = null, { clientFiles = new Set(), staticPaths = [] } = {}) {
|
|
547
|
+
const imports = pages.map((p, i) => (clientFiles.has(p) ? null : `import * as p${i} from ${JSON.stringify(p)};`)).filter(Boolean).join("\n");
|
|
548
|
+
const map = pages.map((p, i) => ` [${JSON.stringify(p)}]: ${clientFiles.has(p) ? "clientRoute()" : `p${i}`},`).join("\n");
|
|
549
|
+
const paths = staticPaths.map(([, file], i) => `import sp${i} from ${JSON.stringify(file)};`).join("\n");
|
|
532
550
|
// i18n: the server-side router needs the locales too — so renderRoute can strip
|
|
533
551
|
// a `/fr/...` prefix to match the route table and set router.locale for `t()`
|
|
534
552
|
// (docs/I18N.md §6). Without this the prefix wouldn't match and pages would
|
|
535
553
|
// render in the default locale (or 404).
|
|
536
554
|
const i18nOn = i18n && Array.isArray(i18n.locales) && i18n.locales.length;
|
|
537
|
-
const named = i18nOn ? "
|
|
555
|
+
const named = ["clientRoute", "registerRoutes", "registerStaticPaths", ...(i18nOn ? ["configureI18n"] : [])].join(", ");
|
|
538
556
|
const i18nCall = i18nOn
|
|
539
557
|
? `configureI18n(${JSON.stringify({ locales: i18n.locales, defaultLocale: i18n.defaultLocale })});\n`
|
|
540
558
|
: "";
|
|
541
559
|
return (
|
|
542
560
|
`${imports}\n` +
|
|
561
|
+
(paths ? `${paths}\n` : "") +
|
|
543
562
|
`import { ${named} } from "@opentf/afw/server";\n` +
|
|
544
563
|
`export { renderRoute, renderHead, collectRoutePaths, resolveMetadata } from "@opentf/afw/server";\n` +
|
|
545
564
|
i18nCall +
|
|
546
|
-
`registerRoutes({\n${map}\n});\n`
|
|
565
|
+
`registerRoutes({\n${map}\n});\n` +
|
|
566
|
+
(staticPaths.length ? `registerStaticPaths({\n${staticPaths.map(([route], i) => ` [${JSON.stringify(route)}]: sp${i}.paths,`).join("\n")}\n});\n` : "")
|
|
547
567
|
);
|
|
548
568
|
}
|
|
549
569
|
|
|
@@ -554,6 +574,18 @@ export function serverEntrySource(pages, i18n = null) {
|
|
|
554
574
|
* temp build dir. The SSG pre-render calls `cleanup()` immediately after rendering
|
|
555
575
|
* every route; the SSR server keeps the module live and cleans up on shutdown.
|
|
556
576
|
*/
|
|
577
|
+
/**
|
|
578
|
+
* The route policies (each folder's `_config.js`) the prerender bundle needs: the route
|
|
579
|
+
* files that render in the browser, which it must not import, and the configs' `paths`.
|
|
580
|
+
*/
|
|
581
|
+
export async function routePolicies({ appDir, pages, afwc }) {
|
|
582
|
+
const found = await discoverRouteConfigs(esdevHost, appDir);
|
|
583
|
+
if (found.size === 0) return {};
|
|
584
|
+
const configs = await readRouteConfigs(esdevHost, startCompilerServer(afwc), found);
|
|
585
|
+
const resolved = resolveRoutePolicies({ root: appDir, pages, configs });
|
|
586
|
+
return { clientFiles: clientRouteFiles(resolved, appDir, pages), staticPaths: staticPathConfigs(resolved, configs) };
|
|
587
|
+
}
|
|
588
|
+
|
|
557
589
|
export async function buildServerBundle({
|
|
558
590
|
root,
|
|
559
591
|
pages,
|
|
@@ -563,11 +595,12 @@ export async function buildServerBundle({
|
|
|
563
595
|
i18n = null,
|
|
564
596
|
onCompile,
|
|
565
597
|
tmpName = ".afw-ssg",
|
|
598
|
+
appDir = join(root, "app"),
|
|
566
599
|
}) {
|
|
567
600
|
const tmp = join(root, tmpName);
|
|
568
601
|
await mkdirp(tmp);
|
|
569
602
|
const entry = join(tmp, "ssg-entry.js");
|
|
570
|
-
await writeFile(entry, serverEntrySource(pages, i18n));
|
|
603
|
+
await writeFile(entry, serverEntrySource(pages, i18n, await routePolicies({ appDir, pages, afwc })));
|
|
571
604
|
|
|
572
605
|
const bundle = await build({
|
|
573
606
|
input: entry,
|
|
@@ -579,6 +612,7 @@ export async function buildServerBundle({
|
|
|
579
612
|
// no `process` global here to read it at runtime — so it has to be folded away at
|
|
580
613
|
// bundle time. Server render is a production render.
|
|
581
614
|
define: { "process.env.NODE_ENV": '"production"' },
|
|
615
|
+
...ENV_COMPILE(root),
|
|
582
616
|
plugins: [
|
|
583
617
|
...docsPlugins,
|
|
584
618
|
afwPlugin(afwc, { failOnError: true, target: "ssg", quiet, onResult: (id) => onCompile?.(id) }),
|