@jxsuite/compiler 8.7.0 → 9.1.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/README.md +6 -6
- package/dist/cli.js +253 -291
- package/dist/compiler.js +134 -138
- package/package.json +21 -11
- package/src/cli.ts +13 -3
- package/src/compiler.ts +25 -12
- package/src/shared.ts +560 -118
- package/src/site/bundler-backend.bun.ts +36 -2
- package/src/site/bundler-backend.node.ts +42 -2
- package/src/site/bundler-backend.ts +12 -0
- package/src/site/bundler.ts +40 -4
- package/src/site/external-class.ts +290 -0
- package/src/site/format-host.ts +49 -10
- package/src/site/headers-emitter.ts +7 -6
- package/src/site/i18n.ts +1 -1
- package/src/site/image-cache.ts +34 -3
- package/src/site/image-optimizer.ts +123 -162
- package/src/site/image-transform.ts +133 -66
- package/src/site/pages-discovery.ts +10 -3
- package/src/site/prototype-resolver.ts +91 -195
- package/src/site/service-worker.ts +5 -4
- package/src/site/site-build.ts +234 -16
- package/src/site/site-loader.ts +11 -2
- package/src/targets/compile-class.ts +42 -3
- package/src/targets/compile-client.ts +177 -55
- package/src/targets/compile-element.ts +308 -46
- package/src/targets/compile-static.ts +21 -83
package/src/site/site-build.ts
CHANGED
|
@@ -12,20 +12,25 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import {
|
|
15
|
+
copyFileSync,
|
|
15
16
|
cpSync,
|
|
16
17
|
existsSync,
|
|
17
18
|
mkdirSync,
|
|
19
|
+
mkdtempSync,
|
|
18
20
|
readFileSync,
|
|
19
21
|
rmSync,
|
|
20
22
|
statSync,
|
|
21
23
|
writeFileSync,
|
|
22
24
|
} from "node:fs";
|
|
23
25
|
import { createHash } from "node:crypto";
|
|
26
|
+
import { tmpdir } from "node:os";
|
|
24
27
|
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
25
28
|
import {
|
|
29
|
+
isBoolean,
|
|
26
30
|
isJsonObject,
|
|
27
31
|
isMappedArray,
|
|
28
32
|
isNonNullObject,
|
|
33
|
+
isNumber,
|
|
29
34
|
isPrototypeDef,
|
|
30
35
|
isRef,
|
|
31
36
|
isString,
|
|
@@ -43,6 +48,7 @@ import {
|
|
|
43
48
|
import { rewriteHtmlBase } from "./base-path.ts";
|
|
44
49
|
import {
|
|
45
50
|
CLIENT_EXTERNALS,
|
|
51
|
+
bundleEntries,
|
|
46
52
|
bundleEntry,
|
|
47
53
|
bundleWorkerSource,
|
|
48
54
|
isBundleableSrc,
|
|
@@ -109,6 +115,7 @@ import {
|
|
|
109
115
|
isRuntimeOnlyStateKey,
|
|
110
116
|
isSingleExpression,
|
|
111
117
|
liftPropsAttributes,
|
|
118
|
+
observedAttributeProps,
|
|
112
119
|
nextSlotId,
|
|
113
120
|
preRenderComponentHtml,
|
|
114
121
|
pureSchemeOf,
|
|
@@ -120,6 +127,7 @@ import {
|
|
|
120
127
|
import type { ComponentPrerenderContext, StaticChild } from "../shared.ts";
|
|
121
128
|
import { SLOT_ATTR } from "@jxsuite/runtime";
|
|
122
129
|
import { resolvePrototypes } from "./prototype-resolver.ts";
|
|
130
|
+
import { classEntrySource, locateClass, readsBuildContext } from "./external-class.ts";
|
|
123
131
|
import { transformImageNodes } from "./image-transform.ts";
|
|
124
132
|
import { collectCspSources, emptyCspSources } from "./csp.ts";
|
|
125
133
|
import { resolveShadowMode } from "../shadow.ts";
|
|
@@ -160,16 +168,18 @@ import {
|
|
|
160
168
|
writeClientRuntime,
|
|
161
169
|
writeRuntimeSubpaths,
|
|
162
170
|
} from "./client-runtime.ts";
|
|
163
|
-
import {
|
|
171
|
+
import { loadCache, saveCache, usedVariantPaths } from "./image-cache.ts";
|
|
164
172
|
import type { ImageConfig } from "./image-optimizer.ts";
|
|
165
173
|
import type { ImageMetaCache } from "./image-transform.ts";
|
|
166
174
|
import type {
|
|
167
175
|
JsonObject,
|
|
168
176
|
JsonValue,
|
|
169
177
|
JxAttributeValue,
|
|
178
|
+
JxDocument,
|
|
170
179
|
JxElement,
|
|
171
180
|
JxHeadEntry,
|
|
172
181
|
JxMappedArray,
|
|
182
|
+
JxPrototypeDef,
|
|
173
183
|
JxStateDefinition,
|
|
174
184
|
JxStyle,
|
|
175
185
|
ProjectConfig,
|
|
@@ -198,6 +208,23 @@ declare module "@jxsuite/schema/format-registry" {
|
|
|
198
208
|
const ADAPTER_REMEDY =
|
|
199
209
|
'set build.adapter to "cloudflare-workers", "cloudflare-pages", "node", or "bun".';
|
|
200
210
|
|
|
211
|
+
/**
|
|
212
|
+
* The phases a build's time is divided into, in the order they run. Each is the wall-clock time
|
|
213
|
+
* between two points in `buildSite`, so together they account for all of `total`:
|
|
214
|
+
*
|
|
215
|
+
* - `setup`: reading the project file, the extension registry, document scripts and server mounts.
|
|
216
|
+
* - `content`: discovering pages, loading extension project sections (collections), asset mounts and
|
|
217
|
+
* expanding dynamic routes. This is extension code, not the compiler's own.
|
|
218
|
+
* - `components`: compiling the site's components to modules and style sheets.
|
|
219
|
+
* - `routes`: compiling every route, including optimizing the images each one uses.
|
|
220
|
+
* - `bundles`: the image cache, the server worker, client bundles and extension-emitted assets.
|
|
221
|
+
* - `outputs`: redirects, sitemap, copied assets, the manifest, the service worker and headers.
|
|
222
|
+
*/
|
|
223
|
+
export type BuildPhase = "setup" | "content" | "components" | "routes" | "bundles" | "outputs";
|
|
224
|
+
|
|
225
|
+
/** Milliseconds per phase, and for the whole build. Measured, never estimated. */
|
|
226
|
+
export type BuildTimings = Record<BuildPhase | "total", number>;
|
|
227
|
+
|
|
201
228
|
/**
|
|
202
229
|
* Build an entire Jx site from a project directory.
|
|
203
230
|
*
|
|
@@ -207,7 +234,10 @@ const ADAPTER_REMEDY =
|
|
|
207
234
|
* @param {boolean} [options.verbose] - Log progress
|
|
208
235
|
* @param {boolean} [options.includeHeld] - Keep entries a content type's `hold` rules match, for a
|
|
209
236
|
* preview. Left off, a build leaves them out (site-architecture.md §7.6).
|
|
210
|
-
* @
|
|
237
|
+
* @param {boolean} [options.optimizeImages] - `false` builds as if the project said
|
|
238
|
+
* `images.optimize: false`, whatever it says: no variant is encoded and every `<img>` keeps its
|
|
239
|
+
* source. For a build nobody measures, such as a preview deploy (site-architecture.md §9.2.1).
|
|
240
|
+
* @returns {Promise<{ routes: number; files: number; errors: string[]; timings: BuildTimings }>}
|
|
211
241
|
*/
|
|
212
242
|
export async function buildSite(
|
|
213
243
|
projectRoot: string,
|
|
@@ -215,15 +245,37 @@ export async function buildSite(
|
|
|
215
245
|
clean?: boolean;
|
|
216
246
|
verbose?: boolean;
|
|
217
247
|
includeHeld?: boolean;
|
|
248
|
+
optimizeImages?: boolean;
|
|
218
249
|
} = {},
|
|
219
250
|
) {
|
|
220
|
-
const { clean = true, includeHeld = false, verbose = false } = options;
|
|
251
|
+
const { clean = true, includeHeld = false, optimizeImages = true, verbose = false } = options;
|
|
221
252
|
const errors: string[] = [];
|
|
222
253
|
const log = verbose ? console.log.bind(console) : () => {};
|
|
254
|
+
const timings: BuildTimings = {
|
|
255
|
+
bundles: 0,
|
|
256
|
+
components: 0,
|
|
257
|
+
content: 0,
|
|
258
|
+
outputs: 0,
|
|
259
|
+
routes: 0,
|
|
260
|
+
setup: 0,
|
|
261
|
+
total: 0,
|
|
262
|
+
};
|
|
263
|
+
const started = performance.now();
|
|
264
|
+
let lap = started;
|
|
265
|
+
/** Close the phase that just ran: everything since the last one closed is its time. */
|
|
266
|
+
const closePhase = (name: BuildPhase) => {
|
|
267
|
+
const now = performance.now();
|
|
268
|
+
timings[name] = now - lap;
|
|
269
|
+
lap = now;
|
|
270
|
+
};
|
|
223
271
|
|
|
224
272
|
// ── 1. Load project configuration ──────────────────────────────────────────
|
|
225
273
|
log("Loading project.json...");
|
|
226
274
|
const { config: projectConfig } = loadProjectConfig(projectRoot);
|
|
275
|
+
if (!optimizeImages && projectConfig.images.optimize) {
|
|
276
|
+
projectConfig.images = { ...projectConfig.images, optimize: false };
|
|
277
|
+
console.log("Image optimization is off for this build: images are published as they are.");
|
|
278
|
+
}
|
|
227
279
|
|
|
228
280
|
// The legacy imports-based content model is gone (specs/extensions.md v2) — fail loudly with
|
|
229
281
|
// The migration path instead of silently ignoring the section.
|
|
@@ -420,6 +472,37 @@ export async function buildSite(
|
|
|
420
472
|
|
|
421
473
|
// ── 1b. Build the extension registry from project.json#/extensions ─────
|
|
422
474
|
const registry = await buildProjectExtensionRegistry(projectRoot, projectConfig);
|
|
475
|
+
/*
|
|
476
|
+
* Class modules: each `timing: "client"` external class a page or component names is delivered
|
|
477
|
+
* as an entry module that default-exports the class, keyed by its `/assets/class-<Name>.js` URL
|
|
478
|
+
* and bundled in ONE call after every document has compiled (compiler.md §12). One call, because
|
|
479
|
+
* two classes that import the same module have to meet the same instance of it in the browser.
|
|
480
|
+
*/
|
|
481
|
+
const classEntries = new Map<string, string>();
|
|
482
|
+
const deliverClass = (
|
|
483
|
+
def: JxPrototypeDef,
|
|
484
|
+
docDir: string | null,
|
|
485
|
+
imports: Record<string, string>,
|
|
486
|
+
): string => {
|
|
487
|
+
const located = locateClass(def, { docDir, imports, projectRoot, registry });
|
|
488
|
+
if (located === undefined) {
|
|
489
|
+
throw new Error(
|
|
490
|
+
`external class "${def.$prototype}" names no class: give it a $src or an imports ` +
|
|
491
|
+
`entry, or enable the extension that provides "${def.$prototype}"`,
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
if (readsBuildContext(located.classDef)) {
|
|
495
|
+
throw new Error(
|
|
496
|
+
`external class "${def.$prototype}" reads the build's context (#/$context/) and cannot ` +
|
|
497
|
+
`run in a browser: use "timing": "compiler"`,
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
const source = classEntrySource(located);
|
|
501
|
+
const assetPath = sidecarAssetPath(`class:${located.exportName}`);
|
|
502
|
+
claimAsset(assetPath, located.classJsonPath, def.$prototype);
|
|
503
|
+
classEntries.set(assetPath, source);
|
|
504
|
+
return assetPath;
|
|
505
|
+
};
|
|
423
506
|
const formatRegistry = registry.formats;
|
|
424
507
|
// Every document's own modules (spec.md §23.2), read with the formats this project registered.
|
|
425
508
|
const scripts = documentScripts(projectRoot, (path) => readPageDocument(path, formatRegistry));
|
|
@@ -452,6 +535,8 @@ export async function buildSite(
|
|
|
452
535
|
);
|
|
453
536
|
}
|
|
454
537
|
|
|
538
|
+
closePhase("setup");
|
|
539
|
+
|
|
455
540
|
// ── 2. Clean output directory ───────────────────────────────────────────
|
|
456
541
|
if (clean && existsSync(outDir)) {
|
|
457
542
|
rmSync(outDir, { force: true, recursive: true });
|
|
@@ -524,6 +609,8 @@ export async function buildSite(
|
|
|
524
609
|
|
|
525
610
|
let fileCount = 0;
|
|
526
611
|
|
|
612
|
+
closePhase("content");
|
|
613
|
+
|
|
527
614
|
// ── 5. Compile site components ──────────────────────────────────────────
|
|
528
615
|
const compiledComponentTags: string[] = [];
|
|
529
616
|
const componentCSS = new Map<string, string>(); // TagName → CSS text
|
|
@@ -563,13 +650,57 @@ export async function buildSite(
|
|
|
563
650
|
// So it is given the deployment base now (site-architecture.md §14.7).
|
|
564
651
|
const serverBaseUrl = serverRoutePrefix(componentBase);
|
|
565
652
|
|
|
653
|
+
/*
|
|
654
|
+
* A component's build-time external classes (compiler.md §3), resolved once per component file
|
|
655
|
+
* and written as its initial state, so the module compiled from the file and the definition
|
|
656
|
+
* every page prerenders from start from the same value. The file is read more than once (as a
|
|
657
|
+
* site component, as the module's source, as another component's `$elements` dependency), so
|
|
658
|
+
* the entries the first read resolved are kept and assigned onto each later one.
|
|
659
|
+
*/
|
|
660
|
+
const componentResolutions = new Map<string, Promise<Record<string, JxStateDefinition>>>();
|
|
661
|
+
const prepareComponent = async (
|
|
662
|
+
componentDoc: JxDocument | JxElement,
|
|
663
|
+
filePath: string | null,
|
|
664
|
+
): Promise<void> => {
|
|
665
|
+
// Every document this build compiles is a file, so the empty key is never shared in practice.
|
|
666
|
+
const key = filePath ?? "";
|
|
667
|
+
const resolved = componentResolutions.get(key);
|
|
668
|
+
if (resolved === undefined) {
|
|
669
|
+
const resolving = resolvePrototypes(
|
|
670
|
+
componentDoc,
|
|
671
|
+
filePath === null ? {} : { sourcePath: filePath },
|
|
672
|
+
projectRoot,
|
|
673
|
+
{
|
|
674
|
+
asInitialValues: true,
|
|
675
|
+
config: projectConfig,
|
|
676
|
+
registerBundle: (specifier: string) => {
|
|
677
|
+
rewriteSidecarSrc(specifier, null);
|
|
678
|
+
},
|
|
679
|
+
registry,
|
|
680
|
+
sections,
|
|
681
|
+
},
|
|
682
|
+
);
|
|
683
|
+
componentResolutions.set(key, resolving);
|
|
684
|
+
await resolving;
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const entries = await resolved;
|
|
688
|
+
if (componentDoc.state) {
|
|
689
|
+
Object.assign(componentDoc.state, entries);
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
|
|
566
693
|
for (const { doc: written, path: componentPath } of siteComponents) {
|
|
567
694
|
/* The definition every page prerenders from, with the project formulas it reads (spec.md
|
|
568
695
|
§13.4). The module is compiled from the file, and is given the same project state. */
|
|
569
696
|
const doc = inheritProjectFormulas(written, projectConfig.state);
|
|
570
697
|
try {
|
|
698
|
+
await prepareComponent(doc, componentPath);
|
|
571
699
|
const elementOptions: CompileElementOptions = {
|
|
572
700
|
formats: formatRegistry,
|
|
701
|
+
prepareDocument: prepareComponent,
|
|
702
|
+
resolveClass: (def, docDir, imports) =>
|
|
703
|
+
withBase(componentBase, deliverClass(def, docDir, imports)),
|
|
573
704
|
resolveElementPath: (refPath: string, currentDir: string | null) => {
|
|
574
705
|
const depPath = currentDir ? resolve(currentDir, refPath) : resolve(refPath);
|
|
575
706
|
const depTag = componentTagByPath.get(depPath);
|
|
@@ -688,11 +819,20 @@ export async function buildSite(
|
|
|
688
819
|
}
|
|
689
820
|
}
|
|
690
821
|
|
|
822
|
+
closePhase("components");
|
|
823
|
+
|
|
691
824
|
// ── 6. Compile each route ───────────────────────────────────────────────
|
|
692
825
|
|
|
693
826
|
const cfImages = projectConfig.images.optimize && projectConfig.images.service === "cloudflare";
|
|
694
827
|
const imageCache = projectConfig.images.optimize && !cfImages ? loadCache(projectRoot) : null;
|
|
695
828
|
const imageMetaCache: ImageMetaCache | null = cfImages ? new Map() : null;
|
|
829
|
+
if (imageCache && projectConfig.images.formats?.includes("webp")) {
|
|
830
|
+
console.warn(
|
|
831
|
+
`images.formats lists "webp", which this compiler writes losslessly: its files are several ` +
|
|
832
|
+
`times the size of AVIF or JPEG. Keep it only for pixel-exact output; ["avif", "jpeg"] ` +
|
|
833
|
+
`is the default.`,
|
|
834
|
+
);
|
|
835
|
+
}
|
|
696
836
|
if (cfImages) {
|
|
697
837
|
console.log(
|
|
698
838
|
`images.service is "cloudflare" — srcsets use /cdn-cgi/image transform URLs. ` +
|
|
@@ -852,6 +992,7 @@ export async function buildSite(
|
|
|
852
992
|
},
|
|
853
993
|
runtimeImports,
|
|
854
994
|
registerElementBundle,
|
|
995
|
+
deliverClass,
|
|
855
996
|
);
|
|
856
997
|
siteServerEntries.push(...result.serverEntries);
|
|
857
998
|
|
|
@@ -983,7 +1124,7 @@ export async function buildSite(
|
|
|
983
1124
|
fileCount += 1;
|
|
984
1125
|
}
|
|
985
1126
|
|
|
986
|
-
// Write any additional files (
|
|
1127
|
+
// Write any additional files (the page's own module, etc.)
|
|
987
1128
|
for (const file of result.files) {
|
|
988
1129
|
const filePath = resolve(dirname(outPath), file.path);
|
|
989
1130
|
mkdirSync(dirname(filePath), { recursive: true });
|
|
@@ -997,16 +1138,22 @@ export async function buildSite(
|
|
|
997
1138
|
}
|
|
998
1139
|
}
|
|
999
1140
|
|
|
1141
|
+
closePhase("routes");
|
|
1142
|
+
|
|
1000
1143
|
// ── 6b. Save image cache and copy variants to dist ──────────────────────
|
|
1001
1144
|
if (imageCache && projectConfig.images.optimize) {
|
|
1002
1145
|
// Prune only on fully successful builds — a route that failed to compile never
|
|
1003
1146
|
// Touched its images, and pruning would evict their still-valid entries.
|
|
1004
1147
|
saveCache(projectRoot, imageCache, { prune: errors.length === 0 });
|
|
1005
|
-
|
|
1006
|
-
|
|
1148
|
+
/* Only the variants of entries this build resolved, never the directory: whatever else the
|
|
1149
|
+
cache holds (another build's leftovers, a stray file) is not part of this site. */
|
|
1150
|
+
const used = usedVariantPaths(imageCache).filter((path) => existsSync(path));
|
|
1151
|
+
if (used.length > 0) {
|
|
1007
1152
|
const distOptimizedDir = resolve(outDir, "images/_optimized");
|
|
1008
1153
|
mkdirSync(distOptimizedDir, { recursive: true });
|
|
1009
|
-
|
|
1154
|
+
for (const path of used) {
|
|
1155
|
+
copyFileSync(path, resolve(distOptimizedDir, basename(path)));
|
|
1156
|
+
}
|
|
1010
1157
|
}
|
|
1011
1158
|
const totalImages = Object.keys(imageCache.entries).length;
|
|
1012
1159
|
if (totalImages > 0) {
|
|
@@ -1188,6 +1335,36 @@ export async function buildSite(
|
|
|
1188
1335
|
}
|
|
1189
1336
|
}
|
|
1190
1337
|
|
|
1338
|
+
if (classEntries.size > 0) {
|
|
1339
|
+
log(`Bundling ${classEntries.size} external class module(s)...`);
|
|
1340
|
+
/* Each entry is written to a temporary directory under its asset's base name, which is the name
|
|
1341
|
+
the bundler gives its output; an entry's imports are absolute paths, so where it stands does
|
|
1342
|
+
not matter. */
|
|
1343
|
+
const entryDir = mkdtempSync(join(tmpdir(), "jx-class-entries-"));
|
|
1344
|
+
try {
|
|
1345
|
+
const entryPaths = [...classEntries].map(([assetPath, source]) => {
|
|
1346
|
+
const entryPath = join(entryDir, basename(assetPath));
|
|
1347
|
+
writeFileSync(entryPath, source, "utf8");
|
|
1348
|
+
return entryPath;
|
|
1349
|
+
});
|
|
1350
|
+
const written = await bundleEntries(entryPaths, resolve(outDir, "assets"), {
|
|
1351
|
+
external: CLIENT_EXTERNALS,
|
|
1352
|
+
minify,
|
|
1353
|
+
target: "browser",
|
|
1354
|
+
});
|
|
1355
|
+
fileCount += written.length;
|
|
1356
|
+
for (const assetPath of classEntries.keys()) {
|
|
1357
|
+
log(` ${assetPath}`);
|
|
1358
|
+
}
|
|
1359
|
+
} catch (error) {
|
|
1360
|
+
const msg = `Error bundling external classes: ${errorMessage(error)}`;
|
|
1361
|
+
errors.push(msg);
|
|
1362
|
+
console.error(msg);
|
|
1363
|
+
} finally {
|
|
1364
|
+
rmSync(entryDir, { force: true, recursive: true });
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1191
1368
|
if (elementBundles.size > 0) {
|
|
1192
1369
|
log(`Bundling ${elementBundles.size} npm element set(s)...`);
|
|
1193
1370
|
for (const [assetPath, specifiers] of elementBundles) {
|
|
@@ -1270,6 +1447,8 @@ export async function buildSite(
|
|
|
1270
1447
|
}
|
|
1271
1448
|
}
|
|
1272
1449
|
|
|
1450
|
+
closePhase("bundles");
|
|
1451
|
+
|
|
1273
1452
|
// ── 7. Generate redirects ───────────────────────────────────────────────
|
|
1274
1453
|
if (projectConfig.redirects && Object.keys(projectConfig.redirects).length > 0) {
|
|
1275
1454
|
log("Generating redirects...");
|
|
@@ -1440,7 +1619,9 @@ export async function buildSite(
|
|
|
1440
1619
|
log(` ${errors.length} error(s)`);
|
|
1441
1620
|
}
|
|
1442
1621
|
|
|
1443
|
-
|
|
1622
|
+
closePhase("outputs");
|
|
1623
|
+
timings.total = performance.now() - started;
|
|
1624
|
+
return { errors, files: fileCount, routes: routes.length, serverEntries, timings };
|
|
1444
1625
|
}
|
|
1445
1626
|
|
|
1446
1627
|
/** The route `compilePage` renders: a URL and the page document that produces it. */
|
|
@@ -1493,8 +1674,14 @@ async function compilePage(
|
|
|
1493
1674
|
runtimeImports: Record<string, string>,
|
|
1494
1675
|
/** Registers a page's npm `$elements` set as one bundle and returns its URL. */
|
|
1495
1676
|
registerElementBundle: (specifiers: string[]) => string,
|
|
1677
|
+
/** Registers a `timing: "client"` external class's module and returns its URL (compiler.md §3). */
|
|
1678
|
+
deliverClass: (
|
|
1679
|
+
def: JxPrototypeDef,
|
|
1680
|
+
docDir: string | null,
|
|
1681
|
+
imports: Record<string, string>,
|
|
1682
|
+
) => string,
|
|
1496
1683
|
) {
|
|
1497
|
-
/* Derived rather than passed as
|
|
1684
|
+
/* Derived rather than passed as one more parameter: it is a pure function of
|
|
1498
1685
|
`projectConfig.url`, which is already here, so the two cannot disagree. */
|
|
1499
1686
|
const basePath = siteBasePath(projectConfig.url);
|
|
1500
1687
|
|
|
@@ -1792,6 +1979,10 @@ async function compilePage(
|
|
|
1792
1979
|
* The page's own module (`app.js`) is JavaScript the page loads, like a component: the page
|
|
1793
1980
|
* rewrite does not read it, so its `/assets/` imports are re-rooted on the site base here.
|
|
1794
1981
|
*/
|
|
1982
|
+
/* A `timing: "client"` external class the page still holds after resolution and lowering: its
|
|
1983
|
+
module is bundled under `/assets/`, and the page's module imports it (compiler.md §3). */
|
|
1984
|
+
resolveClass: (def: JxPrototypeDef) =>
|
|
1985
|
+
withBase(basePath, deliverClass(def, dirname(route.sourcePath), layoutDoc.imports ?? {})),
|
|
1795
1986
|
rewriteSrc: (specifier: string) =>
|
|
1796
1987
|
withBase(basePath, rewriteSidecarSrc(specifier, dirname(route.sourcePath))),
|
|
1797
1988
|
// The page's own module calls the worker's routes, under the same base the worker registers.
|
|
@@ -1800,7 +1991,7 @@ async function compilePage(
|
|
|
1800
1991
|
};
|
|
1801
1992
|
/*
|
|
1802
1993
|
* The page-template tiers emit their own import map, and it defaulted to the CDN — so a page
|
|
1803
|
-
* that reached
|
|
1994
|
+
* that reached the client or custom-element target kept loading its runtime from esm.sh even after
|
|
1804
1995
|
* the self-hosting landed, because `injectComponentScripts` sees a map already present and
|
|
1805
1996
|
* declines to add its own. Both halves have to name the same URLs.
|
|
1806
1997
|
*/
|
|
@@ -2147,7 +2338,20 @@ function inertAttribute(resolved: string): string {
|
|
|
2147
2338
|
}
|
|
2148
2339
|
|
|
2149
2340
|
/**
|
|
2150
|
-
*
|
|
2341
|
+
* The text a resolved template text child stands for, or `null` when the result is not text: a
|
|
2342
|
+
* string, number or boolean is substituted, while `null`, `undefined` and an object keep the
|
|
2343
|
+
* template, as `textContent`'s fallback does (compiler.md §2.2).
|
|
2344
|
+
*
|
|
2345
|
+
* @param {JxScope[string]} value
|
|
2346
|
+
* @returns {string | null}
|
|
2347
|
+
*/
|
|
2348
|
+
function textChildValue(value: JxScope[string]): string | null {
|
|
2349
|
+
return isString(value) || isNumber(value) || isBoolean(value) ? String(value) : null;
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
/**
|
|
2353
|
+
* Deep-clone a map template, resolving template strings and $ref values against the given scope. A
|
|
2354
|
+
* template text child resolves per item like every other position.
|
|
2151
2355
|
*
|
|
2152
2356
|
* @param {JxElement} template
|
|
2153
2357
|
* @param {JxScope} scope
|
|
@@ -2164,7 +2368,9 @@ function expandMapTemplate(template: JxElement, scope: JxScope): JxElement {
|
|
|
2164
2368
|
if (k === "children" && Array.isArray(template.children)) {
|
|
2165
2369
|
node.children = template.children.map((child) => {
|
|
2166
2370
|
if (isString(child)) {
|
|
2167
|
-
return child
|
|
2371
|
+
return isTemplateString(child)
|
|
2372
|
+
? (textChildValue(evaluateMapTemplate(child, scope)) ?? child)
|
|
2373
|
+
: child;
|
|
2168
2374
|
}
|
|
2169
2375
|
return expandMapTemplate(child, scope);
|
|
2170
2376
|
});
|
|
@@ -2249,7 +2455,8 @@ function evaluateMapTemplate(str: string, scope: JxScope): JxScope[string] {
|
|
|
2249
2455
|
|
|
2250
2456
|
/**
|
|
2251
2457
|
* Recursively resolve template strings in a document tree against a scope. Mutates the document in
|
|
2252
|
-
* place — evaluates ${...} in innerHTML, textContent, style values,
|
|
2458
|
+
* place — evaluates ${...} in innerHTML, textContent, style values, attribute values, and text
|
|
2459
|
+
* children (compiler.md §2.2).
|
|
2253
2460
|
*
|
|
2254
2461
|
* @param {JxElement | string} node
|
|
2255
2462
|
* @param {JxScope} scope
|
|
@@ -2368,6 +2575,12 @@ function resolveDocTemplates(
|
|
|
2368
2575
|
i += resolvedNodes.length;
|
|
2369
2576
|
continue;
|
|
2370
2577
|
}
|
|
2578
|
+
const resolvedText = textChildValue(resolved);
|
|
2579
|
+
if (resolvedText !== null) {
|
|
2580
|
+
node.children[i] = resolvedText;
|
|
2581
|
+
i += 1;
|
|
2582
|
+
continue;
|
|
2583
|
+
}
|
|
2371
2584
|
}
|
|
2372
2585
|
// Array pseudo-element among siblings: expand its items in place, on the same rule as the
|
|
2373
2586
|
// Whole-children branch above. An empty expansion removes the member and nothing else.
|
|
@@ -2503,7 +2716,7 @@ interface SlotCssCollector {
|
|
|
2503
2716
|
/**
|
|
2504
2717
|
* Walk the document tree and expand registered custom elements in-place. Applies $props via
|
|
2505
2718
|
* preRenderComponentHtml, marks static/prerendered. Slot children get their static styles collected
|
|
2506
|
-
* into `slotCss` (
|
|
2719
|
+
* into `slotCss` (each styled one given its `jxs-<n>` handle) before being serialized to HTML.
|
|
2507
2720
|
*
|
|
2508
2721
|
* @param {JxElement | string | (JxElement | string)[]} node - A nested array (authored JSON can
|
|
2509
2722
|
* carry one among children) is walked member by member
|
|
@@ -2574,7 +2787,12 @@ function expandComponents(
|
|
|
2574
2787
|
* render them twice.
|
|
2575
2788
|
*/
|
|
2576
2789
|
const shadow = resolveShadowMode(def, defaults);
|
|
2577
|
-
|
|
2790
|
+
/* The instance's observed attributes, coerced, lie beneath its `$props` (spec.md §16.5): they
|
|
2791
|
+
feed the prerender, the host style and the path, and stay out of `data-jx-props`, because the
|
|
2792
|
+
element's module reads the attribute itself. The page walk has resolved the templates, so
|
|
2793
|
+
the attribute values resolve against nothing. */
|
|
2794
|
+
const observedProps = observedAttributeProps(def, node.attributes, {});
|
|
2795
|
+
const props = observedProps ? { ...observedProps, ...node.$props } : node.$props || null;
|
|
2578
2796
|
// This instance is the first frame of the path, so a definition that names itself is reported
|
|
2579
2797
|
// At the first nesting rather than after the stack overflows.
|
|
2580
2798
|
const context: ComponentPrerenderContext = {
|
|
@@ -2639,7 +2857,7 @@ function expandComponents(
|
|
|
2639
2857
|
/*
|
|
2640
2858
|
* A non-static instance discards its prerendered markup on upgrade and re-renders, so the
|
|
2641
2859
|
* props have to survive as data or the element comes back with its state defaults — correct
|
|
2642
|
-
* content painting first and then being replaced by the wrong content. compiler.md §
|
|
2860
|
+
* content painting first and then being replaced by the wrong content. compiler.md §4.4 gives
|
|
2643
2861
|
* connectedCallback three prop sources and this is the first of them; the compiler read it but
|
|
2644
2862
|
* never wrote it, and lifting `props.*` into $props above removed the other channel too.
|
|
2645
2863
|
*
|
package/src/site/site-loader.ts
CHANGED
|
@@ -48,11 +48,11 @@ const DEFAULTS = {
|
|
|
48
48
|
layout: null,
|
|
49
49
|
},
|
|
50
50
|
images: {
|
|
51
|
-
formats: ["
|
|
51
|
+
formats: ["avif", "jpeg"],
|
|
52
52
|
lazyLoad: true,
|
|
53
53
|
optimize: true,
|
|
54
54
|
picture: true,
|
|
55
|
-
quality: { avif: 65, jpeg: 80
|
|
55
|
+
quality: { avif: 65, jpeg: 80 },
|
|
56
56
|
service: "build",
|
|
57
57
|
sizes: "(max-width: 768px) 100vw, 50vw",
|
|
58
58
|
widths: [320, 640, 960, 1280, 1920],
|
|
@@ -148,3 +148,12 @@ export function loadProjectConfig(projectRoot: string) {
|
|
|
148
148
|
projectRoot: resolve(projectRoot),
|
|
149
149
|
};
|
|
150
150
|
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Whether `JX_IMAGES_OPTIMIZE` turns image optimization off for this build. Only a plain "no" does:
|
|
154
|
+
* unset, empty or anything else leaves the project's own `images.optimize` in charge, so the
|
|
155
|
+
* variable can switch a build off and can never switch one on that the project switched off.
|
|
156
|
+
*/
|
|
157
|
+
export function imagesOptimizeOff(value: string | undefined): boolean {
|
|
158
|
+
return ["0", "false", "no", "off"].includes((value ?? "").trim().toLowerCase());
|
|
159
|
+
}
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Compile-class.js — Compile .class.json schema-defined classes to JavaScript ES modules
|
|
3
3
|
*
|
|
4
4
|
* Generates proper ES modules with private fields, static members, accessors, etc. from a JSON
|
|
5
|
-
* Schema 2020-12 class definition.
|
|
5
|
+
* Schema 2020-12 class definition (compiler.md §5). A module imports nothing but a `$ref` base
|
|
6
|
+
* class, by the default export of the sibling module route 0 writes for that document.
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
9
|
import { erasedClass } from "../erased.ts";
|
|
@@ -45,6 +46,12 @@ export function compileClassJson(authored: JxClassDef) {
|
|
|
45
46
|
}
|
|
46
47
|
lines.push("");
|
|
47
48
|
|
|
49
|
+
// A `$ref` base is another class document's module; a named base is a global the host supplies.
|
|
50
|
+
const baseImport = baseClassImport(classDef.extends);
|
|
51
|
+
if (baseImport !== null) {
|
|
52
|
+
lines.push(baseImport, "");
|
|
53
|
+
}
|
|
54
|
+
|
|
48
55
|
// Class declaration
|
|
49
56
|
const extendsClause = baseClass !== "Object" ? ` extends ${baseClass}` : "";
|
|
50
57
|
lines.push(`class ${className}${extendsClause} {`);
|
|
@@ -136,7 +143,8 @@ export function compileClassJson(authored: JxClassDef) {
|
|
|
136
143
|
}
|
|
137
144
|
// Setter
|
|
138
145
|
if (m.setter) {
|
|
139
|
-
|
|
146
|
+
// A setter takes exactly one parameter, so one that declares none is given a name.
|
|
147
|
+
const setterParams = resolveParams(m.setter.parameters ?? []) || "v";
|
|
140
148
|
lines.push(
|
|
141
149
|
` ${staticPrefix}set ${prefix}${name}(${setterParams}) {`,
|
|
142
150
|
` ${m.setter.body}`,
|
|
@@ -182,12 +190,43 @@ function resolveBaseClass(ext: JxClassDef["extends"]) {
|
|
|
182
190
|
return ext;
|
|
183
191
|
}
|
|
184
192
|
if (ext.$ref) {
|
|
185
|
-
const match = ext.$ref.match(
|
|
193
|
+
const match = ext.$ref.match(CLASS_DOCUMENT_REF);
|
|
186
194
|
return match ? match[1] : "Object";
|
|
187
195
|
}
|
|
188
196
|
return "Object";
|
|
189
197
|
}
|
|
190
198
|
|
|
199
|
+
/** A `$ref` to a class document: the file's base name is the local name the class extends. */
|
|
200
|
+
const CLASS_DOCUMENT_REF = /([A-Za-z0-9_]+)\.class\.json/;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The import a `$ref` base class needs, or `null` when `extends` names a global class, an
|
|
204
|
+
* unrecognised reference, or nothing.
|
|
205
|
+
*
|
|
206
|
+
* The specifier is the reference without its fragment, pointed at the module route 0 writes beside
|
|
207
|
+
* that document. The default export is what links the two, so a base whose `title` differs from its
|
|
208
|
+
* file name still resolves; a reference with no leading `./`, `../`, `/` or scheme is relative to
|
|
209
|
+
* the document, which a module specifier has to say.
|
|
210
|
+
*
|
|
211
|
+
* @param {JxClassDef["extends"]} ext
|
|
212
|
+
* @returns {string | null}
|
|
213
|
+
*/
|
|
214
|
+
function baseClassImport(ext: JxClassDef["extends"]): string | null {
|
|
215
|
+
if (!ext || isString(ext) || !ext.$ref) {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
const match = ext.$ref.match(CLASS_DOCUMENT_REF);
|
|
219
|
+
if (!match) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
const [path = ""] = ext.$ref.split("#");
|
|
223
|
+
const modulePath = path.replace(/\.class\.json$/, ".js");
|
|
224
|
+
const specifier = /^(?:\.{0,2}\/|[a-z][a-z0-9+.-]*:)/i.test(modulePath)
|
|
225
|
+
? modulePath
|
|
226
|
+
: `./${modulePath}`;
|
|
227
|
+
return `import ${match[1]} from ${JSON.stringify(specifier)};`;
|
|
228
|
+
}
|
|
229
|
+
|
|
191
230
|
/**
|
|
192
231
|
* Resolve parameter names from $ref or inline definitions.
|
|
193
232
|
*
|