@opentf/afw-build 0.5.0 → 0.7.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/compile.js +23 -14
- package/document.js +9 -1
- package/index.js +3 -3
- package/package.json +1 -1
- package/route-policy.js +18 -19
package/compile.js
CHANGED
|
@@ -2,36 +2,45 @@
|
|
|
2
2
|
// the build at hand, compile through `afwc`, and turn a failure into either a thrown
|
|
3
3
|
// diagnostic or a visible stub module.
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* How an app renders overall, from its routes' policies: `client` when every page renders
|
|
7
|
+
* in the browser, `static` when none renders per request but some are built, `server`
|
|
8
|
+
* when any renders per request. It decides the browser build's compiler target.
|
|
9
|
+
*/
|
|
10
|
+
export const APP_RENDERS = new Set(["client", "static", "server"]);
|
|
7
11
|
/** Compiler backends: live DOM, HTML-string renderers, or the hydrating dual module. */
|
|
8
12
|
export const TARGETS = new Set(["csr", "ssg", "hydrate"]);
|
|
9
13
|
|
|
10
14
|
/** Source modules the compiler owns. */
|
|
11
15
|
export const SOURCE_FILTER = /\.(mdx|md|[jt]sx)$/;
|
|
12
16
|
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Throw on an unknown option, before any compiler starts. `mode` was removed: how pages
|
|
19
|
+
* render is set per folder in `_config.js`.
|
|
20
|
+
*/
|
|
21
|
+
export function validateOptions({ mode, target } = {}) {
|
|
22
|
+
if (mode !== undefined) {
|
|
23
|
+
throw new Error('The AFW plugin\'s `mode` option was removed: set how pages render in app/_config.js, e.g. `export default defineRoute({ render: "server" })` (or "static", "client").');
|
|
24
|
+
}
|
|
16
25
|
if (target !== undefined && !TARGETS.has(target)) {
|
|
17
26
|
throw new Error(`Unknown AFW compiler target: ${target} (expected csr, ssg, or hydrate)`);
|
|
18
27
|
}
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
/**
|
|
22
|
-
* The compiler target for one module. `
|
|
23
|
-
*
|
|
24
|
-
* `
|
|
25
|
-
* renders pages first, as production does
|
|
26
|
-
*
|
|
27
|
-
* module when
|
|
31
|
+
* The compiler target for one module. `app` is how the app renders overall (see
|
|
32
|
+
* `APP_RENDERS`). `server` — the module is bundled for the server (string renderers).
|
|
33
|
+
* `test` — a test run, which renders on the client. `dev` — a dev server's browser build:
|
|
34
|
+
* the hydrating dual module when the dev server renders pages first, as production does
|
|
35
|
+
* (`devRenders`, by default only for a `server` app); CSR otherwise. Otherwise a release
|
|
36
|
+
* browser build: CSR for a client app, the hydrating dual module when pages render first.
|
|
28
37
|
*/
|
|
29
|
-
export function chooseTarget({
|
|
38
|
+
export function chooseTarget({ app = "client", target, server = false, dev = false, test = false, devRenders = app === "server" } = {}) {
|
|
30
39
|
if (target) return target;
|
|
31
40
|
if (server) return "ssg";
|
|
32
41
|
if (test) return "csr";
|
|
33
|
-
if (dev) return devRenders &&
|
|
34
|
-
return
|
|
42
|
+
if (dev) return devRenders && app !== "client" ? "hydrate" : "csr";
|
|
43
|
+
return app === "client" ? "csr" : "hydrate";
|
|
35
44
|
}
|
|
36
45
|
|
|
37
46
|
/** The compiler backend token for `target`, with `hot` selecting its refresh build. */
|
package/document.js
CHANGED
|
@@ -245,6 +245,14 @@ export async function renderDocument(render, {
|
|
|
245
245
|
return { status, html: document, dataJson, loaded: !!match, metadata, route: result.route ?? null };
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
+
/**
|
|
249
|
+
* The inline route data of a 404 page, so the client adopts it as the 404 page even
|
|
250
|
+
* where the URL matches a route (a loader's `notFound()`, or a static host serving
|
|
251
|
+
* 404.html for a path it has no file for). Mirrors `NOT_FOUND_PAYLOAD` in the
|
|
252
|
+
* `@opentf/afw` runtime (runtime/route-data.js).
|
|
253
|
+
*/
|
|
254
|
+
export const NOT_FOUND_DATA = '{"__afwNotFound":true}';
|
|
255
|
+
|
|
248
256
|
/** The 404 page as a document (`noindex`), or `null` when the app has none. */
|
|
249
257
|
export async function renderNotFound(render, { shell, baseUrl = "", preload = "" } = {}) {
|
|
250
258
|
const result = await render.renderRoute("/__afw_404__");
|
|
@@ -253,7 +261,7 @@ export async function renderNotFound(render, { shell, baseUrl = "", preload = ""
|
|
|
253
261
|
if (preload) head += `\n${preload}`;
|
|
254
262
|
return {
|
|
255
263
|
status: 404,
|
|
256
|
-
html: injectHydrationData(injectMarkup(injectHead(shell, head), result.html), result.hydration),
|
|
264
|
+
html: injectRouteData(injectHydrationData(injectMarkup(injectHead(shell, head), result.html), result.hydration), NOT_FOUND_DATA),
|
|
257
265
|
dataJson: "",
|
|
258
266
|
loaded: false,
|
|
259
267
|
metadata: result.metadata ?? {},
|
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
export { assertHost } from "./host.js";
|
|
7
7
|
export { compileError, closeCompilers, startCompilerServer } from "./compiler-client.js";
|
|
8
8
|
export {
|
|
9
|
-
|
|
9
|
+
APP_RENDERS, FRAMEWORK_IMPORT, SOURCE_FILTER, TARGETS,
|
|
10
10
|
chooseTarget, compileModule, compilerTarget, errorStub, isRouteModule, usesServerEntry, validateOptions,
|
|
11
11
|
} from "./compile.js";
|
|
12
12
|
export { ensureCompiler, findUp, resolveCompiler } from "./resolve-compiler.js";
|
|
@@ -16,8 +16,8 @@ export {
|
|
|
16
16
|
resolveAppRoot, routeKey, routesModule,
|
|
17
17
|
} from "./routes.js";
|
|
18
18
|
export {
|
|
19
|
-
CONFIG_FILE, RENDERS,
|
|
20
|
-
clientRouteFiles, discoverRouteConfigs, folderRoute, formatRouteTable, readRouteConfigs, resolveRoutePolicies, routeManifest, routeRenders, staticPathConfigs,
|
|
19
|
+
CONFIG_FILE, RENDERS,
|
|
20
|
+
clientRouteFiles, discoverRouteConfigs, folderRoute, formatRouteTable, readRouteConfigs, resolveRoutePolicies, routeManifest, routeRenders, staticPathConfigs, appRender,
|
|
21
21
|
} from "./route-policy.js";
|
|
22
22
|
export { SERVER_ID, discoverServerFiles, hasServerFiles, serverConflicts, serverModule } from "./server.js";
|
|
23
23
|
export {
|
package/package.json
CHANGED
package/route-policy.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Per-route rendering: each folder's `_config.{js,ts}` declares how its pages render
|
|
2
2
|
// (`server`, `static`, `client`), and the nearest one up the folder tree wins. The app
|
|
3
|
-
// default is the root `_config.js`, else
|
|
3
|
+
// default is the root `_config.js`, else `client`. Resolution needs
|
|
4
4
|
// only the files: the compiler reads each config without running it (`route-config`), so
|
|
5
5
|
// the result can decide what each bundle contains before any bundle exists.
|
|
6
6
|
|
|
@@ -10,9 +10,6 @@ export const CONFIG_FILE = /^_config\.(js|ts)$/;
|
|
|
10
10
|
/** The render strategies. */
|
|
11
11
|
export const RENDERS = ["server", "static", "client"];
|
|
12
12
|
|
|
13
|
-
/** The plugin's `mode` shorthand, as the app default it stands for. */
|
|
14
|
-
export const RENDER_OF_MODE = { spa: "client", ssg: "static", ssr: "server" };
|
|
15
|
-
|
|
16
13
|
const PAGE = /^page\.(mdx|md|[jt]sx)$/;
|
|
17
14
|
const LAYOUT = /^layout\.[jt]sx$/;
|
|
18
15
|
|
|
@@ -93,28 +90,20 @@ export async function readRouteConfigs(host, compiler, configs) {
|
|
|
93
90
|
/**
|
|
94
91
|
* Resolve every page's policy. `pages` are absolute page/layout/404/loading/error files
|
|
95
92
|
* (as `discoverPages` returns); only `page.*` files are routes. `configs` is the map
|
|
96
|
-
* `readRouteConfigs` returns.
|
|
97
|
-
* (`undefined` when it did not).
|
|
93
|
+
* `readRouteConfigs` returns.
|
|
98
94
|
*
|
|
99
95
|
* Returns `{ defaultRender, routes, policyOf, warnings }`:
|
|
100
96
|
* - `routes`: one entry per page, sorted by route — `{ route, folder, file, render, data,
|
|
101
|
-
* from }`, where `from` is the config file that decided `render` (
|
|
97
|
+
* from }`, where `from` is the config file that decided `render` ("default" without one).
|
|
102
98
|
* - `policyOf(folder)`: the same resolution for any folder (a layout's, say).
|
|
103
99
|
* - `warnings`: config files that govern no page.
|
|
104
|
-
* Throws on a conflict the app must fix:
|
|
105
|
-
*
|
|
100
|
+
* Throws on a conflict the app must fix: `paths` outside a static folder, `data` outside a
|
|
101
|
+
* client folder, a server or static page inside a client layout.
|
|
106
102
|
*/
|
|
107
|
-
export function resolveRoutePolicies({ root, pages, configs
|
|
103
|
+
export function resolveRoutePolicies({ root, pages, configs }) {
|
|
108
104
|
const rootConfig = configs.get("");
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
`The app default is set twice: render "${rootConfig.render}" in ${rootConfig.file} and mode "${mode}" in the plugin options. ` +
|
|
112
|
-
`Set it in one place (the root _config.js).`,
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
if (mode !== undefined && !RENDER_OF_MODE[mode]) throw new Error(`Unknown AFW rendering mode: ${mode} (expected spa, ssg, or ssr)`);
|
|
116
|
-
const defaultRender = rootConfig?.render ?? RENDER_OF_MODE[mode ?? "spa"];
|
|
117
|
-
const defaultFrom = rootConfig?.render ? rootConfig.file : mode !== undefined ? `mode: "${mode}"` : "default";
|
|
105
|
+
const defaultRender = rootConfig?.render ?? "client";
|
|
106
|
+
const defaultFrom = rootConfig?.render ? rootConfig.file : "default";
|
|
118
107
|
|
|
119
108
|
function policyOf(folder) {
|
|
120
109
|
let render = null, from = null, data = null;
|
|
@@ -186,6 +175,16 @@ export function clientRouteFiles({ policyOf }, root, files) {
|
|
|
186
175
|
return new Set(files.filter((abs) => policyOf(folderOf(relative(root, abs))).render === "client"));
|
|
187
176
|
}
|
|
188
177
|
|
|
178
|
+
/**
|
|
179
|
+
* How the app renders overall: `server` when any route renders per request, else `static`
|
|
180
|
+
* when any is built, else `client` (the default, also for an app without pages).
|
|
181
|
+
*/
|
|
182
|
+
export function appRender({ defaultRender, routes }) {
|
|
183
|
+
const strategies = new Set(routes.map((r) => r.render));
|
|
184
|
+
if (strategies.size === 0) strategies.add(defaultRender);
|
|
185
|
+
return strategies.has("server") ? "server" : strategies.has("static") ? "static" : "client";
|
|
186
|
+
}
|
|
187
|
+
|
|
189
188
|
/** The configs that declare `paths`, as `[routePattern, configFile]` for the server module. */
|
|
190
189
|
export function staticPathConfigs({ routes }, configs) {
|
|
191
190
|
return routes.filter((r) => configs.get(r.folder)?.paths).map((r) => [r.folder ? `/${r.folder}` : "/", configs.get(r.folder).file]);
|