@jxsuite/compiler 9.0.1 → 10.0.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 +269 -254
- package/dist/compiler.js +181 -162
- package/package.json +19 -18
- package/src/cli.ts +13 -3
- package/src/compiler.ts +38 -20
- package/src/shadow.ts +6 -33
- package/src/shared.ts +1850 -283
- package/src/site/base-path.ts +88 -2
- 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/client-runtime.ts +13 -14
- 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 +219 -88
- 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 +425 -105
- package/src/site/site-loader.ts +9 -0
- package/src/targets/compile-class.ts +42 -3
- package/src/targets/compile-client.ts +319 -91
- package/src/targets/compile-element.ts +696 -94
- package/src/targets/compile-pack.ts +234 -0
- package/src/targets/compile-static.ts +33 -83
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
*
|
|
10
10
|
* Output pattern: HTML: pre-rendered with data-bind, :prop="key", @event="key" JS: state (reactive
|
|
11
11
|
* state + computed signals), bind (DOM getters), on (event handlers), hydrate()
|
|
12
|
+
*
|
|
13
|
+
* A template string written as a text child has no element to carry a `data-bind`, so it is emitted
|
|
14
|
+
* between two comments, `<!--jx-text:_tN-->…<!--/jx-text-->`, and `hydrate()` keeps the text node
|
|
15
|
+
* after the opening comment current (compiler.md §2.2).
|
|
12
16
|
*/
|
|
13
17
|
|
|
14
18
|
import {
|
|
@@ -21,6 +25,10 @@ import {
|
|
|
21
25
|
ATTR_HELPER,
|
|
22
26
|
refuseBodyWithSrc,
|
|
23
27
|
attrHelperSource,
|
|
28
|
+
collectClientClasses,
|
|
29
|
+
emitExternalClassResolution,
|
|
30
|
+
externalClassHelperSource,
|
|
31
|
+
isClientExternalClass,
|
|
24
32
|
booleanAttrValue,
|
|
25
33
|
DEFAULT_LIT_HTML_SRC,
|
|
26
34
|
DEFAULT_REACTIVITY_SRC,
|
|
@@ -30,6 +38,7 @@ import {
|
|
|
30
38
|
compileExpression,
|
|
31
39
|
compileStatements,
|
|
32
40
|
compileStyles,
|
|
41
|
+
contentPropertyText,
|
|
33
42
|
createCompileContext,
|
|
34
43
|
emitFormulaFn,
|
|
35
44
|
emitRequestFetch,
|
|
@@ -39,14 +48,28 @@ import {
|
|
|
39
48
|
isRefObject,
|
|
40
49
|
isSchemaOnly,
|
|
41
50
|
isTemplateString,
|
|
51
|
+
KEYED_LIST_HELPER,
|
|
52
|
+
KEYED_LIST_IMPORTS,
|
|
53
|
+
keyedListDirectiveSource,
|
|
54
|
+
keyPath,
|
|
55
|
+
LIST_LITERAL_HELPER,
|
|
56
|
+
LIST_ITEMS_HELPER,
|
|
57
|
+
listHelperSource,
|
|
58
|
+
mappedArrayFeatures,
|
|
59
|
+
pageImportMap,
|
|
60
|
+
refuseTemplateItems,
|
|
42
61
|
LIT_PART_GUARD,
|
|
43
62
|
PREFORMATTED_TAGS,
|
|
44
63
|
pureSchemeOf,
|
|
64
|
+
isExpandedInstance,
|
|
45
65
|
resolveStaticValue,
|
|
46
66
|
serverCallHelperSource,
|
|
47
67
|
collectFunctionImports,
|
|
48
68
|
emitSrcImports,
|
|
49
69
|
templateValueSource,
|
|
70
|
+
authorClassValue,
|
|
71
|
+
styleHandleOf,
|
|
72
|
+
writesMergedClass,
|
|
50
73
|
} from "../shared.ts";
|
|
51
74
|
import {
|
|
52
75
|
hasStructuredBody,
|
|
@@ -64,8 +87,9 @@ import {
|
|
|
64
87
|
isTagExpression,
|
|
65
88
|
tagNameCandidates,
|
|
66
89
|
} from "@jxsuite/schema/guards";
|
|
67
|
-
import type { CompileContext, SrcImports } from "../shared.ts";
|
|
90
|
+
import type { ClientClasses, CompileContext, MappedArrayFeatures, SrcImports } from "../shared.ts";
|
|
68
91
|
import {
|
|
92
|
+
canonicalMapRef,
|
|
69
93
|
escapeToken,
|
|
70
94
|
objectKey,
|
|
71
95
|
refAccessor,
|
|
@@ -81,6 +105,7 @@ import type {
|
|
|
81
105
|
JxExpressionNode,
|
|
82
106
|
JxMappedArray,
|
|
83
107
|
JxMutableNode,
|
|
108
|
+
JxPrototypeDef,
|
|
84
109
|
JxStyle,
|
|
85
110
|
} from "@jxsuite/schema/types";
|
|
86
111
|
|
|
@@ -90,6 +115,34 @@ import type {
|
|
|
90
115
|
*/
|
|
91
116
|
type RepeaterSlot = Omit<JxMappedArray, "items"> & { items?: JxMappedArray["items"] };
|
|
92
117
|
|
|
118
|
+
/**
|
|
119
|
+
* The key counters one compilation shares between the tree walker and the module emitter, and what
|
|
120
|
+
* the walker found that the module has to carry.
|
|
121
|
+
*/
|
|
122
|
+
interface ClientCounter {
|
|
123
|
+
/** Inline event handlers (`_hN`). */
|
|
124
|
+
h: number;
|
|
125
|
+
/** Lit-rendered regions (`_listN`, `_childrenN`). */
|
|
126
|
+
l: number;
|
|
127
|
+
m: number;
|
|
128
|
+
/** Whether any region is rendered through lit-html. */
|
|
129
|
+
needsLit: boolean;
|
|
130
|
+
/** Bound style declarations (`_sN`). */
|
|
131
|
+
s: number;
|
|
132
|
+
/** `$switch` regions (`_swN`). */
|
|
133
|
+
sw: number;
|
|
134
|
+
/** Template bindings (`_tN`): `textContent`, attributes, properties and text children. */
|
|
135
|
+
t: number;
|
|
136
|
+
/** Whether any text child is bound between comment markers (compiler.md §2.2). */
|
|
137
|
+
textParts: boolean;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Elements whose content HTML parses as text, so a comment written there is rendered literally and
|
|
142
|
+
* cannot mark a bound text child.
|
|
143
|
+
*/
|
|
144
|
+
const RAW_TEXT_TAGS = new Set(["script", "style", "textarea", "title"]);
|
|
145
|
+
|
|
93
146
|
/** A compiled event handler entry: parameter names + function body. */
|
|
94
147
|
interface HandlerDef {
|
|
95
148
|
/** The parameters the body is built with: `state` first, then the event under some name. */
|
|
@@ -117,6 +170,8 @@ export function compileClient(
|
|
|
117
170
|
title?: string;
|
|
118
171
|
reactivitySrc?: string;
|
|
119
172
|
litHtmlSrc?: string;
|
|
173
|
+
/** The build's whole import map, written in place of the two sources above (compiler.md §12). */
|
|
174
|
+
runtimeImports?: Record<string, string> | undefined;
|
|
120
175
|
modulePath?: string;
|
|
121
176
|
projectStyle?: JxStyle | null;
|
|
122
177
|
prePaintScheme?: boolean;
|
|
@@ -127,6 +182,11 @@ export function compileClient(
|
|
|
127
182
|
* module, so the base has to be known when it is compiled.
|
|
128
183
|
*/
|
|
129
184
|
serverBaseUrl?: string;
|
|
185
|
+
/**
|
|
186
|
+
* The URL a `timing: "client"` external class's module is served at (compiler.md §3, §12). Only
|
|
187
|
+
* a site build has one; without it such an entry is a compile error.
|
|
188
|
+
*/
|
|
189
|
+
resolveClass?: (def: JxPrototypeDef) => string;
|
|
130
190
|
},
|
|
131
191
|
) {
|
|
132
192
|
const {
|
|
@@ -143,7 +203,16 @@ export function compileClient(
|
|
|
143
203
|
const styleBlock = compileStyles(raw, raw.$media ?? {}, opts.projectStyle ?? null);
|
|
144
204
|
|
|
145
205
|
// Collectors for bindings and handlers
|
|
146
|
-
const counter = {
|
|
206
|
+
const counter: ClientCounter = {
|
|
207
|
+
h: 0,
|
|
208
|
+
l: 0,
|
|
209
|
+
m: 0,
|
|
210
|
+
needsLit: false,
|
|
211
|
+
s: 0,
|
|
212
|
+
sw: 0,
|
|
213
|
+
t: 0,
|
|
214
|
+
textParts: false,
|
|
215
|
+
};
|
|
147
216
|
const bindings = new Map<string, string>(); // Key → expression string
|
|
148
217
|
const handlers = new Map<string, HandlerDef>(); // Key → { body, args }
|
|
149
218
|
|
|
@@ -156,6 +225,8 @@ export function compileClient(
|
|
|
156
225
|
const serverCalls: string[] = []; // One block per `timing: "server"` entry, after the computed block
|
|
157
226
|
|
|
158
227
|
const defs = raw.state ?? {};
|
|
228
|
+
// `timing: "client"` external classes: null until the module below resolves them (compiler.md §3)
|
|
229
|
+
const clientClasses = collectClientClasses(defs, opts.resolveClass);
|
|
159
230
|
for (const [key, def] of Object.entries(defs)) {
|
|
160
231
|
if (!isNonNullObject(def) || Array.isArray(def)) {
|
|
161
232
|
// Naked primitive or array → reactive state
|
|
@@ -257,6 +328,11 @@ export function compileClient(
|
|
|
257
328
|
initBlocks.push(emitCookieInit(key, def.name ?? key, def.default ?? null));
|
|
258
329
|
continue;
|
|
259
330
|
}
|
|
331
|
+
|
|
332
|
+
if (isClientExternalClass(def)) {
|
|
333
|
+
stateEntries.push([key, null]);
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
260
336
|
}
|
|
261
337
|
|
|
262
338
|
// `timing: "server"`: null until the site worker's route answers (compiler.md §6.1)
|
|
@@ -303,13 +379,16 @@ export function compileClient(
|
|
|
303
379
|
counter,
|
|
304
380
|
reactivitySrc,
|
|
305
381
|
opts.rewriteSrc,
|
|
382
|
+
clientClasses,
|
|
383
|
+
mappedArrayFeatures(raw),
|
|
306
384
|
);
|
|
307
385
|
|
|
308
|
-
//
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
386
|
+
// The page's import map: lit-html always, since compiled custom elements need it.
|
|
387
|
+
const importMap = pageImportMap({
|
|
388
|
+
litHtmlSrc,
|
|
389
|
+
reactivitySrc,
|
|
390
|
+
runtimeImports: opts.runtimeImports,
|
|
391
|
+
});
|
|
313
392
|
|
|
314
393
|
// Forced-scheme contract (spec §9.5): restore the persisted scheme before any styles apply
|
|
315
394
|
const prePaint =
|
|
@@ -324,13 +403,7 @@ export function compileClient(
|
|
|
324
403
|
<meta charset="utf-8">
|
|
325
404
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
326
405
|
<title>${escapeHtml(title)}</title>
|
|
327
|
-
${prePaint}
|
|
328
|
-
{
|
|
329
|
-
"imports": {
|
|
330
|
-
${importmapEntries.join(",\n")}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
</script>
|
|
406
|
+
${prePaint}${importMap}
|
|
334
407
|
${styleBlock}
|
|
335
408
|
<script type="module" src="./${modulePath}"></script>
|
|
336
409
|
</head>
|
|
@@ -349,15 +422,7 @@ ${importmapEntries.join(",\n")}
|
|
|
349
422
|
* @param {CompileContext} context
|
|
350
423
|
* @param {Map<string, string>} bindings
|
|
351
424
|
* @param {Map<string, HandlerDef>} handlers
|
|
352
|
-
* @param {
|
|
353
|
-
* t: number;
|
|
354
|
-
* s: number;
|
|
355
|
-
* h: number;
|
|
356
|
-
* m: number;
|
|
357
|
-
* sw: number;
|
|
358
|
-
* l: number;
|
|
359
|
-
* needsLit: boolean;
|
|
360
|
-
* }} counter
|
|
425
|
+
* @param {ClientCounter} counter
|
|
361
426
|
* @returns {string}
|
|
362
427
|
*/
|
|
363
428
|
function buildClientNode(
|
|
@@ -365,19 +430,24 @@ function buildClientNode(
|
|
|
365
430
|
context: CompileContext,
|
|
366
431
|
bindings: Map<string, string>,
|
|
367
432
|
handlers: Map<string, HandlerDef>,
|
|
368
|
-
counter:
|
|
369
|
-
t: number;
|
|
370
|
-
s: number;
|
|
371
|
-
h: number;
|
|
372
|
-
m: number;
|
|
373
|
-
sw: number;
|
|
374
|
-
l: number;
|
|
375
|
-
needsLit: boolean;
|
|
376
|
-
},
|
|
433
|
+
counter: ClientCounter,
|
|
377
434
|
) {
|
|
378
435
|
// String children are text nodes
|
|
379
436
|
if (isString(def)) {
|
|
380
|
-
|
|
437
|
+
if (!isTemplateString(def)) {
|
|
438
|
+
return escapeHtml(def);
|
|
439
|
+
}
|
|
440
|
+
/* A template text child is bound without a wrapper element (spec.md §8.4): the two comments
|
|
441
|
+
delimit the one text node the binding owns, so the separator after it and any text sibling
|
|
442
|
+
the parser would merge with it stay out. It holds whatever prerender can resolve, as a
|
|
443
|
+
bound `textContent` does. */
|
|
444
|
+
const key = `_t${counter.t}`;
|
|
445
|
+
counter.t += 1;
|
|
446
|
+
counter.textParts = true;
|
|
447
|
+
bindings.set(key, `() => \`${def}\``);
|
|
448
|
+
const value = resolveStaticValue(def, context.scope);
|
|
449
|
+
const text = value == null ? "" : escapeHtml(String(value));
|
|
450
|
+
return `<!--jx-text:${key}-->${text}<!--/jx-text-->`;
|
|
381
451
|
}
|
|
382
452
|
if (isNumber(def) || isBoolean(def)) {
|
|
383
453
|
return escapeHtml(String(def));
|
|
@@ -500,6 +570,22 @@ function buildClientNode(
|
|
|
500
570
|
// Dynamic attributes
|
|
501
571
|
if (isNonNullObject(def.attributes)) {
|
|
502
572
|
for (const [attr, val] of Object.entries(def.attributes)) {
|
|
573
|
+
const handle = attr === "class" ? styleHandleOf(def) : undefined;
|
|
574
|
+
if (handle !== undefined && (isRefObject(val) || isTemplateString(val))) {
|
|
575
|
+
/* A bound `class` attribute would replace the whole attribute and take the style handle
|
|
576
|
+
with it, so its binding joins the bound value and the handle. A key of its own: a ref's
|
|
577
|
+
shared key is also read by every other element binding the same ref. */
|
|
578
|
+
const key = `_t${counter.t}`;
|
|
579
|
+
counter.t += 1;
|
|
580
|
+
const source = isRefObject(val) ? stateRead(val.$ref) : `\`${val}\``;
|
|
581
|
+
bindAttrs.push(`:attr.class="${key}"`);
|
|
582
|
+
bindings.set(
|
|
583
|
+
key,
|
|
584
|
+
`() => [${source}, ${JSON.stringify(handle)}].filter((c) => c != null && c !== "").join(" ")`,
|
|
585
|
+
);
|
|
586
|
+
needsBind = true;
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
503
589
|
if (isRefObject(val)) {
|
|
504
590
|
const key = refToBindingKey(val.$ref);
|
|
505
591
|
bindAttrs.push(`:attr.${attr}="${key}"`);
|
|
@@ -582,6 +668,8 @@ function buildClientNode(
|
|
|
582
668
|
const bindAttrStrSwitch = ` ${bindAttrs.join(" ")}`;
|
|
583
669
|
return `<${tag}${staticAttrs} data-bind${bindAttrStrSwitch}></${tag}>`;
|
|
584
670
|
}
|
|
671
|
+
// A content property is the element's text (spec.md §8.1); a bound one keeps its `:prop` above.
|
|
672
|
+
const contentText = contentPropertyText(source, tag, nextContext.scope);
|
|
585
673
|
if (source.textContent !== undefined && !needsBind) {
|
|
586
674
|
const value = resolveStaticValue(source.textContent, nextContext.scope);
|
|
587
675
|
inner = value == null ? "" : escapeHtml(String(value));
|
|
@@ -591,8 +679,13 @@ function buildClientNode(
|
|
|
591
679
|
} else if (source.innerHTML) {
|
|
592
680
|
// ResolveStaticValue may return null if innerHTML contains `${` from rendered content
|
|
593
681
|
// (e.g., code examples) that isn't an actual template expression. Fall back to raw value.
|
|
594
|
-
|
|
682
|
+
// An expanded instance's markup is finished HTML, never a template (`isExpandedInstance`).
|
|
683
|
+
const resolved = isExpandedInstance(source)
|
|
684
|
+
? source.innerHTML
|
|
685
|
+
: resolveStaticValue(source.innerHTML, nextContext.scope);
|
|
595
686
|
inner = resolved == null ? source.innerHTML : String(resolved);
|
|
687
|
+
} else if (contentText !== undefined) {
|
|
688
|
+
inner = contentText;
|
|
596
689
|
} else if (isMappedArray(source.children)) {
|
|
597
690
|
// ─── Mapped array → lit-html render binding ───
|
|
598
691
|
counter.needsLit = true;
|
|
@@ -600,17 +693,8 @@ function buildClientNode(
|
|
|
600
693
|
counter.l += 1;
|
|
601
694
|
const arrayDef = source.children;
|
|
602
695
|
|
|
603
|
-
//
|
|
604
|
-
|
|
605
|
-
? stateRead(arrayDef.items.$ref)
|
|
606
|
-
: JSON.stringify(arrayDef.items);
|
|
607
|
-
|
|
608
|
-
// Compile the map template to a lit-html template string
|
|
609
|
-
const litTemplate = emitLitMapTemplate(arrayDef.map);
|
|
610
|
-
bindings.set(
|
|
611
|
-
listKey,
|
|
612
|
-
`() => (${itemsExpr} ?? []).map((item, index) => html\`${litTemplate}\`)`,
|
|
613
|
-
);
|
|
696
|
+
// The list, arranged and keyed as the document asks, over the map template (`emitClientList`).
|
|
697
|
+
bindings.set(listKey, `() => ${emitClientList(arrayDef)}`);
|
|
614
698
|
|
|
615
699
|
bindAttrs.push(`:render="${listKey}"`);
|
|
616
700
|
needsBind = true;
|
|
@@ -644,6 +728,12 @@ function buildClientNode(
|
|
|
644
728
|
const bindAttrStr2 = ` ${bindAttrs.join(" ")}`;
|
|
645
729
|
return `<${tag}${staticAttrs} data-bind${bindAttrStr2}></${tag}>`;
|
|
646
730
|
} else if (Array.isArray(source.children)) {
|
|
731
|
+
if (RAW_TEXT_TAGS.has(tag) && source.children.some((c) => isTemplateString(c))) {
|
|
732
|
+
throw new Error(
|
|
733
|
+
`A template text child of <${tag}> cannot be bound: HTML reads its content as text. ` +
|
|
734
|
+
"Write it as the element's textContent.",
|
|
735
|
+
);
|
|
736
|
+
}
|
|
647
737
|
inner = source.children
|
|
648
738
|
.map((c) => buildClientNode(c, nextContext, bindings, handlers, counter))
|
|
649
739
|
.join(childSeparator(nextContext.preformatted));
|
|
@@ -690,8 +780,21 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
|
|
|
690
780
|
if (def.id) {
|
|
691
781
|
attrs += ` id="${def.id}"`;
|
|
692
782
|
}
|
|
693
|
-
|
|
694
|
-
|
|
783
|
+
/* One `class` attribute: the author's classes, then the style handle the page sheet selects
|
|
784
|
+
(spec.md §9.2). The author's are `attributes.class` when the element writes one. */
|
|
785
|
+
const mergesClass = writesMergedClass(def);
|
|
786
|
+
if (mergesClass) {
|
|
787
|
+
const author = authorClassValue(def);
|
|
788
|
+
let authorText = "";
|
|
789
|
+
if (isRefObject(author)) {
|
|
790
|
+
authorText = `\${${mapRefToClientExpr(author.$ref)} ?? ""}`;
|
|
791
|
+
} else if (isString(author)) {
|
|
792
|
+
authorText = mapRefsToLit(author);
|
|
793
|
+
} else if (isNumber(author)) {
|
|
794
|
+
authorText = String(author);
|
|
795
|
+
}
|
|
796
|
+
const classText = [authorText, styleHandleOf(def) ?? ""].filter((t) => t !== "").join(" ");
|
|
797
|
+
attrs += ` class="${classText}"`;
|
|
695
798
|
}
|
|
696
799
|
|
|
697
800
|
// $props → lit property bindings. The element target emits these; the client target emitted
|
|
@@ -711,10 +814,19 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
|
|
|
711
814
|
// Attributes object
|
|
712
815
|
if (isNonNullObject(def.attributes)) {
|
|
713
816
|
for (const [k, v] of Object.entries(def.attributes)) {
|
|
817
|
+
// Written once above, merged with the style handle.
|
|
818
|
+
if (mergesClass && k === "class") {
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
714
821
|
if (isTemplateString(v)) {
|
|
715
822
|
attrs += ` ${k}="${mapRefsToLit(v)}"`;
|
|
716
823
|
continue;
|
|
717
824
|
}
|
|
825
|
+
// A bound attribute reads its pointer, the loop variable included, as the element target does.
|
|
826
|
+
if (isRefObject(v)) {
|
|
827
|
+
attrs += ` ${k}="\${${mapRefToClientExpr(v.$ref)}}"`;
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
718
830
|
/* A boolean is the attribute's PRESENCE or its text depending on the family its NAME puts it
|
|
719
831
|
in — `booleanAttrValue` owns that judgement, and the static emitter in `shared.ts` and the
|
|
720
832
|
runtime's `applyAttributes` both defer to it, so a prerendered page cannot change meaning
|
|
@@ -732,7 +844,10 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
|
|
|
732
844
|
}
|
|
733
845
|
}
|
|
734
846
|
|
|
735
|
-
|
|
847
|
+
/* Style → inline CSS, for templated declarations only. A literal declaration is a rule on the
|
|
848
|
+
element's handle in the page sheet (`collectStyles` walks row templates, `$switch` cases and
|
|
849
|
+
mixed children), and an inline copy would outweigh that rule's own `:hover` and `@media`
|
|
850
|
+
blocks, so none is written. */
|
|
736
851
|
if (isNonNullObject(def.style)) {
|
|
737
852
|
const parts: string[] = [];
|
|
738
853
|
for (const [k, v] of Object.entries(def.style)) {
|
|
@@ -748,11 +863,8 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
|
|
|
748
863
|
if (isNonDeclarationValue(v)) {
|
|
749
864
|
continue;
|
|
750
865
|
}
|
|
751
|
-
const cssProp = camelToKebab(k);
|
|
752
866
|
if (isTemplateString(String(v))) {
|
|
753
|
-
parts.push(`${
|
|
754
|
-
} else {
|
|
755
|
-
parts.push(`${cssProp}: ${v}`);
|
|
867
|
+
parts.push(`${camelToKebab(k)}: ${mapRefsToLit(String(v))}`);
|
|
756
868
|
}
|
|
757
869
|
}
|
|
758
870
|
if (parts.length > 0) {
|
|
@@ -802,9 +914,31 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
|
|
|
802
914
|
}
|
|
803
915
|
}
|
|
804
916
|
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
917
|
+
/* Every other property the row sets is bound as a property, as the element target and the
|
|
918
|
+
runtime do (spec.md §8.1): the platform reflects the ones HTML reflects. */
|
|
919
|
+
for (const [key, val] of Object.entries(def)) {
|
|
920
|
+
if (
|
|
921
|
+
RESERVED_KEYS.has(key) ||
|
|
922
|
+
key.startsWith("$") ||
|
|
923
|
+
key.startsWith("on") ||
|
|
924
|
+
key === "tagName" ||
|
|
925
|
+
key === "id" ||
|
|
926
|
+
key === "className" ||
|
|
927
|
+
key === "style" ||
|
|
928
|
+
key === "children" ||
|
|
929
|
+
key === "textContent" ||
|
|
930
|
+
key === "innerHTML" ||
|
|
931
|
+
key === "attributes"
|
|
932
|
+
) {
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
if (isRefObject(val)) {
|
|
936
|
+
attrs += ` .${key}="\${${mapRefToClientExpr(val.$ref)}}"`;
|
|
937
|
+
} else if (isTemplateString(val)) {
|
|
938
|
+
attrs += ` .${key}="${mapRefsToLit(val)}"`;
|
|
939
|
+
} else {
|
|
940
|
+
attrs += ` .${key}="\${${JSON.stringify(val)}}"`;
|
|
941
|
+
}
|
|
808
942
|
}
|
|
809
943
|
|
|
810
944
|
/* A row can hold a component instance, whose children its slots redistribute; a child part
|
|
@@ -818,8 +952,10 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
|
|
|
818
952
|
if (isTemplateString(tc)) {
|
|
819
953
|
inner = `${mapRefsToLit(tc)}${guard}`;
|
|
820
954
|
} else if (isRefObject(def.textContent)) {
|
|
821
|
-
|
|
822
|
-
|
|
955
|
+
/* Read as the expression the pointer names, the loop variable included. The binding key
|
|
956
|
+
(`refToBindingKey`) is a name, and read as one it made `$map/item` the member
|
|
957
|
+
`state.$map_item`, so a row bound to its own item rendered empty. */
|
|
958
|
+
inner = `\${${mapRefToClientExpr(def.textContent.$ref)}}${guard}`;
|
|
823
959
|
} else {
|
|
824
960
|
inner = escapeHtml(tc);
|
|
825
961
|
}
|
|
@@ -866,17 +1002,68 @@ function emitChildLit(c: JxMutableNode | JsonPrimitive, preformatted = false): s
|
|
|
866
1002
|
}
|
|
867
1003
|
|
|
868
1004
|
/**
|
|
869
|
-
* Emit a lit-html
|
|
870
|
-
*
|
|
1005
|
+
* Emit a lit-html hole for a mapped array, expanding its template inline among sibling nodes
|
|
1006
|
+
* (wrapper-less).
|
|
871
1007
|
*
|
|
872
1008
|
* @param {RepeaterSlot} arrayDef
|
|
873
1009
|
* @returns {string}
|
|
874
1010
|
*/
|
|
875
1011
|
function emitArrayHole(arrayDef: RepeaterSlot): string {
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
1012
|
+
return `\${${emitClientList(arrayDef)}}`;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
/**
|
|
1016
|
+
* The read of a list source (`items`, `filter` or `sort`): the loop variable for a `$map` pointer,
|
|
1017
|
+
* which is how a nested list reads its items off the row, and the null-tolerant state read for
|
|
1018
|
+
* anything else, since a `Request` or a server entry holds nothing until it answers.
|
|
1019
|
+
*
|
|
1020
|
+
* @param {string} ref
|
|
1021
|
+
* @returns {string}
|
|
1022
|
+
*/
|
|
1023
|
+
function listSourceRead(ref: string): string {
|
|
1024
|
+
return canonicalMapRef(ref).startsWith("$map/") ? mapRefToClientExpr(ref) : stateRead(ref);
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* The expression a mapped array renders as, the one list emitter of this target: the whole-children
|
|
1029
|
+
* binding and a hole among siblings both call it (spec.md §10.3, §10.4, compiler.md §4.7).
|
|
1030
|
+
*
|
|
1031
|
+
* A keyed list goes through `__jxKeyed`, which shapes it, keys it and hands lit's `repeat()` a
|
|
1032
|
+
* snapshot. An unkeyed list is a `.map()`, over `__jxListItems(…)` when it filters or sorts and
|
|
1033
|
+
* over `items` as written when it declares neither, so a list compiled before either existed keeps
|
|
1034
|
+
* its bytes.
|
|
1035
|
+
*
|
|
1036
|
+
* @param {RepeaterSlot} arrayDef
|
|
1037
|
+
* @returns {string}
|
|
1038
|
+
*/
|
|
1039
|
+
function emitClientList(arrayDef: RepeaterSlot): string {
|
|
1040
|
+
refuseTemplateItems(arrayDef);
|
|
1041
|
+
const { items, filter, sort, key } = arrayDef;
|
|
1042
|
+
const keyed = isRefObject(key);
|
|
1043
|
+
const literal = JSON.stringify(items ?? []);
|
|
1044
|
+
let itemsExpr = keyed ? `${LIST_LITERAL_HELPER}(${JSON.stringify(literal)})` : literal;
|
|
1045
|
+
if (isRefObject(items)) {
|
|
1046
|
+
itemsExpr = listSourceRead(items.$ref);
|
|
1047
|
+
}
|
|
1048
|
+
const filterExpr = isRefObject(filter) ? listSourceRead(filter.$ref) : "undefined";
|
|
1049
|
+
const sortExpr = isRefObject(sort) ? listSourceRead(sort.$ref) : "undefined";
|
|
1050
|
+
const row = `(item, index) => html\`${emitLitMapTemplate(arrayDef.map)}\``;
|
|
1051
|
+
if (keyed) {
|
|
1052
|
+
const pointer = key.$ref;
|
|
1053
|
+
const args = [
|
|
1054
|
+
`(${itemsExpr} ?? [])`,
|
|
1055
|
+
filterExpr,
|
|
1056
|
+
sortExpr,
|
|
1057
|
+
JSON.stringify(keyPath(pointer)),
|
|
1058
|
+
JSON.stringify(pointer),
|
|
1059
|
+
row,
|
|
1060
|
+
];
|
|
1061
|
+
return `${KEYED_LIST_HELPER}(${args.join(", ")})`;
|
|
1062
|
+
}
|
|
1063
|
+
if (isRefObject(filter) || isRefObject(sort)) {
|
|
1064
|
+
return `${LIST_ITEMS_HELPER}(${itemsExpr}, ${filterExpr}, ${sortExpr}).map(${row})`;
|
|
1065
|
+
}
|
|
1066
|
+
return `(${itemsExpr} ?? []).map(${row})`;
|
|
880
1067
|
}
|
|
881
1068
|
|
|
882
1069
|
/**
|
|
@@ -910,15 +1097,7 @@ function bareMapRefsToLit(body: string): string {
|
|
|
910
1097
|
* @param {string[]} initBlocks
|
|
911
1098
|
* @param {string[]} serverCalls
|
|
912
1099
|
* @param {SrcImports} srcImports
|
|
913
|
-
* @param {
|
|
914
|
-
* t: number;
|
|
915
|
-
* s: number;
|
|
916
|
-
* h: number;
|
|
917
|
-
* m: number;
|
|
918
|
-
* sw: number;
|
|
919
|
-
* l: number;
|
|
920
|
-
* needsLit: boolean;
|
|
921
|
-
* }} counter
|
|
1100
|
+
* @param {ClientCounter} counter
|
|
922
1101
|
* @param {string} _reactivitySrc
|
|
923
1102
|
* @returns {string}
|
|
924
1103
|
*/
|
|
@@ -930,20 +1109,14 @@ function emitClientModule(
|
|
|
930
1109
|
initBlocks: string[],
|
|
931
1110
|
serverCalls: string[],
|
|
932
1111
|
srcImports: SrcImports,
|
|
933
|
-
counter:
|
|
934
|
-
t: number;
|
|
935
|
-
s: number;
|
|
936
|
-
h: number;
|
|
937
|
-
m: number;
|
|
938
|
-
sw: number;
|
|
939
|
-
l: number;
|
|
940
|
-
needsLit: boolean;
|
|
941
|
-
},
|
|
1112
|
+
counter: ClientCounter,
|
|
942
1113
|
_reactivitySrc: string,
|
|
943
|
-
rewriteSrc
|
|
1114
|
+
rewriteSrc: ((specifier: string) => string) | undefined,
|
|
1115
|
+
clientClasses: ClientClasses,
|
|
1116
|
+
lists: MappedArrayFeatures,
|
|
944
1117
|
) {
|
|
945
1118
|
const lines: string[] = [];
|
|
946
|
-
const { needsLit } = counter;
|
|
1119
|
+
const { needsLit, textParts } = counter;
|
|
947
1120
|
const needsComputed = computedEntries.length > 0;
|
|
948
1121
|
|
|
949
1122
|
lines.push("// Generated by @jxsuite/compiler — do not edit manually");
|
|
@@ -953,12 +1126,19 @@ function emitClientModule(
|
|
|
953
1126
|
if (needsComputed) {
|
|
954
1127
|
reactivityImports.push("computed");
|
|
955
1128
|
}
|
|
1129
|
+
// The keyed-list directive owns an effect it stops, and a snapshot the page's render reads.
|
|
1130
|
+
if (lists.keyed) {
|
|
1131
|
+
reactivityImports.push("stop", "shallowRef");
|
|
1132
|
+
}
|
|
956
1133
|
lines.push(`import { ${reactivityImports.join(", ")} } from '@vue/reactivity';`);
|
|
957
1134
|
|
|
958
1135
|
// Lit-html imports (only when arrays are present)
|
|
959
1136
|
if (needsLit) {
|
|
960
1137
|
lines.push("import { html, render } from 'lit-html';");
|
|
961
1138
|
}
|
|
1139
|
+
if (lists.keyed) {
|
|
1140
|
+
lines.push(...KEYED_LIST_IMPORTS);
|
|
1141
|
+
}
|
|
962
1142
|
|
|
963
1143
|
// $src imports — bundleable specifiers (npm:…, ./relative) are rewritten to their /assets/
|
|
964
1144
|
// Bundle URL; the site build bundles them after all documents compile (spec.md §12).
|
|
@@ -967,10 +1147,22 @@ function emitClientModule(
|
|
|
967
1147
|
// State — reactive state
|
|
968
1148
|
/* The boolean-attribute rule, inlined because this module loads with `@vue/reactivity` and
|
|
969
1149
|
nothing else — see `attrHelperSource`. The hydration script below is its only caller. */
|
|
970
|
-
lines.push(...src.imports, ...src.loaders, "", attrHelperSource());
|
|
1150
|
+
lines.push(...src.imports, ...clientClasses.imports, ...src.loaders, "", attrHelperSource());
|
|
1151
|
+
/* The list rules (compiler.md §4.7), carried only by a module whose document filters, sorts or
|
|
1152
|
+
keys a list. */
|
|
1153
|
+
if (lists.arranged || lists.keyed) {
|
|
1154
|
+
lines.push(listHelperSource());
|
|
1155
|
+
}
|
|
1156
|
+
if (lists.keyed) {
|
|
1157
|
+
lines.push(keyedListDirectiveSource());
|
|
1158
|
+
}
|
|
971
1159
|
if (serverCalls.length > 0) {
|
|
972
1160
|
lines.push("", serverCallHelperSource());
|
|
973
1161
|
}
|
|
1162
|
+
const waitsForClasses = clientClasses.entries.length > 0;
|
|
1163
|
+
if (waitsForClasses) {
|
|
1164
|
+
lines.push("", externalClassHelperSource(), "const _jxInstances = new Map();");
|
|
1165
|
+
}
|
|
974
1166
|
lines.push("", "const state = reactive({");
|
|
975
1167
|
for (const [key, val] of stateEntries) {
|
|
976
1168
|
/*
|
|
@@ -1032,9 +1224,20 @@ function emitClientModule(
|
|
|
1032
1224
|
lines.push("const on = {};");
|
|
1033
1225
|
}
|
|
1034
1226
|
// Hydration function
|
|
1227
|
+
lines.push("", "function hydrate(root) {");
|
|
1228
|
+
if (textParts) {
|
|
1229
|
+
/* Collected BEFORE the element pass, so a region lit renders during it is never walked. 128 is
|
|
1230
|
+
`NodeFilter.SHOW_COMMENT`, written as a literal so the module needs no further global. */
|
|
1231
|
+
lines.push(
|
|
1232
|
+
" const texts = [];",
|
|
1233
|
+
" const walker = document.createTreeWalker(root, 128);",
|
|
1234
|
+
" while (walker.nextNode()) {",
|
|
1235
|
+
" const c = walker.currentNode;",
|
|
1236
|
+
" if (c.data.startsWith('jx-text:') && c.data.slice(8) in bind) { texts.push(c); }",
|
|
1237
|
+
" }",
|
|
1238
|
+
);
|
|
1239
|
+
}
|
|
1035
1240
|
lines.push(
|
|
1036
|
-
"",
|
|
1037
|
-
"function hydrate(root) {",
|
|
1038
1241
|
" root.querySelectorAll('[data-bind]').forEach(el => {",
|
|
1039
1242
|
" [...el.attributes].forEach(a => {",
|
|
1040
1243
|
" if (a.name.startsWith(':')) {",
|
|
@@ -1070,11 +1273,35 @@ function emitClientModule(
|
|
|
1070
1273
|
" }",
|
|
1071
1274
|
" });",
|
|
1072
1275
|
" });",
|
|
1073
|
-
"}",
|
|
1074
|
-
"",
|
|
1075
|
-
"hydrate(document);",
|
|
1076
|
-
"",
|
|
1077
1276
|
);
|
|
1277
|
+
if (textParts) {
|
|
1278
|
+
// The prerendered text node is reused; an empty prerender has none, so one is put there.
|
|
1279
|
+
lines.push(
|
|
1280
|
+
" texts.forEach(c => {",
|
|
1281
|
+
" const key = c.data.slice(8);",
|
|
1282
|
+
" let text = c.nextSibling;",
|
|
1283
|
+
" if (!text || text.nodeType !== 3) {",
|
|
1284
|
+
" text = document.createTextNode('');",
|
|
1285
|
+
" c.after(text);",
|
|
1286
|
+
" }",
|
|
1287
|
+
" effect(() => { text.data = bind[key](); });",
|
|
1288
|
+
" });",
|
|
1289
|
+
);
|
|
1290
|
+
}
|
|
1291
|
+
lines.push("}", "");
|
|
1292
|
+
if (waitsForClasses) {
|
|
1293
|
+
/* Hydration waits for the classes: a binding that reads a class's value
|
|
1294
|
+
(`${state.forecast.days.length}`) then reads the value, as it does under the interpreter,
|
|
1295
|
+
which awaits every class before it renders. The prerendered page stays on screen meanwhile. */
|
|
1296
|
+
lines.push(
|
|
1297
|
+
`await ${emitExternalClassResolution(clientClasses.entries, {
|
|
1298
|
+
indent: "",
|
|
1299
|
+
instances: "_jxInstances",
|
|
1300
|
+
statePrefix: "state",
|
|
1301
|
+
})};`,
|
|
1302
|
+
);
|
|
1303
|
+
}
|
|
1304
|
+
lines.push("hydrate(document);", "");
|
|
1078
1305
|
|
|
1079
1306
|
return lines.join("\n");
|
|
1080
1307
|
}
|
|
@@ -1161,7 +1388,8 @@ function isNonDeclarationValue(
|
|
|
1161
1388
|
* @param {string} ref
|
|
1162
1389
|
* @returns {string}
|
|
1163
1390
|
*/
|
|
1164
|
-
function mapRefToClientExpr(
|
|
1391
|
+
function mapRefToClientExpr(written: string) {
|
|
1392
|
+
const ref = canonicalMapRef(written);
|
|
1165
1393
|
if (ref.startsWith("$map/")) {
|
|
1166
1394
|
// The loop variable is an emitted identifier, so its first segment is the base, not a member.
|
|
1167
1395
|
const [head, ...rest] = refSegments(ref.slice("$map/".length));
|