@opentf/afw-cli 1.35.0 → 1.37.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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/shared.js +29 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentf/afw-cli",
3
- "version": "1.35.0",
3
+ "version": "1.37.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.3.0",
20
- "@opentf/esdev-plugin-afw": "0.7.1"
19
+ "@opentf/afw-build": "0.4.0",
20
+ "@opentf/esdev-plugin-afw": "0.9.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "@opentf/afw": "0.36.0"
23
+ "@opentf/afw": "0.38.0"
24
24
  },
25
25
  "keywords": [
26
26
  "afw",
package/src/shared.js CHANGED
@@ -76,7 +76,10 @@ export async function loadDocsPlugins(root, appDir, config, exclude = new Set())
76
76
  if (!docs && !blog) return [];
77
77
  try {
78
78
  const entry = await resolveFrom("@opentf/afw-docs/build", root);
79
- const { docsNavPlugin, blogPostsPlugin, lastUpdatedPlugin } = await import(toFileURL(entry).href);
79
+ const { afwDocsPlugin, docsNavPlugin, blogPostsPlugin, lastUpdatedPlugin, codeHighlightPlugin } = await import(toFileURL(entry).href);
80
+ // The same set the project's `esdev.json` entry gets: afw-docs decides it from the
81
+ // config. An older afw-docs has no `afwDocsPlugin`; assemble the set here instead.
82
+ if (afwDocsPlugin) return await afwDocsPlugin({ appDir, exclude: [...exclude], root, config });
80
83
  const plugins = [];
81
84
  // Resolves `@opentf/afw-docs/nav` to a section map — one generated tree per top-level
82
85
  // folder under app/. Any folder with a DocsLayout becomes a section automatically.
@@ -86,6 +89,9 @@ export async function loadDocsPlugins(root, appDir, config, exclude = new Set())
86
89
  // Resolves `@opentf/afw-docs/updated` to the per-page last-updated map (every route),
87
90
  // when last-updated is turned on anywhere.
88
91
  if (wantsLastUpdated(config)) plugins.push(lastUpdatedPlugin({ appDir, exclude, blogDir: blog ? blog.dir ?? "blog" : undefined }));
92
+ // Highlights the code fences of compiled `.md`/`.mdx` pages (an older afw-docs
93
+ // without it leaves them as plain text).
94
+ if (codeHighlightPlugin) plugins.push(codeHighlightPlugin());
89
95
  return plugins;
90
96
  } catch (e) {
91
97
  console.warn(
@@ -281,19 +287,26 @@ export async function discoverLoaders(appDir, exclude = new Set()) {
281
287
  for (const entry of await readEntries(dir)) {
282
288
  if (entry.isDir) {
283
289
  if (!exclude.has(entry.name)) await walk(join(dir, entry.name));
284
- } else if (/^loader\.(js|ts)$/.test(entry.name)) out.push(join(dir, entry.name));
290
+ } else if (/^(?:layout\.)?loader\.(js|ts)$/.test(entry.name)) out.push(join(dir, entry.name));
285
291
  }
286
292
  };
287
293
  await walk(appDir);
288
294
  return out;
289
295
  }
290
296
 
291
- /** The page route a `.../app/<...>/loader.{js,ts}` file feeds (folder = URL). */
297
+ /** The route a `.../app/<...>/loader.{js,ts}` (its page) or `layout.loader.{js,ts}`
298
+ * (its layout's folder) file feeds (folder = URL). */
292
299
  export function loaderRoutePath(filePath, appDir) {
293
- const r = stripAppPrefix(filePath, appDir).replace(/\/loader\.(js|ts)$/, "");
300
+ const r = stripAppPrefix(filePath, appDir).replace(/\/(?:layout\.)?loader\.(js|ts)$/, "");
294
301
  return r === "" ? "/" : r;
295
302
  }
296
303
 
304
+ /** The route of every `page.*` under `appDir` (folder = URL). */
305
+ async function pageRoutesOf(appDir, exclude) {
306
+ const pages = (await discoverPages(appDir, exclude)).filter((p) => /\/page\.[^/]+$/.test(p));
307
+ return pages.map((p) => stripAppPrefix(p, appDir).replace(/\/page\.[^/]+$/, "") || "/").sort();
308
+ }
309
+
297
310
  /**
298
311
  * Detect folders that hold both a `page.*` and a `route.*` — they'd resolve to the
299
312
  * same URL. Returns the conflicting `{ path, page, route }` entries (empty = none).
@@ -325,6 +338,12 @@ export async function detectLoaderConflicts(appDir, exclude = new Set()) {
325
338
  };
326
339
  for (const loader of await discoverLoaders(appDir, exclude)) {
327
340
  const dir = dirname(loader);
341
+ if (/(?:^|\/)layout\.loader\.(js|ts)$/.test(loader)) {
342
+ // A layout loader feeds the layout in its folder (and the pages beneath it).
343
+ if (await anyExists(dir, ["layout.jsx", "layout.tsx", "layout.mdx", "layout.md"])) continue;
344
+ conflicts.push({ loader, route: loaderRoutePath(loader, appDir), reason: "has no sibling layout.* to feed" });
345
+ continue;
346
+ }
328
347
  if (await anyExists(dir, ["page.jsx", "page.tsx", "page.mdx", "page.md"])) continue;
329
348
  const hasRoute = await anyExists(dir, ["route.js", "route.ts"]);
330
349
  conflicts.push({
@@ -348,7 +367,7 @@ export async function assertNoRouteConflicts(appDir, exclude = new Set()) {
348
367
  const loaderConflicts = await detectLoaderConflicts(appDir, exclude);
349
368
  if (loaderConflicts.length > 0) {
350
369
  const lines = loaderConflicts.map((c) => ` ${c.route}\n loader: ${c.loader}\n ${c.reason}`).join("\n");
351
- fail(`a loader.* file must sit next to the page.* it feeds:\n${lines}`);
370
+ fail(`a loader.* file must sit next to the page.* it feeds, and a layout.loader.* next to its layout.*:\n${lines}`);
352
371
  }
353
372
  }
354
373
 
@@ -428,14 +447,16 @@ export async function emitApiBundle({ root, appDir, afwEntry, exclude, i18n, out
428
447
  * `loader.{js,ts}` module (keyed by absolute path so the registry derives each
429
448
  * route from it) and export the composed registry. Mirrors `apiEntrySource`.
430
449
  */
431
- export function loaderEntrySource(loaderFiles, appDir, i18n = null) {
450
+ export function loaderEntrySource(loaderFiles, appDir, i18n = null, pageRoutes = []) {
432
451
  const imports = loaderFiles.map((p, i) => `import * as l${i} from ${JSON.stringify(p)};`).join("\n");
433
452
  const map = loaderFiles.map((p, i) => ` [${JSON.stringify(p)}]: l${i},`).join("\n");
434
453
  const i18nOpt =
435
454
  i18n && Array.isArray(i18n.locales) && i18n.locales.length
436
455
  ? `, i18n: ${JSON.stringify({ locales: i18n.locales, defaultLocale: i18n.defaultLocale })}`
437
456
  : "";
438
- const opts = `\n{ appDir: ${JSON.stringify(appDir ?? "")}${i18nOpt} },\n`;
457
+ // `pageRoutes`: a page beneath a layout.loader.* matches without a loader of its own.
458
+ const pagesOpt = pageRoutes.length ? `, pageRoutes: ${JSON.stringify(pageRoutes)}` : "";
459
+ const opts = `\n{ appDir: ${JSON.stringify(appDir ?? "")}${i18nOpt}${pagesOpt} },\n`;
439
460
  return (
440
461
  `import { createLoaderRegistry } from "@opentf/afw/server";\n` +
441
462
  `${imports}\n` +
@@ -456,7 +477,7 @@ export async function emitLoaderBundle({ root, appDir, afwEntry, exclude, i18n,
456
477
  const tmp = join(root, ".afw-loaders-build");
457
478
  await mkdirp(tmp);
458
479
  const entry = join(tmp, "loaders-entry.js");
459
- await writeFile(entry, loaderEntrySource(files, appDir, i18n));
480
+ await writeFile(entry, loaderEntrySource(files, appDir, i18n, await pageRoutesOf(appDir, exclude ?? new Set())));
460
481
 
461
482
  await mkdirp(outDir);
462
483
  const bundle = await build({