@opentf/afw-build 0.5.0 → 0.6.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 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
- /** Rendering modes a project chooses from. */
6
- export const MODES = new Set(["spa", "ssg", "ssr"]);
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
- /** Throw on an unknown mode or target, before any compiler starts. */
14
- export function validateOptions({ mode = "spa", target } = {}) {
15
- if (!MODES.has(mode)) throw new Error(`Unknown AFW rendering mode: ${mode} (expected spa, ssg, or ssr)`);
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. `server` — the module is bundled for the
23
- * server (string renderers). `test` — a test run, which renders on the client.
24
- * `dev` — a dev server's browser build: the hydrating dual module when the dev server
25
- * renders pages first, as production does (`devRenders`, by default only for `ssr`);
26
- * CSR otherwise. Otherwise a release browser build: CSR for an SPA, the hydrating dual
27
- * module when the server renders first.
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({ mode = "spa", target, server = false, dev = false, test = false, devRenders = mode === "ssr" } = {}) {
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 && mode !== "spa" ? "hydrate" : "csr";
34
- return mode === "spa" ? "csr" : "hydrate";
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/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
- FRAMEWORK_IMPORT, MODES, SOURCE_FILTER, TARGETS,
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, RENDER_OF_MODE,
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentf/afw-build",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "The toolchain-neutral core of AFW's build: compiler client, compile targets and file-based routing.",
5
5
  "type": "module",
6
6
  "scripts": {
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 the plugin's `mode` shorthand. Resolution needs
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. `mode` is the plugin option as the app passed it
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` (or `mode` / the default).
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: a root `render` and a `mode` together, `paths`
105
- * outside a static folder, `data` outside a client folder.
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, mode }) {
103
+ export function resolveRoutePolicies({ root, pages, configs }) {
108
104
  const rootConfig = configs.get("");
109
- if (rootConfig?.render && mode !== undefined) {
110
- throw new Error(
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]);