@jxsuite/compiler 8.6.0 → 8.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/dist/cli.js +250 -215
- package/dist/compiler.js +145 -123
- package/package.json +11 -9
- package/src/cli.ts +12 -5
- package/src/shared.ts +249 -1
- package/src/site/bun-plugin.ts +61 -0
- package/src/site/bundler-backend.bun.ts +15 -3
- package/src/site/bundler-backend.node.ts +24 -1
- package/src/site/bundler-backend.ts +21 -4
- package/src/site/bundler.ts +28 -6
- package/src/site/component-set.ts +179 -0
- package/src/site/convert-command.ts +5 -27
- package/src/site/csp.ts +38 -8
- package/src/site/document-scripts.ts +231 -0
- package/src/site/format-command.ts +29 -0
- package/src/site/headers-emitter.ts +1 -1
- package/src/site/layout-resolver.ts +5 -4
- package/src/site/server-command.ts +63 -0
- package/src/site/service-worker.ts +19 -1
- package/src/site/site-build.ts +296 -84
- package/src/site/validate-command.ts +15 -6
- package/src/targets/compile-client.ts +9 -20
- package/src/targets/compile-element.ts +53 -73
- package/src/targets/compile-server.ts +49 -2
package/src/site/site-build.ts
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* compile 5. Emit compiled files to dist/ 6. Generate redirects
|
|
7
7
|
*
|
|
8
8
|
* This is the Phase 1 implementation of site-architecture spec §12.
|
|
9
|
+
*
|
|
10
|
+
* @docs framework/build
|
|
11
|
+
* @docs framework/concepts/lists
|
|
9
12
|
*/
|
|
10
13
|
|
|
11
14
|
import {
|
|
@@ -13,7 +16,6 @@ import {
|
|
|
13
16
|
existsSync,
|
|
14
17
|
mkdirSync,
|
|
15
18
|
readFileSync,
|
|
16
|
-
readdirSync,
|
|
17
19
|
rmSync,
|
|
18
20
|
statSync,
|
|
19
21
|
writeFileSync,
|
|
@@ -24,6 +26,7 @@ import {
|
|
|
24
26
|
isJsonObject,
|
|
25
27
|
isMappedArray,
|
|
26
28
|
isNonNullObject,
|
|
29
|
+
isPrototypeDef,
|
|
27
30
|
isRef,
|
|
28
31
|
isString,
|
|
29
32
|
isTemplateString,
|
|
@@ -46,6 +49,7 @@ import {
|
|
|
46
49
|
bundleSource,
|
|
47
50
|
resolveSidecarEntry,
|
|
48
51
|
} from "./bundler.ts";
|
|
52
|
+
import type { BundleRequest } from "./bundler.ts";
|
|
49
53
|
import { loadProjectConfig } from "./site-loader.ts";
|
|
50
54
|
import type { ResolvedProjectConfig } from "./site-loader.ts";
|
|
51
55
|
import {
|
|
@@ -68,6 +72,13 @@ import { collectAssetRefs, copyMountedAssets, loadAssetMounts } from "./asset-mo
|
|
|
68
72
|
import type { AssetMount } from "@jxsuite/schema/asset-paths";
|
|
69
73
|
import type { FormatEntry, FormatRegistry } from "@jxsuite/schema/format-registry";
|
|
70
74
|
import type { ExtensionRegistry } from "@jxsuite/schema/extension-registry";
|
|
75
|
+
import { collectSiteComponents } from "./component-set.ts";
|
|
76
|
+
import { documentScripts, qualifiedDocument } from "./document-scripts.ts";
|
|
77
|
+
import {
|
|
78
|
+
isScriptSpecifier,
|
|
79
|
+
parseScriptSpecifier,
|
|
80
|
+
scriptRefSide,
|
|
81
|
+
} from "@jxsuite/schema/document-scripts";
|
|
71
82
|
import { layoutChain, resolveLayout } from "./layout-resolver.ts";
|
|
72
83
|
import { dedupeServerEntries, documentServerEntries } from "./server-entries.ts";
|
|
73
84
|
import type { DedupedServerEntries, ServerEntryRecord } from "./server-entries.ts";
|
|
@@ -84,6 +95,7 @@ import type { SiteConnectorSpec, SiteMountSpec } from "../targets/compile-server
|
|
|
84
95
|
import {
|
|
85
96
|
buildComponentCSS,
|
|
86
97
|
buildInitialScope,
|
|
98
|
+
collectSrcImports,
|
|
87
99
|
buildInstanceScope,
|
|
88
100
|
collectStyles,
|
|
89
101
|
colorSchemePrePaintScript,
|
|
@@ -91,12 +103,16 @@ import {
|
|
|
91
103
|
definitionHasSlot,
|
|
92
104
|
escapeStyleText,
|
|
93
105
|
evaluateStaticTemplate,
|
|
106
|
+
inheritProjectFormulas,
|
|
94
107
|
isComponentFullyStatic,
|
|
108
|
+
isPureNamedFormula,
|
|
109
|
+
isRuntimeOnlyStateKey,
|
|
95
110
|
isSingleExpression,
|
|
96
111
|
liftPropsAttributes,
|
|
97
112
|
nextSlotId,
|
|
98
113
|
preRenderComponentHtml,
|
|
99
114
|
pureSchemeOf,
|
|
115
|
+
referencesStateKey,
|
|
100
116
|
renderStaticNode,
|
|
101
117
|
resolveHostStyle,
|
|
102
118
|
resolveRefValue,
|
|
@@ -234,7 +250,9 @@ export async function buildSite(
|
|
|
234
250
|
// Client sidecar bundles: bundleable Function-def `$src` specifiers (and lowered-def `$bundle`
|
|
235
251
|
// Hints) are rewritten to their /assets/ URL during compilation, registered here, and bundled
|
|
236
252
|
// In step 6d (spec.md §12). Keyed by asset path so duplicate specifiers bundle once.
|
|
237
|
-
|
|
253
|
+
/* A document's own script (spec.md §23.2) is bundled like a sidecar, but it is not a file: an
|
|
254
|
+
entry with `script` is one, named by its qualified specifier and read from its document. */
|
|
255
|
+
const sidecarBundles = new Map<string, { entryPath: string; specifier: string; script?: true }>();
|
|
238
256
|
/*
|
|
239
257
|
* Files a `$head` entry names by bare specifier — a package's stylesheet, most often. Copied
|
|
240
258
|
* rather than bundled.
|
|
@@ -307,7 +325,57 @@ export async function buildSite(
|
|
|
307
325
|
}
|
|
308
326
|
assetClaims.set(assetPath, { entryPath, specifier });
|
|
309
327
|
};
|
|
328
|
+
/*
|
|
329
|
+
* A document's client module: one bundle per document, keyed on the document's project-relative
|
|
330
|
+
* path, so two documents each exporting `bump` are two files (spec.md §23.2). Whether the
|
|
331
|
+
* document carries a client script at all is found when the bundle is made, which reports it.
|
|
332
|
+
*/
|
|
333
|
+
const scriptErrors = new Set<string>();
|
|
334
|
+
/** A layout's entry is rewritten once per page it wraps, and its mistake is one mistake. */
|
|
335
|
+
const scriptError = (message: string) => {
|
|
336
|
+
if (!scriptErrors.has(message)) {
|
|
337
|
+
scriptErrors.add(message);
|
|
338
|
+
errors.push(message);
|
|
339
|
+
console.error(message);
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
const rewriteScriptSrc = (specifier: string): string => {
|
|
343
|
+
const parsed = parseScriptSpecifier(specifier);
|
|
344
|
+
if (parsed === null) {
|
|
345
|
+
scriptError(`$src "${specifier}" is not a document script specifier`);
|
|
346
|
+
return specifier;
|
|
347
|
+
}
|
|
348
|
+
if (parsed.side === "server") {
|
|
349
|
+
scriptError(
|
|
350
|
+
`${parsed.path}: a client function names "#server". A server module is called by a ` +
|
|
351
|
+
`timing: "server" entry and never imported by a page`,
|
|
352
|
+
);
|
|
353
|
+
return specifier;
|
|
354
|
+
}
|
|
355
|
+
try {
|
|
356
|
+
const assetPath = sidecarAssetPath(`./${parsed.path}`);
|
|
357
|
+
claimAsset(assetPath, specifier, specifier);
|
|
358
|
+
sidecarBundles.set(assetPath, { entryPath: specifier, script: true, specifier });
|
|
359
|
+
return assetPath;
|
|
360
|
+
} catch (error) {
|
|
361
|
+
scriptError(`Script of ${parsed.path}: ${errorMessage(error)}`);
|
|
362
|
+
return specifier;
|
|
363
|
+
}
|
|
364
|
+
};
|
|
310
365
|
const rewriteSidecarSrc = (specifier: string, docDir: string | null): string => {
|
|
366
|
+
if (isScriptSpecifier(specifier)) {
|
|
367
|
+
return rewriteScriptSrc(specifier);
|
|
368
|
+
}
|
|
369
|
+
if (scriptRefSide(specifier) !== null) {
|
|
370
|
+
/* Every document the build reads is qualified as it is read, so this is an entry written
|
|
371
|
+
somewhere that has no document of its own to name: `project.json`'s `state`. */
|
|
372
|
+
scriptError(
|
|
373
|
+
`$src "${specifier}" names its own document's script, but it is not in a document: ` +
|
|
374
|
+
`project.json has no $script. Move the entry into the page, layout or component that ` +
|
|
375
|
+
`carries the script`,
|
|
376
|
+
);
|
|
377
|
+
return specifier;
|
|
378
|
+
}
|
|
311
379
|
if (!isBundleableSrc(specifier)) {
|
|
312
380
|
return specifier;
|
|
313
381
|
}
|
|
@@ -353,6 +421,8 @@ export async function buildSite(
|
|
|
353
421
|
// ── 1b. Build the extension registry from project.json#/extensions ─────
|
|
354
422
|
const registry = await buildProjectExtensionRegistry(projectRoot, projectConfig);
|
|
355
423
|
const formatRegistry = registry.formats;
|
|
424
|
+
// Every document's own modules (spec.md §23.2), read with the formats this project registered.
|
|
425
|
+
const scripts = documentScripts(projectRoot, (path) => readPageDocument(path, formatRegistry));
|
|
356
426
|
if (formatRegistry.entries.length > 0) {
|
|
357
427
|
log(
|
|
358
428
|
` Registered ${formatRegistry.entries.length} format(s): ${formatRegistry.entries
|
|
@@ -455,40 +525,32 @@ export async function buildSite(
|
|
|
455
525
|
let fileCount = 0;
|
|
456
526
|
|
|
457
527
|
// ── 5. Compile site components ──────────────────────────────────────────
|
|
458
|
-
const componentsDir = resolve(projectRoot, "components");
|
|
459
528
|
const compiledComponentTags: string[] = [];
|
|
460
529
|
const componentCSS = new Map<string, string>(); // TagName → CSS text
|
|
461
530
|
const componentDefs = new Map<string, JxElement>(); // TagName → parsed component definition
|
|
462
531
|
// TagName → the absolute path its definition was read from, for what is relative to that file.
|
|
463
532
|
const componentPathByTag = new Map<string, string>();
|
|
464
|
-
|
|
533
|
+
/* What this build compiles: every file directly in `components/`, plus every component a page,
|
|
534
|
+
a layout or project.json names by `$ref`, wherever it lives (site-architecture.md §10.3). */
|
|
535
|
+
const { components: siteComponents, errors: componentErrors } = await collectSiteComponents({
|
|
536
|
+
formatRegistry,
|
|
537
|
+
pageSources: routes.map((route) => route.sourcePath),
|
|
538
|
+
projectConfig,
|
|
539
|
+
projectRoot,
|
|
540
|
+
});
|
|
541
|
+
for (const message of componentErrors) {
|
|
542
|
+
errors.push(message);
|
|
543
|
+
console.error(message);
|
|
544
|
+
}
|
|
545
|
+
if (siteComponents.length > 0) {
|
|
465
546
|
log("Compiling components...");
|
|
466
|
-
const componentExtensions = [".json", ...formatRegistry.documentExtensions("component")];
|
|
467
|
-
/* Sorted, because this order is the order of the sheets in every page's `<head>`, and readdir
|
|
468
|
-
has none of its own: on ext4 it follows a hash of the name, so the same project built to
|
|
469
|
-
different bytes on different machines. */
|
|
470
|
-
const componentFiles = readdirSync(componentsDir)
|
|
471
|
-
.filter((f: string) => componentExtensions.some((ext) => f.endsWith(ext)))
|
|
472
|
-
.toSorted();
|
|
473
547
|
const componentOutDir = resolve(outDir, "components");
|
|
474
548
|
mkdirSync(componentOutDir, { recursive: true });
|
|
475
549
|
|
|
476
550
|
// Emitted modules are named after the component's `tagName` — the name the page's loader
|
|
477
|
-
// `<script>` and the CSS sidecar both use
|
|
478
|
-
//
|
|
479
|
-
|
|
480
|
-
const componentTagByPath = new Map<string, string>();
|
|
481
|
-
for (const file of componentFiles) {
|
|
482
|
-
const componentPath = resolve(componentsDir, file);
|
|
483
|
-
try {
|
|
484
|
-
const { tagName: depTag } = await readPageDocument(componentPath, formatRegistry);
|
|
485
|
-
if (depTag) {
|
|
486
|
-
componentTagByPath.set(componentPath, depTag);
|
|
487
|
-
}
|
|
488
|
-
} catch {
|
|
489
|
-
// Left to the compile pass below, which reports the parse failure with its file context.
|
|
490
|
-
}
|
|
491
|
-
}
|
|
551
|
+
// `<script>` and the CSS sidecar both use, wherever the source file lives. So a component's
|
|
552
|
+
// `$elements` imports are renamed to match, which needs every component's tag up front.
|
|
553
|
+
const componentTagByPath = new Map(siteComponents.map((c) => [c.path, c.tagName]));
|
|
492
554
|
|
|
493
555
|
/*
|
|
494
556
|
* A component is a JavaScript module the page loads, and the page rewrite (`rewriteHtmlBase`) never
|
|
@@ -501,9 +563,11 @@ export async function buildSite(
|
|
|
501
563
|
// So it is given the deployment base now (site-architecture.md §14.7).
|
|
502
564
|
const serverBaseUrl = serverRoutePrefix(componentBase);
|
|
503
565
|
|
|
504
|
-
for (const
|
|
566
|
+
for (const { doc: written, path: componentPath } of siteComponents) {
|
|
567
|
+
/* The definition every page prerenders from, with the project formulas it reads (spec.md
|
|
568
|
+
§13.4). The module is compiled from the file, and is given the same project state. */
|
|
569
|
+
const doc = inheritProjectFormulas(written, projectConfig.state);
|
|
505
570
|
try {
|
|
506
|
-
const componentPath = resolve(componentsDir, file);
|
|
507
571
|
const elementOptions: CompileElementOptions = {
|
|
508
572
|
formats: formatRegistry,
|
|
509
573
|
resolveElementPath: (refPath: string, currentDir: string | null) => {
|
|
@@ -511,6 +575,8 @@ export async function buildSite(
|
|
|
511
575
|
const depTag = componentTagByPath.get(depPath);
|
|
512
576
|
return depTag ? `./${depTag}.js` : refPath.replace(/\.(?:json|jxn)$/i, ".js");
|
|
513
577
|
},
|
|
578
|
+
projectRoot,
|
|
579
|
+
projectState: projectConfig.state,
|
|
514
580
|
rewriteSrc: (specifier: string, docDir: string | null) =>
|
|
515
581
|
withBase(componentBase, rewriteSidecarSrc(specifier, docDir)),
|
|
516
582
|
serverBaseUrl,
|
|
@@ -548,7 +614,6 @@ export async function buildSite(
|
|
|
548
614
|
}
|
|
549
615
|
|
|
550
616
|
// Pre-render component HTML scaffold and CSS sidecar
|
|
551
|
-
const doc = await readPageDocument(componentPath, formatRegistry);
|
|
552
617
|
if (doc.tagName) {
|
|
553
618
|
componentDefs.set(doc.tagName, doc);
|
|
554
619
|
componentPathByTag.set(doc.tagName, componentPath);
|
|
@@ -576,6 +641,7 @@ export async function buildSite(
|
|
|
576
641
|
}
|
|
577
642
|
} catch (error) {
|
|
578
643
|
const message = errorMessage(error);
|
|
644
|
+
const file = relative(projectRoot, componentPath).replaceAll("\\", "/");
|
|
579
645
|
errors.push(`Error compiling component ${file}: ${message}`);
|
|
580
646
|
console.error(`Error compiling component ${file}: ${message}`);
|
|
581
647
|
}
|
|
@@ -601,15 +667,25 @@ export async function buildSite(
|
|
|
601
667
|
);
|
|
602
668
|
}
|
|
603
669
|
if (projectConfig.state) {
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
projectRoot,
|
|
611
|
-
),
|
|
670
|
+
const projectEntries = documentServerEntries(
|
|
671
|
+
/* SAFETY: `ProjectConfig.state` is a plain record of state entries, and the collector reads
|
|
672
|
+
only a document's `state`, so a document carrying that one member is all it needs. */
|
|
673
|
+
{ state: projectConfig.state } as JxElement,
|
|
674
|
+
projectFilePath(projectRoot),
|
|
675
|
+
projectRoot,
|
|
612
676
|
);
|
|
677
|
+
for (const entry of projectEntries) {
|
|
678
|
+
// `project.json` carries no `$script`, so a `#server` written there names nothing.
|
|
679
|
+
if (scriptRefSide(entry.src) === null) {
|
|
680
|
+
siteServerEntries.push(entry);
|
|
681
|
+
} else {
|
|
682
|
+
scriptError(
|
|
683
|
+
`Server function "${entry.exportName}" in ${entry.declaredIn} names "${entry.src}", ` +
|
|
684
|
+
`its own document's script, but project.json has no $script. Move the entry into ` +
|
|
685
|
+
`the page, layout or component that carries the script`,
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
613
689
|
}
|
|
614
690
|
|
|
615
691
|
// ── 6. Compile each route ───────────────────────────────────────────────
|
|
@@ -1015,6 +1091,8 @@ export async function buildSite(
|
|
|
1015
1091
|
await bundleWorkerSource(workerSource, {
|
|
1016
1092
|
adapter,
|
|
1017
1093
|
outfile: resolve(outDir, workerName),
|
|
1094
|
+
// A server function may live in a document's own server script (spec.md §23.2).
|
|
1095
|
+
plugins: [scripts.serverPlugin],
|
|
1018
1096
|
projectRoot,
|
|
1019
1097
|
});
|
|
1020
1098
|
fileCount += 1;
|
|
@@ -1086,10 +1164,20 @@ export async function buildSite(
|
|
|
1086
1164
|
try {
|
|
1087
1165
|
const outfile = resolve(outDir, assetPath.replace(/^\//, ""));
|
|
1088
1166
|
mkdirSync(dirname(outfile), { recursive: true });
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
)
|
|
1167
|
+
/* A document's script is bundled from its text, standing beside its document. Every
|
|
1168
|
+
bundle gets the plugin, because a file module may import a `.jxn` document too. */
|
|
1169
|
+
const request: BundleRequest = { entryPath: bundle.entryPath, outfile };
|
|
1170
|
+
if (bundle.script) {
|
|
1171
|
+
const module = await scripts.module(bundle.specifier);
|
|
1172
|
+
request.entryPath = module.path;
|
|
1173
|
+
request.contents = module.contents;
|
|
1174
|
+
}
|
|
1175
|
+
await bundleEntry(request, {
|
|
1176
|
+
external: CLIENT_EXTERNALS,
|
|
1177
|
+
minify,
|
|
1178
|
+
plugins: [scripts.clientPlugin],
|
|
1179
|
+
target: "browser",
|
|
1180
|
+
});
|
|
1093
1181
|
fileCount += 1;
|
|
1094
1182
|
log(` ${bundle.specifier} → ${assetPath}`);
|
|
1095
1183
|
} catch (error) {
|
|
@@ -1410,8 +1498,13 @@ async function compilePage(
|
|
|
1410
1498
|
`projectConfig.url`, which is already here, so the two cannot disagree. */
|
|
1411
1499
|
const basePath = siteBasePath(projectConfig.url);
|
|
1412
1500
|
|
|
1413
|
-
// Load the raw page document (.json natively, other formats via the registry)
|
|
1414
|
-
|
|
1501
|
+
// Load the raw page document (.json natively, other formats via the registry). Its `#client` and
|
|
1502
|
+
// `#server` name this page from here on, before a layout's can meet them (spec.md §23.2).
|
|
1503
|
+
const pageDoc = qualifiedDocument(
|
|
1504
|
+
await readPageDocument(route.sourcePath, formatRegistry),
|
|
1505
|
+
route.sourcePath,
|
|
1506
|
+
projectRoot,
|
|
1507
|
+
);
|
|
1415
1508
|
|
|
1416
1509
|
// Resolve layout (wraps page in layout with slot distribution)
|
|
1417
1510
|
const layoutDoc = await resolveLayout(pageDoc, projectConfig, projectRoot);
|
|
@@ -1422,11 +1515,10 @@ async function compilePage(
|
|
|
1422
1515
|
* `injectContext`: with no layout `resolveLayout` hands back `pageDoc` itself, and injection
|
|
1423
1516
|
* then adds project.json's state to it, which `buildSite` collects once, as project.json's own.
|
|
1424
1517
|
*/
|
|
1518
|
+
const layoutLevels = layoutChain(pageDoc, projectConfig, projectRoot);
|
|
1425
1519
|
const serverEntries = [
|
|
1426
1520
|
...documentServerEntries(pageDoc, route.sourcePath, projectRoot),
|
|
1427
|
-
...
|
|
1428
|
-
documentServerEntries(level.doc, level.path, projectRoot),
|
|
1429
|
-
),
|
|
1521
|
+
...layoutLevels.flatMap((level) => documentServerEntries(level.doc, level.path, projectRoot)),
|
|
1430
1522
|
];
|
|
1431
1523
|
|
|
1432
1524
|
// Extract head arrays before they get lost in the merge
|
|
@@ -1446,7 +1538,13 @@ async function compilePage(
|
|
|
1446
1538
|
delete layoutDoc._pageTitle;
|
|
1447
1539
|
|
|
1448
1540
|
// Inject $site and $page context
|
|
1541
|
+
const declaredStateKeys = new Set(Object.keys(layoutDoc.state ?? {}));
|
|
1449
1542
|
injectContext(layoutDoc, projectConfig, route, projectRoot, locale.i18n, locale.translations);
|
|
1543
|
+
/* The project `state` entries this page did not declare, nor its layout: the ones the injection
|
|
1544
|
+
just copied in, which the page keeps only while something on it reads them (compiler.md §8.1). */
|
|
1545
|
+
const inheritedStateKeys = Object.keys(projectConfig.state ?? {}).filter(
|
|
1546
|
+
(key) => key !== "$site" && key !== "$page" && !declaredStateKeys.has(key),
|
|
1547
|
+
);
|
|
1450
1548
|
|
|
1451
1549
|
/*
|
|
1452
1550
|
* The page's language and writing direction. `$lang` on the document wins over the locale its
|
|
@@ -1464,6 +1562,9 @@ async function compilePage(
|
|
|
1464
1562
|
* A page of a paged list gets its slice in place of the entry it pages. The entry was resolved
|
|
1465
1563
|
* once while the route was expanded, so it is not queried again for every page.
|
|
1466
1564
|
*/
|
|
1565
|
+
/* The state as authored, read after resolution to tell which entries the build itself resolved
|
|
1566
|
+
(see `buildResolvedStateKeys`). A shallow copy: only the identity of each entry is compared. */
|
|
1567
|
+
const authoredState = { ...layoutDoc.state };
|
|
1467
1568
|
if (route.pagedState && layoutDoc.state && route.pagedState.key in layoutDoc.state) {
|
|
1468
1569
|
layoutDoc.state[route.pagedState.key] = route.pagedState.items;
|
|
1469
1570
|
}
|
|
@@ -1478,6 +1579,12 @@ async function compilePage(
|
|
|
1478
1579
|
sections,
|
|
1479
1580
|
});
|
|
1480
1581
|
|
|
1582
|
+
const buildResolved = buildResolvedStateKeys(
|
|
1583
|
+
authoredState,
|
|
1584
|
+
layoutDoc.state ?? {},
|
|
1585
|
+
route.pagedState?.key,
|
|
1586
|
+
);
|
|
1587
|
+
|
|
1481
1588
|
// Build scope from resolved state so template strings in title/$head can be evaluated
|
|
1482
1589
|
const scope = buildInitialScope(layoutDoc.state ?? {});
|
|
1483
1590
|
|
|
@@ -1513,7 +1620,7 @@ async function compilePage(
|
|
|
1513
1620
|
|
|
1514
1621
|
// Resolve template strings in the document tree (innerHTML, textContent, style, attributes)
|
|
1515
1622
|
// So that timing: "compiler" data is baked into the static HTML
|
|
1516
|
-
resolveDocTemplates(layoutDoc, scope);
|
|
1623
|
+
resolveDocTemplates(layoutDoc, scope, buildResolved);
|
|
1517
1624
|
|
|
1518
1625
|
// Expand registered custom elements (apply $props, pre-render, mark static/prerendered).
|
|
1519
1626
|
// Slot children are serialized to HTML strings during expansion — before compileStyles can walk
|
|
@@ -1536,8 +1643,14 @@ async function compilePage(
|
|
|
1536
1643
|
// Expansion is one consumer, not the only one: the same array is routinely also read by a
|
|
1537
1644
|
// Computed at runtime, and dropping it there left `state.rows` undefined in the browser while
|
|
1538
1645
|
// The build reported success (issue #122). `timing: "compiler"` is excluded from this rescue —
|
|
1539
|
-
// That is the author declaring the entry build-time-only, so it is stripped as before.
|
|
1540
|
-
|
|
1646
|
+
// That is the author declaring the entry build-time-only, so it is stripped as before. An
|
|
1647
|
+
// Inherited project entry is only droppable on the same terms (compiler.md §8.1).
|
|
1648
|
+
// A pure named formula is droppable on those terms too, wherever it is declared: every call
|
|
1649
|
+
// The build resolved is gone from the tree, so a formula nothing left for the client reads is
|
|
1650
|
+
// Dead, and keeping it would make a page with one date tag a client page (compiler.md §3).
|
|
1651
|
+
const droppable = new Set<string>();
|
|
1652
|
+
/** Entries a `$src` module may read unseen: the inherited ones, and every pure formula. */
|
|
1653
|
+
const unseenReads = new Set<string>(inheritedStateKeys);
|
|
1541
1654
|
for (const [key, def] of Object.entries(layoutDoc.state)) {
|
|
1542
1655
|
if (key === "$site" || key === "$page") {
|
|
1543
1656
|
continue;
|
|
@@ -1545,26 +1658,55 @@ async function compilePage(
|
|
|
1545
1658
|
if (isJsonObject(def) && def.timing === "compiler") {
|
|
1546
1659
|
delete layoutDoc.state[key];
|
|
1547
1660
|
} else if (Array.isArray(def)) {
|
|
1548
|
-
|
|
1661
|
+
droppable.add(key);
|
|
1662
|
+
} else if (isPureNamedFormula(def)) {
|
|
1663
|
+
droppable.add(key);
|
|
1664
|
+
unseenReads.add(key);
|
|
1549
1665
|
}
|
|
1550
1666
|
}
|
|
1551
|
-
|
|
1667
|
+
for (const key of inheritedStateKeys) {
|
|
1668
|
+
if (key in layoutDoc.state) {
|
|
1669
|
+
droppable.add(key);
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
// Iterated to a fixpoint: rescuing one entry can reveal a read of another from its own def.
|
|
1552
1673
|
let rescued = true;
|
|
1553
1674
|
while (rescued) {
|
|
1554
1675
|
rescued = false;
|
|
1555
1676
|
const surviving = { ...layoutDoc.state };
|
|
1556
|
-
|
|
1677
|
+
/* `$site` holds project definitions as written and never runs, so a handler body there is no
|
|
1678
|
+
read; scanning it would keep every entry a project handler names alive on every page. */
|
|
1679
|
+
delete surviving.$site;
|
|
1680
|
+
delete surviving.$page;
|
|
1681
|
+
for (const key of droppable) {
|
|
1557
1682
|
delete surviving[key];
|
|
1558
1683
|
}
|
|
1559
|
-
|
|
1560
|
-
|
|
1684
|
+
/* The whole document, because a root-level `attributes`, `style` or `$switch` read is a read;
|
|
1685
|
+
minus `$head` and `title`, which are resolved into `<head>` and never reach the island, and
|
|
1686
|
+
`$paths`, which route discovery consumed before this page existed: its `paginate` pointer
|
|
1687
|
+
names the paged entry, and read as a reference it kept every page of a paged list dynamic. */
|
|
1688
|
+
const { $head: _head, $paths: _paths, title: _title, ...body } = layoutDoc;
|
|
1689
|
+
const probe: JxElement = { ...body, state: surviving };
|
|
1690
|
+
/* A `$src` module's reads are invisible to the scan below, so while one survives every
|
|
1691
|
+
inherited entry stays, and every pure formula with it. Judged on the surviving state: a
|
|
1692
|
+
project-level `$src` Function is itself inherited, and must not keep everything alive on
|
|
1693
|
+
pages that never use it. */
|
|
1694
|
+
if (collectSrcImports(probe).length > 0) {
|
|
1695
|
+
for (const key of unseenReads) {
|
|
1696
|
+
if (droppable.delete(key)) {
|
|
1697
|
+
rescued = true;
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
const haystack = JSON.stringify(probe);
|
|
1702
|
+
for (const key of droppable) {
|
|
1561
1703
|
if (referencesStateKey(haystack, key)) {
|
|
1562
|
-
|
|
1704
|
+
droppable.delete(key);
|
|
1563
1705
|
rescued = true;
|
|
1564
1706
|
}
|
|
1565
1707
|
}
|
|
1566
1708
|
}
|
|
1567
|
-
for (const key of
|
|
1709
|
+
for (const key of droppable) {
|
|
1568
1710
|
delete layoutDoc.state[key];
|
|
1569
1711
|
}
|
|
1570
1712
|
}
|
|
@@ -1702,9 +1844,12 @@ async function compilePage(
|
|
|
1702
1844
|
typeof e === "string" && !e.startsWith("./") && !e.startsWith("../");
|
|
1703
1845
|
const npmElements = [
|
|
1704
1846
|
...new Set(
|
|
1705
|
-
[
|
|
1706
|
-
|
|
1707
|
-
|
|
1847
|
+
[
|
|
1848
|
+
...(layoutDoc.$elements ?? []),
|
|
1849
|
+
// Every layout in the chain: resolution keeps only the outermost layout's entries.
|
|
1850
|
+
...layoutLevels.flatMap((level) => level.doc.$elements ?? []),
|
|
1851
|
+
...(pageDoc.$elements ?? []),
|
|
1852
|
+
].filter((e) => isNpmElementEntry(e)),
|
|
1708
1853
|
),
|
|
1709
1854
|
];
|
|
1710
1855
|
if (npmElements.length > 0) {
|
|
@@ -2108,8 +2253,13 @@ function evaluateMapTemplate(str: string, scope: JxScope): JxScope[string] {
|
|
|
2108
2253
|
*
|
|
2109
2254
|
* @param {JxElement | string} node
|
|
2110
2255
|
* @param {JxScope} scope
|
|
2256
|
+
* @param {ReadonlySet<string>} buildResolved - See {@link buildResolvedStateKeys}
|
|
2111
2257
|
*/
|
|
2112
|
-
function resolveDocTemplates(
|
|
2258
|
+
function resolveDocTemplates(
|
|
2259
|
+
node: JxElement | string,
|
|
2260
|
+
scope: JxScope,
|
|
2261
|
+
buildResolved: ReadonlySet<string>,
|
|
2262
|
+
) {
|
|
2113
2263
|
// Children are authored JSON, so a member reached by recursion may be neither node nor string.
|
|
2114
2264
|
if (!isNonNullObject(node)) {
|
|
2115
2265
|
return;
|
|
@@ -2178,14 +2328,14 @@ function resolveDocTemplates(node: JxElement | string, scope: JxScope) {
|
|
|
2178
2328
|
}
|
|
2179
2329
|
const rawChildren = node.children;
|
|
2180
2330
|
// Legacy whole-children repeater: expand the items into the node's static children.
|
|
2181
|
-
//
|
|
2182
|
-
//
|
|
2183
|
-
//
|
|
2184
|
-
//
|
|
2185
|
-
//
|
|
2331
|
+
// `expandMappedArrayStatic` answers `null` for a list the client must keep: one whose `items`
|
|
2332
|
+
// Cannot be resolved at build time, and one that resolves to an EMPTY array out of client state.
|
|
2333
|
+
// Replacing the second with its empty expansion would discard the definition the client needs,
|
|
2334
|
+
// And the list could never populate. An empty array comes back only for a list the build itself
|
|
2335
|
+
// Resolved, which renders nothing and leaves nothing for the client (compiler.md §8.1).
|
|
2186
2336
|
if (isMappedArray(rawChildren)) {
|
|
2187
|
-
const expanded = expandMappedArrayStatic(rawChildren, scope);
|
|
2188
|
-
if (expanded
|
|
2337
|
+
const expanded = expandMappedArrayStatic(rawChildren, scope, buildResolved);
|
|
2338
|
+
if (expanded) {
|
|
2189
2339
|
node.children = expanded;
|
|
2190
2340
|
return;
|
|
2191
2341
|
}
|
|
@@ -2198,7 +2348,7 @@ function resolveDocTemplates(node: JxElement | string, scope: JxScope) {
|
|
|
2198
2348
|
const resolvedNodes = resolved as (JxElement | string)[];
|
|
2199
2349
|
node.children = resolvedNodes;
|
|
2200
2350
|
for (const child of resolvedNodes) {
|
|
2201
|
-
resolveDocTemplates(child, scope);
|
|
2351
|
+
resolveDocTemplates(child, scope, buildResolved);
|
|
2202
2352
|
}
|
|
2203
2353
|
}
|
|
2204
2354
|
} else if (Array.isArray(node.children)) {
|
|
@@ -2213,52 +2363,111 @@ function resolveDocTemplates(node: JxElement | string, scope: JxScope) {
|
|
|
2213
2363
|
const resolvedNodes = resolved as (JxElement | string)[];
|
|
2214
2364
|
node.children.splice(i, 1, ...resolvedNodes);
|
|
2215
2365
|
for (const spliced of resolvedNodes) {
|
|
2216
|
-
resolveDocTemplates(spliced, scope);
|
|
2366
|
+
resolveDocTemplates(spliced, scope, buildResolved);
|
|
2217
2367
|
}
|
|
2218
2368
|
i += resolvedNodes.length;
|
|
2219
2369
|
continue;
|
|
2220
2370
|
}
|
|
2221
2371
|
}
|
|
2222
|
-
// Array pseudo-element among siblings: expand its items in place, on the same
|
|
2223
|
-
//
|
|
2372
|
+
// Array pseudo-element among siblings: expand its items in place, on the same rule as the
|
|
2373
|
+
// Whole-children branch above. An empty expansion removes the member and nothing else.
|
|
2224
2374
|
if (isMappedArray(child)) {
|
|
2225
|
-
const expanded = expandMappedArrayStatic(child, scope);
|
|
2226
|
-
if (expanded
|
|
2375
|
+
const expanded = expandMappedArrayStatic(child, scope, buildResolved);
|
|
2376
|
+
if (expanded) {
|
|
2227
2377
|
node.children.splice(i, 1, ...expanded);
|
|
2228
2378
|
i += expanded.length;
|
|
2229
2379
|
continue;
|
|
2230
2380
|
}
|
|
2231
2381
|
}
|
|
2232
|
-
resolveDocTemplates(child, scope);
|
|
2382
|
+
resolveDocTemplates(child, scope, buildResolved);
|
|
2233
2383
|
i += 1;
|
|
2234
2384
|
}
|
|
2235
2385
|
}
|
|
2236
2386
|
}
|
|
2237
2387
|
|
|
2238
2388
|
/**
|
|
2239
|
-
*
|
|
2240
|
-
*
|
|
2389
|
+
* The top-level state keys whose value the BUILD produced, so nothing in the browser fills them in
|
|
2390
|
+
* later: an entry resolved through its class (`timing: "compiler"`, or no `timing` and a class
|
|
2391
|
+
* mapping, which is how a content collection is written), the entry a paged route replaced with its
|
|
2392
|
+
* slice, and the injected `$site` and `$page` context.
|
|
2241
2393
|
*
|
|
2242
|
-
*
|
|
2243
|
-
*
|
|
2394
|
+
* An authored value is not in the set, whatever its shape: `{ "default": [] }` and a bare `[]` are
|
|
2395
|
+
* client state that starts empty, and a list over one is the client's to populate. A key in the set
|
|
2396
|
+
* that a handler writes is excluded where the set is read ({@link isBuildResolvedListSource}).
|
|
2397
|
+
*
|
|
2398
|
+
* @param {Record<string, JxStateDefinition>} authored - The state before `resolvePrototypes` ran
|
|
2399
|
+
* @param {Record<string, JxStateDefinition>} resolved - The same state afterwards
|
|
2400
|
+
* @param {string} [pagedKey] - The entry a paged route replaced with its slice
|
|
2401
|
+
* @returns {Set<string>}
|
|
2402
|
+
*/
|
|
2403
|
+
function buildResolvedStateKeys(
|
|
2404
|
+
authored: Record<string, JxStateDefinition>,
|
|
2405
|
+
resolved: Record<string, JxStateDefinition>,
|
|
2406
|
+
pagedKey?: string,
|
|
2407
|
+
): Set<string> {
|
|
2408
|
+
const keys = new Set<string>(["$site", "$page"]);
|
|
2409
|
+
if (pagedKey !== undefined) {
|
|
2410
|
+
keys.add(pagedKey);
|
|
2411
|
+
}
|
|
2412
|
+
for (const [key, def] of Object.entries(authored)) {
|
|
2413
|
+
// `resolvePrototypes` replaces an entry it resolved and leaves one it could not resolve as the
|
|
2414
|
+
// Same object, so identity says which happened. A client- or server-timed entry it LOWERED is
|
|
2415
|
+
// Replaced too, by a definition the browser runs, and is not build-resolved.
|
|
2416
|
+
if (
|
|
2417
|
+
isPrototypeDef(def) &&
|
|
2418
|
+
(def.timing === undefined || def.timing === "compiler") &&
|
|
2419
|
+
resolved[key] !== def
|
|
2420
|
+
) {
|
|
2421
|
+
keys.add(key);
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
return keys;
|
|
2425
|
+
}
|
|
2426
|
+
|
|
2427
|
+
/**
|
|
2428
|
+
* Whether a mapped array's `items` is a source the build fully resolved, so an empty result is the
|
|
2429
|
+
* list's final answer: a literal, a `$map/` pointer (a row exists only while the build expands it),
|
|
2430
|
+
* or a `#/state/` pointer into a build-resolved entry no handler writes.
|
|
2431
|
+
*
|
|
2432
|
+
* @param {JxMappedArray["items"]} itemsSrc
|
|
2433
|
+
* @param {JxScope} scope
|
|
2434
|
+
* @param {ReadonlySet<string>} buildResolved - See {@link buildResolvedStateKeys}
|
|
2244
2435
|
* @returns {boolean}
|
|
2245
2436
|
*/
|
|
2246
|
-
function
|
|
2247
|
-
|
|
2248
|
-
|
|
2437
|
+
function isBuildResolvedListSource(
|
|
2438
|
+
itemsSrc: JxMappedArray["items"],
|
|
2439
|
+
scope: JxScope,
|
|
2440
|
+
buildResolved: ReadonlySet<string>,
|
|
2441
|
+
): boolean {
|
|
2442
|
+
if (!isRef(itemsSrc)) {
|
|
2443
|
+
return true;
|
|
2444
|
+
}
|
|
2445
|
+
const ref = itemsSrc.$ref;
|
|
2446
|
+
if (ref.startsWith("$map/")) {
|
|
2447
|
+
return true;
|
|
2448
|
+
}
|
|
2449
|
+
// The first reference token, unescaped as RFC 6901 §4 orders it (`~1` before `~0`). A pointer in
|
|
2450
|
+
// Any other scheme names nothing the build is known to have resolved.
|
|
2451
|
+
const [token = ""] = ref.slice("#/state/".length).split("/");
|
|
2452
|
+
const key = token.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
2453
|
+
return ref.startsWith("#/state/") && buildResolved.has(key) && !isRuntimeOnlyStateKey(scope, key);
|
|
2249
2454
|
}
|
|
2250
2455
|
|
|
2251
2456
|
/**
|
|
2252
|
-
* Statically expand a mapped array to its resolved item nodes when
|
|
2253
|
-
*
|
|
2457
|
+
* Statically expand a mapped array to its resolved item nodes, or null when the array node is left
|
|
2458
|
+
* for client-side rendering: `items` does not resolve to an array at build time, or it resolves to
|
|
2459
|
+
* an empty one out of client state, which the client may still fill. An empty array is returned
|
|
2460
|
+
* only for a source the build fully resolved ({@link isBuildResolvedListSource}).
|
|
2254
2461
|
*
|
|
2255
2462
|
* @param {JxMappedArray} arrayDef
|
|
2256
2463
|
* @param {JxScope} scope
|
|
2464
|
+
* @param {ReadonlySet<string>} buildResolved - See {@link buildResolvedStateKeys}
|
|
2257
2465
|
* @returns {(JxElement | string)[] | null}
|
|
2258
2466
|
*/
|
|
2259
2467
|
function expandMappedArrayStatic(
|
|
2260
2468
|
arrayDef: JxMappedArray,
|
|
2261
2469
|
scope: JxScope,
|
|
2470
|
+
buildResolved: ReadonlySet<string>,
|
|
2262
2471
|
): (JxElement | string)[] | null {
|
|
2263
2472
|
const itemsSrc = arrayDef.items;
|
|
2264
2473
|
// A non-array, non-ref `items` falls through to the array check below and expands to nothing.
|
|
@@ -2267,6 +2476,9 @@ function expandMappedArrayStatic(
|
|
|
2267
2476
|
if (!Array.isArray(items) || !mapTemplate) {
|
|
2268
2477
|
return null;
|
|
2269
2478
|
}
|
|
2479
|
+
if (items.length === 0 && !isBuildResolvedListSource(itemsSrc, scope, buildResolved)) {
|
|
2480
|
+
return null;
|
|
2481
|
+
}
|
|
2270
2482
|
const rows: readonly JxScope[string][] = items;
|
|
2271
2483
|
return rows.map((item, index) => {
|
|
2272
2484
|
/* SAFETY: `Object.create(scope)` is a new object with no own keys whose prototype is `scope`,
|
|
@@ -2276,7 +2488,7 @@ function expandMappedArrayStatic(
|
|
|
2276
2488
|
childScope["$map/item"] = item;
|
|
2277
2489
|
childScope["$map/index"] = index;
|
|
2278
2490
|
const expanded = expandMapTemplate(mapTemplate, childScope);
|
|
2279
|
-
resolveDocTemplates(expanded, childScope);
|
|
2491
|
+
resolveDocTemplates(expanded, childScope, buildResolved);
|
|
2280
2492
|
return expanded;
|
|
2281
2493
|
});
|
|
2282
2494
|
}
|