@jxsuite/compiler 9.0.1 → 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/dist/cli.js +204 -205
- package/dist/compiler.js +134 -138
- package/package.json +18 -18
- 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 +6 -5
- package/src/site/image-cache.ts +34 -3
- package/src/site/image-optimizer.ts +51 -11
- package/src/site/image-transform.ts +100 -62
- package/src/site/pages-discovery.ts +8 -1
- package/src/site/prototype-resolver.ts +91 -195
- package/src/site/service-worker.ts +4 -3
- package/src/site/site-build.ts +179 -14
- package/src/site/site-loader.ts +9 -0
- 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,
|
|
@@ -224,6 +234,9 @@ export type BuildTimings = Record<BuildPhase | "total", number>;
|
|
|
224
234
|
* @param {boolean} [options.verbose] - Log progress
|
|
225
235
|
* @param {boolean} [options.includeHeld] - Keep entries a content type's `hold` rules match, for a
|
|
226
236
|
* preview. Left off, a build leaves them out (site-architecture.md §7.6).
|
|
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).
|
|
227
240
|
* @returns {Promise<{ routes: number; files: number; errors: string[]; timings: BuildTimings }>}
|
|
228
241
|
*/
|
|
229
242
|
export async function buildSite(
|
|
@@ -232,9 +245,10 @@ export async function buildSite(
|
|
|
232
245
|
clean?: boolean;
|
|
233
246
|
verbose?: boolean;
|
|
234
247
|
includeHeld?: boolean;
|
|
248
|
+
optimizeImages?: boolean;
|
|
235
249
|
} = {},
|
|
236
250
|
) {
|
|
237
|
-
const { clean = true, includeHeld = false, verbose = false } = options;
|
|
251
|
+
const { clean = true, includeHeld = false, optimizeImages = true, verbose = false } = options;
|
|
238
252
|
const errors: string[] = [];
|
|
239
253
|
const log = verbose ? console.log.bind(console) : () => {};
|
|
240
254
|
const timings: BuildTimings = {
|
|
@@ -258,6 +272,10 @@ export async function buildSite(
|
|
|
258
272
|
// ── 1. Load project configuration ──────────────────────────────────────────
|
|
259
273
|
log("Loading project.json...");
|
|
260
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
|
+
}
|
|
261
279
|
|
|
262
280
|
// The legacy imports-based content model is gone (specs/extensions.md v2) — fail loudly with
|
|
263
281
|
// The migration path instead of silently ignoring the section.
|
|
@@ -454,6 +472,37 @@ export async function buildSite(
|
|
|
454
472
|
|
|
455
473
|
// ── 1b. Build the extension registry from project.json#/extensions ─────
|
|
456
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
|
+
};
|
|
457
506
|
const formatRegistry = registry.formats;
|
|
458
507
|
// Every document's own modules (spec.md §23.2), read with the formats this project registered.
|
|
459
508
|
const scripts = documentScripts(projectRoot, (path) => readPageDocument(path, formatRegistry));
|
|
@@ -601,13 +650,57 @@ export async function buildSite(
|
|
|
601
650
|
// So it is given the deployment base now (site-architecture.md §14.7).
|
|
602
651
|
const serverBaseUrl = serverRoutePrefix(componentBase);
|
|
603
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
|
+
|
|
604
693
|
for (const { doc: written, path: componentPath } of siteComponents) {
|
|
605
694
|
/* The definition every page prerenders from, with the project formulas it reads (spec.md
|
|
606
695
|
§13.4). The module is compiled from the file, and is given the same project state. */
|
|
607
696
|
const doc = inheritProjectFormulas(written, projectConfig.state);
|
|
608
697
|
try {
|
|
698
|
+
await prepareComponent(doc, componentPath);
|
|
609
699
|
const elementOptions: CompileElementOptions = {
|
|
610
700
|
formats: formatRegistry,
|
|
701
|
+
prepareDocument: prepareComponent,
|
|
702
|
+
resolveClass: (def, docDir, imports) =>
|
|
703
|
+
withBase(componentBase, deliverClass(def, docDir, imports)),
|
|
611
704
|
resolveElementPath: (refPath: string, currentDir: string | null) => {
|
|
612
705
|
const depPath = currentDir ? resolve(currentDir, refPath) : resolve(refPath);
|
|
613
706
|
const depTag = componentTagByPath.get(depPath);
|
|
@@ -899,6 +992,7 @@ export async function buildSite(
|
|
|
899
992
|
},
|
|
900
993
|
runtimeImports,
|
|
901
994
|
registerElementBundle,
|
|
995
|
+
deliverClass,
|
|
902
996
|
);
|
|
903
997
|
siteServerEntries.push(...result.serverEntries);
|
|
904
998
|
|
|
@@ -1030,7 +1124,7 @@ export async function buildSite(
|
|
|
1030
1124
|
fileCount += 1;
|
|
1031
1125
|
}
|
|
1032
1126
|
|
|
1033
|
-
// Write any additional files (
|
|
1127
|
+
// Write any additional files (the page's own module, etc.)
|
|
1034
1128
|
for (const file of result.files) {
|
|
1035
1129
|
const filePath = resolve(dirname(outPath), file.path);
|
|
1036
1130
|
mkdirSync(dirname(filePath), { recursive: true });
|
|
@@ -1051,11 +1145,15 @@ export async function buildSite(
|
|
|
1051
1145
|
// Prune only on fully successful builds — a route that failed to compile never
|
|
1052
1146
|
// Touched its images, and pruning would evict their still-valid entries.
|
|
1053
1147
|
saveCache(projectRoot, imageCache, { prune: errors.length === 0 });
|
|
1054
|
-
|
|
1055
|
-
|
|
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) {
|
|
1056
1152
|
const distOptimizedDir = resolve(outDir, "images/_optimized");
|
|
1057
1153
|
mkdirSync(distOptimizedDir, { recursive: true });
|
|
1058
|
-
|
|
1154
|
+
for (const path of used) {
|
|
1155
|
+
copyFileSync(path, resolve(distOptimizedDir, basename(path)));
|
|
1156
|
+
}
|
|
1059
1157
|
}
|
|
1060
1158
|
const totalImages = Object.keys(imageCache.entries).length;
|
|
1061
1159
|
if (totalImages > 0) {
|
|
@@ -1237,6 +1335,36 @@ export async function buildSite(
|
|
|
1237
1335
|
}
|
|
1238
1336
|
}
|
|
1239
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
|
+
|
|
1240
1368
|
if (elementBundles.size > 0) {
|
|
1241
1369
|
log(`Bundling ${elementBundles.size} npm element set(s)...`);
|
|
1242
1370
|
for (const [assetPath, specifiers] of elementBundles) {
|
|
@@ -1546,8 +1674,14 @@ async function compilePage(
|
|
|
1546
1674
|
runtimeImports: Record<string, string>,
|
|
1547
1675
|
/** Registers a page's npm `$elements` set as one bundle and returns its URL. */
|
|
1548
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,
|
|
1549
1683
|
) {
|
|
1550
|
-
/* Derived rather than passed as
|
|
1684
|
+
/* Derived rather than passed as one more parameter: it is a pure function of
|
|
1551
1685
|
`projectConfig.url`, which is already here, so the two cannot disagree. */
|
|
1552
1686
|
const basePath = siteBasePath(projectConfig.url);
|
|
1553
1687
|
|
|
@@ -1845,6 +1979,10 @@ async function compilePage(
|
|
|
1845
1979
|
* The page's own module (`app.js`) is JavaScript the page loads, like a component: the page
|
|
1846
1980
|
* rewrite does not read it, so its `/assets/` imports are re-rooted on the site base here.
|
|
1847
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 ?? {})),
|
|
1848
1986
|
rewriteSrc: (specifier: string) =>
|
|
1849
1987
|
withBase(basePath, rewriteSidecarSrc(specifier, dirname(route.sourcePath))),
|
|
1850
1988
|
// The page's own module calls the worker's routes, under the same base the worker registers.
|
|
@@ -1853,7 +1991,7 @@ async function compilePage(
|
|
|
1853
1991
|
};
|
|
1854
1992
|
/*
|
|
1855
1993
|
* The page-template tiers emit their own import map, and it defaulted to the CDN — so a page
|
|
1856
|
-
* that reached
|
|
1994
|
+
* that reached the client or custom-element target kept loading its runtime from esm.sh even after
|
|
1857
1995
|
* the self-hosting landed, because `injectComponentScripts` sees a map already present and
|
|
1858
1996
|
* declines to add its own. Both halves have to name the same URLs.
|
|
1859
1997
|
*/
|
|
@@ -2200,7 +2338,20 @@ function inertAttribute(resolved: string): string {
|
|
|
2200
2338
|
}
|
|
2201
2339
|
|
|
2202
2340
|
/**
|
|
2203
|
-
*
|
|
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.
|
|
2204
2355
|
*
|
|
2205
2356
|
* @param {JxElement} template
|
|
2206
2357
|
* @param {JxScope} scope
|
|
@@ -2217,7 +2368,9 @@ function expandMapTemplate(template: JxElement, scope: JxScope): JxElement {
|
|
|
2217
2368
|
if (k === "children" && Array.isArray(template.children)) {
|
|
2218
2369
|
node.children = template.children.map((child) => {
|
|
2219
2370
|
if (isString(child)) {
|
|
2220
|
-
return child
|
|
2371
|
+
return isTemplateString(child)
|
|
2372
|
+
? (textChildValue(evaluateMapTemplate(child, scope)) ?? child)
|
|
2373
|
+
: child;
|
|
2221
2374
|
}
|
|
2222
2375
|
return expandMapTemplate(child, scope);
|
|
2223
2376
|
});
|
|
@@ -2302,7 +2455,8 @@ function evaluateMapTemplate(str: string, scope: JxScope): JxScope[string] {
|
|
|
2302
2455
|
|
|
2303
2456
|
/**
|
|
2304
2457
|
* Recursively resolve template strings in a document tree against a scope. Mutates the document in
|
|
2305
|
-
* 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).
|
|
2306
2460
|
*
|
|
2307
2461
|
* @param {JxElement | string} node
|
|
2308
2462
|
* @param {JxScope} scope
|
|
@@ -2421,6 +2575,12 @@ function resolveDocTemplates(
|
|
|
2421
2575
|
i += resolvedNodes.length;
|
|
2422
2576
|
continue;
|
|
2423
2577
|
}
|
|
2578
|
+
const resolvedText = textChildValue(resolved);
|
|
2579
|
+
if (resolvedText !== null) {
|
|
2580
|
+
node.children[i] = resolvedText;
|
|
2581
|
+
i += 1;
|
|
2582
|
+
continue;
|
|
2583
|
+
}
|
|
2424
2584
|
}
|
|
2425
2585
|
// Array pseudo-element among siblings: expand its items in place, on the same rule as the
|
|
2426
2586
|
// Whole-children branch above. An empty expansion removes the member and nothing else.
|
|
@@ -2556,7 +2716,7 @@ interface SlotCssCollector {
|
|
|
2556
2716
|
/**
|
|
2557
2717
|
* Walk the document tree and expand registered custom elements in-place. Applies $props via
|
|
2558
2718
|
* preRenderComponentHtml, marks static/prerendered. Slot children get their static styles collected
|
|
2559
|
-
* into `slotCss` (
|
|
2719
|
+
* into `slotCss` (each styled one given its `jxs-<n>` handle) before being serialized to HTML.
|
|
2560
2720
|
*
|
|
2561
2721
|
* @param {JxElement | string | (JxElement | string)[]} node - A nested array (authored JSON can
|
|
2562
2722
|
* carry one among children) is walked member by member
|
|
@@ -2627,7 +2787,12 @@ function expandComponents(
|
|
|
2627
2787
|
* render them twice.
|
|
2628
2788
|
*/
|
|
2629
2789
|
const shadow = resolveShadowMode(def, defaults);
|
|
2630
|
-
|
|
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;
|
|
2631
2796
|
// This instance is the first frame of the path, so a definition that names itself is reported
|
|
2632
2797
|
// At the first nesting rather than after the stack overflows.
|
|
2633
2798
|
const context: ComponentPrerenderContext = {
|
|
@@ -2692,7 +2857,7 @@ function expandComponents(
|
|
|
2692
2857
|
/*
|
|
2693
2858
|
* A non-static instance discards its prerendered markup on upgrade and re-renders, so the
|
|
2694
2859
|
* props have to survive as data or the element comes back with its state defaults — correct
|
|
2695
|
-
* 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
|
|
2696
2861
|
* connectedCallback three prop sources and this is the first of them; the compiler read it but
|
|
2697
2862
|
* never wrote it, and lifting `props.*` into $props above removed the other channel too.
|
|
2698
2863
|
*
|
package/src/site/site-loader.ts
CHANGED
|
@@ -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
|
*
|