@opentf/afw-cli 1.38.1 → 1.39.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/package.json +4 -4
- package/src/prerender.js +4 -2
- package/src/shared.js +30 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentf/afw-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.39.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.11.
|
|
19
|
+
"@opentf/afw-build": "0.7.0",
|
|
20
|
+
"@opentf/esdev-plugin-afw": "0.11.1"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@opentf/afw": "0.
|
|
23
|
+
"@opentf/afw": "0.40.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
|
|
|
@@ -526,24 +527,33 @@ export function cssPlugin() {
|
|
|
526
527
|
// sees real namespaces, enabling `getStaticPaths`) and re-export the render API.
|
|
527
528
|
// Shared by the SSG pre-render and the SSR server — both render through the same
|
|
528
529
|
// SSG-compiled bundle (ARCHITECTURE.md §6: "SSR … shares the SSG path").
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
530
|
+
/**
|
|
531
|
+
* The prerender bundle's entry. `clientFiles` (route files whose folder's `_config.js`
|
|
532
|
+
* says `render: "client"`) are registered as `clientRoute()` and never imported, so their
|
|
533
|
+
* browser-only code stays out of the server bundle; `staticPaths` (`[route, configFile]`)
|
|
534
|
+
* registers each `_config.js`'s `paths`.
|
|
535
|
+
*/
|
|
536
|
+
export function serverEntrySource(pages, i18n = null, { clientFiles = new Set(), staticPaths = [] } = {}) {
|
|
537
|
+
const imports = pages.map((p, i) => (clientFiles.has(p) ? null : `import * as p${i} from ${JSON.stringify(p)};`)).filter(Boolean).join("\n");
|
|
538
|
+
const map = pages.map((p, i) => ` [${JSON.stringify(p)}]: ${clientFiles.has(p) ? "clientRoute()" : `p${i}`},`).join("\n");
|
|
539
|
+
const paths = staticPaths.map(([, file], i) => `import sp${i} from ${JSON.stringify(file)};`).join("\n");
|
|
532
540
|
// i18n: the server-side router needs the locales too — so renderRoute can strip
|
|
533
541
|
// a `/fr/...` prefix to match the route table and set router.locale for `t()`
|
|
534
542
|
// (docs/I18N.md §6). Without this the prefix wouldn't match and pages would
|
|
535
543
|
// render in the default locale (or 404).
|
|
536
544
|
const i18nOn = i18n && Array.isArray(i18n.locales) && i18n.locales.length;
|
|
537
|
-
const named = i18nOn ? "
|
|
545
|
+
const named = ["clientRoute", "registerRoutes", "registerStaticPaths", ...(i18nOn ? ["configureI18n"] : [])].join(", ");
|
|
538
546
|
const i18nCall = i18nOn
|
|
539
547
|
? `configureI18n(${JSON.stringify({ locales: i18n.locales, defaultLocale: i18n.defaultLocale })});\n`
|
|
540
548
|
: "";
|
|
541
549
|
return (
|
|
542
550
|
`${imports}\n` +
|
|
551
|
+
(paths ? `${paths}\n` : "") +
|
|
543
552
|
`import { ${named} } from "@opentf/afw/server";\n` +
|
|
544
553
|
`export { renderRoute, renderHead, collectRoutePaths, resolveMetadata } from "@opentf/afw/server";\n` +
|
|
545
554
|
i18nCall +
|
|
546
|
-
`registerRoutes({\n${map}\n});\n`
|
|
555
|
+
`registerRoutes({\n${map}\n});\n` +
|
|
556
|
+
(staticPaths.length ? `registerStaticPaths({\n${staticPaths.map(([route], i) => ` [${JSON.stringify(route)}]: sp${i}.paths,`).join("\n")}\n});\n` : "")
|
|
547
557
|
);
|
|
548
558
|
}
|
|
549
559
|
|
|
@@ -554,6 +564,18 @@ export function serverEntrySource(pages, i18n = null) {
|
|
|
554
564
|
* temp build dir. The SSG pre-render calls `cleanup()` immediately after rendering
|
|
555
565
|
* every route; the SSR server keeps the module live and cleans up on shutdown.
|
|
556
566
|
*/
|
|
567
|
+
/**
|
|
568
|
+
* The route policies (each folder's `_config.js`) the prerender bundle needs: the route
|
|
569
|
+
* files that render in the browser, which it must not import, and the configs' `paths`.
|
|
570
|
+
*/
|
|
571
|
+
export async function routePolicies({ appDir, pages, afwc }) {
|
|
572
|
+
const found = await discoverRouteConfigs(esdevHost, appDir);
|
|
573
|
+
if (found.size === 0) return {};
|
|
574
|
+
const configs = await readRouteConfigs(esdevHost, startCompilerServer(afwc), found);
|
|
575
|
+
const resolved = resolveRoutePolicies({ root: appDir, pages, configs });
|
|
576
|
+
return { clientFiles: clientRouteFiles(resolved, appDir, pages), staticPaths: staticPathConfigs(resolved, configs) };
|
|
577
|
+
}
|
|
578
|
+
|
|
557
579
|
export async function buildServerBundle({
|
|
558
580
|
root,
|
|
559
581
|
pages,
|
|
@@ -563,11 +585,12 @@ export async function buildServerBundle({
|
|
|
563
585
|
i18n = null,
|
|
564
586
|
onCompile,
|
|
565
587
|
tmpName = ".afw-ssg",
|
|
588
|
+
appDir = join(root, "app"),
|
|
566
589
|
}) {
|
|
567
590
|
const tmp = join(root, tmpName);
|
|
568
591
|
await mkdirp(tmp);
|
|
569
592
|
const entry = join(tmp, "ssg-entry.js");
|
|
570
|
-
await writeFile(entry, serverEntrySource(pages, i18n));
|
|
593
|
+
await writeFile(entry, serverEntrySource(pages, i18n, await routePolicies({ appDir, pages, afwc })));
|
|
571
594
|
|
|
572
595
|
const bundle = await build({
|
|
573
596
|
input: entry,
|