@opentf/afw-cli 1.36.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 +22 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentf/afw-cli",
3
- "version": "1.36.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.8.0"
19
+ "@opentf/afw-build": "0.4.0",
20
+ "@opentf/esdev-plugin-afw": "0.9.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "@opentf/afw": "0.37.0"
23
+ "@opentf/afw": "0.38.0"
24
24
  },
25
25
  "keywords": [
26
26
  "afw",
package/src/shared.js CHANGED
@@ -287,19 +287,26 @@ export async function discoverLoaders(appDir, exclude = new Set()) {
287
287
  for (const entry of await readEntries(dir)) {
288
288
  if (entry.isDir) {
289
289
  if (!exclude.has(entry.name)) await walk(join(dir, entry.name));
290
- } 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));
291
291
  }
292
292
  };
293
293
  await walk(appDir);
294
294
  return out;
295
295
  }
296
296
 
297
- /** 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). */
298
299
  export function loaderRoutePath(filePath, appDir) {
299
- const r = stripAppPrefix(filePath, appDir).replace(/\/loader\.(js|ts)$/, "");
300
+ const r = stripAppPrefix(filePath, appDir).replace(/\/(?:layout\.)?loader\.(js|ts)$/, "");
300
301
  return r === "" ? "/" : r;
301
302
  }
302
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
+
303
310
  /**
304
311
  * Detect folders that hold both a `page.*` and a `route.*` — they'd resolve to the
305
312
  * same URL. Returns the conflicting `{ path, page, route }` entries (empty = none).
@@ -331,6 +338,12 @@ export async function detectLoaderConflicts(appDir, exclude = new Set()) {
331
338
  };
332
339
  for (const loader of await discoverLoaders(appDir, exclude)) {
333
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
+ }
334
347
  if (await anyExists(dir, ["page.jsx", "page.tsx", "page.mdx", "page.md"])) continue;
335
348
  const hasRoute = await anyExists(dir, ["route.js", "route.ts"]);
336
349
  conflicts.push({
@@ -354,7 +367,7 @@ export async function assertNoRouteConflicts(appDir, exclude = new Set()) {
354
367
  const loaderConflicts = await detectLoaderConflicts(appDir, exclude);
355
368
  if (loaderConflicts.length > 0) {
356
369
  const lines = loaderConflicts.map((c) => ` ${c.route}\n loader: ${c.loader}\n ${c.reason}`).join("\n");
357
- 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}`);
358
371
  }
359
372
  }
360
373
 
@@ -434,14 +447,16 @@ export async function emitApiBundle({ root, appDir, afwEntry, exclude, i18n, out
434
447
  * `loader.{js,ts}` module (keyed by absolute path so the registry derives each
435
448
  * route from it) and export the composed registry. Mirrors `apiEntrySource`.
436
449
  */
437
- export function loaderEntrySource(loaderFiles, appDir, i18n = null) {
450
+ export function loaderEntrySource(loaderFiles, appDir, i18n = null, pageRoutes = []) {
438
451
  const imports = loaderFiles.map((p, i) => `import * as l${i} from ${JSON.stringify(p)};`).join("\n");
439
452
  const map = loaderFiles.map((p, i) => ` [${JSON.stringify(p)}]: l${i},`).join("\n");
440
453
  const i18nOpt =
441
454
  i18n && Array.isArray(i18n.locales) && i18n.locales.length
442
455
  ? `, i18n: ${JSON.stringify({ locales: i18n.locales, defaultLocale: i18n.defaultLocale })}`
443
456
  : "";
444
- 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`;
445
460
  return (
446
461
  `import { createLoaderRegistry } from "@opentf/afw/server";\n` +
447
462
  `${imports}\n` +
@@ -462,7 +477,7 @@ export async function emitLoaderBundle({ root, appDir, afwEntry, exclude, i18n,
462
477
  const tmp = join(root, ".afw-loaders-build");
463
478
  await mkdirp(tmp);
464
479
  const entry = join(tmp, "loaders-entry.js");
465
- await writeFile(entry, loaderEntrySource(files, appDir, i18n));
480
+ await writeFile(entry, loaderEntrySource(files, appDir, i18n, await pageRoutesOf(appDir, exclude ?? new Set())));
466
481
 
467
482
  await mkdirp(outDir);
468
483
  const bundle = await build({