@jxsuite/compiler 0.22.1 → 0.23.1
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/compiler.js +171 -171
- package/package.json +3 -3
- package/src/shared.js +21 -2
- package/src/site/site-build.js +97 -1
- package/src/targets/compile-client.js +31 -1
- package/src/targets/compile-element.js +39 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jxsuite/compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.1",
|
|
4
4
|
"description": "Jx static HTML compiler, island detector, and site builder",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@apidevtools/json-schema-ref-parser": "^15.3.5",
|
|
41
|
-
"@jxsuite/parser": "^0.
|
|
42
|
-
"@jxsuite/runtime": "^0.
|
|
41
|
+
"@jxsuite/parser": "^0.23.1",
|
|
42
|
+
"@jxsuite/runtime": "^0.23.1",
|
|
43
43
|
"remark-gfm": "^4.0.1",
|
|
44
44
|
"remark-stringify": "^11.0.0",
|
|
45
45
|
"sharp": "^0.34.5",
|
package/src/shared.js
CHANGED
|
@@ -6,9 +6,17 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { camelToKebab, toCSSText, RESERVED_KEYS } from "@jxsuite/runtime";
|
|
9
|
+
import { compileExpression, isMutating, evaluateExpression } from "@jxsuite/runtime/expression";
|
|
9
10
|
|
|
10
11
|
// Re-export runtime utilities used by submodules
|
|
11
|
-
export {
|
|
12
|
+
export {
|
|
13
|
+
camelToKebab,
|
|
14
|
+
toCSSText,
|
|
15
|
+
RESERVED_KEYS,
|
|
16
|
+
compileExpression,
|
|
17
|
+
isMutating,
|
|
18
|
+
evaluateExpression,
|
|
19
|
+
};
|
|
12
20
|
|
|
13
21
|
// CDN defaults
|
|
14
22
|
export const DEFAULT_REACTIVITY_SRC = "https://esm.sh/@vue/reactivity@3.5.32";
|
|
@@ -231,7 +239,7 @@ export function buildInitialScope(defs = {}, parentScope = null) {
|
|
|
231
239
|
setOwnScopeValue(scope, key, cloneValue(d.default));
|
|
232
240
|
continue;
|
|
233
241
|
}
|
|
234
|
-
if (!d.$prototype && !isSchemaOnly(d)) {
|
|
242
|
+
if (!d.$prototype && !("$expression" in d) && !isSchemaOnly(d)) {
|
|
235
243
|
setOwnScopeValue(scope, key, cloneValue(d));
|
|
236
244
|
}
|
|
237
245
|
}
|
|
@@ -243,6 +251,17 @@ export function buildInitialScope(defs = {}, parentScope = null) {
|
|
|
243
251
|
}
|
|
244
252
|
if (!def || typeof def !== "object") continue;
|
|
245
253
|
const d = /** @type {JxStateObject & JxPrototypeDef} */ (def);
|
|
254
|
+
if ("$expression" in d) {
|
|
255
|
+
const node = /** @type {any} */ (d).$expression;
|
|
256
|
+
if (isMutating(node.operator)) {
|
|
257
|
+
setOwnScopeValue(scope, key, (/** @type {any} */ s, /** @type {any} */ event) =>
|
|
258
|
+
evaluateExpression(node, s, event),
|
|
259
|
+
);
|
|
260
|
+
} else {
|
|
261
|
+
defineLazyScopeValue(scope, key, () => evaluateExpression(node, scope, null));
|
|
262
|
+
}
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
246
265
|
if (d.$prototype === "Function") {
|
|
247
266
|
if (d.body) {
|
|
248
267
|
const fn = new Function("state", ...(d.parameters ?? d.arguments ?? []), d.body);
|
package/src/site/site-build.js
CHANGED
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
collectServerEntries,
|
|
38
38
|
renderStaticNode,
|
|
39
39
|
resolveStaticValue,
|
|
40
|
+
resolveRefValue,
|
|
40
41
|
DEFAULT_REACTIVITY_SRC,
|
|
41
42
|
DEFAULT_LIT_HTML_SRC,
|
|
42
43
|
} from "../shared.js";
|
|
@@ -435,7 +436,9 @@ async function compilePage(
|
|
|
435
436
|
expandComponents(layoutDoc, componentDefs);
|
|
436
437
|
|
|
437
438
|
// Strip resolved timing: "compiler" state entries — they're now baked into the tree
|
|
438
|
-
// and keeping them would cause isDynamic() to misclassify the page as dynamic
|
|
439
|
+
// and keeping them would cause isDynamic() to misclassify the page as dynamic.
|
|
440
|
+
// Also strip resolved content arrays (from ContentCollection) that have been
|
|
441
|
+
// baked into unrolled map templates.
|
|
439
442
|
if (layoutDoc.state) {
|
|
440
443
|
for (const [key, def] of Object.entries(layoutDoc.state)) {
|
|
441
444
|
if (key === "$site" || key === "$page") continue;
|
|
@@ -446,6 +449,8 @@ async function compilePage(
|
|
|
446
449
|
/** @type {JxMutableNode} */ (def).timing === "compiler"
|
|
447
450
|
) {
|
|
448
451
|
delete layoutDoc.state[key];
|
|
452
|
+
} else if (Array.isArray(def)) {
|
|
453
|
+
delete layoutDoc.state[key];
|
|
449
454
|
}
|
|
450
455
|
}
|
|
451
456
|
}
|
|
@@ -576,6 +581,69 @@ function isBareSpecifier(s) {
|
|
|
576
581
|
);
|
|
577
582
|
}
|
|
578
583
|
|
|
584
|
+
/**
|
|
585
|
+
* Deep-clone a map template, resolving template strings and $ref values against the given scope.
|
|
586
|
+
*
|
|
587
|
+
* @param {JxMutableNode} template
|
|
588
|
+
* @param {Record<string, unknown>} scope
|
|
589
|
+
* @returns {JxMutableNode}
|
|
590
|
+
*/
|
|
591
|
+
function expandMapTemplate(template, scope) {
|
|
592
|
+
if (!template || typeof template !== "object") return template;
|
|
593
|
+
const node = /** @type {JxMutableNode} */ ({});
|
|
594
|
+
for (const [k, v] of Object.entries(template)) {
|
|
595
|
+
if (k === "children" && Array.isArray(v)) {
|
|
596
|
+
node.children = v.map((child) => {
|
|
597
|
+
if (typeof child === "string") return child;
|
|
598
|
+
return expandMapTemplate(/** @type {JxMutableNode} */ (child), scope);
|
|
599
|
+
});
|
|
600
|
+
} else if (k === "style" && v && typeof v === "object") {
|
|
601
|
+
node.style = { ...v };
|
|
602
|
+
for (const [sk, sv] of Object.entries(node.style)) {
|
|
603
|
+
if (typeof sv === "string" && isTemplateString(sv)) {
|
|
604
|
+
node.style[sk] = evaluateMapTemplate(sv, scope) ?? sv;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
} else if (k === "attributes" && v && typeof v === "object") {
|
|
608
|
+
node.attributes = { ...v };
|
|
609
|
+
for (const [ak, av] of Object.entries(node.attributes)) {
|
|
610
|
+
if (typeof av === "string" && isTemplateString(av)) {
|
|
611
|
+
node.attributes[ak] = evaluateMapTemplate(av, scope) ?? av;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
} else if (typeof v === "string" && isTemplateString(v)) {
|
|
615
|
+
node[k] = evaluateMapTemplate(v, scope) ?? v;
|
|
616
|
+
} else {
|
|
617
|
+
node[k] = v;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
return node;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Evaluate a template string in the context of a mapped array item. Exposes `item`, `index`,
|
|
625
|
+
* `state`, and `$map` as local variables.
|
|
626
|
+
*
|
|
627
|
+
* @param {string} str
|
|
628
|
+
* @param {Record<string, unknown>} scope
|
|
629
|
+
* @returns {unknown}
|
|
630
|
+
*/
|
|
631
|
+
function evaluateMapTemplate(str, scope) {
|
|
632
|
+
try {
|
|
633
|
+
const item = /** @type {Record<string, unknown>} */ (scope.$map)?.item;
|
|
634
|
+
const index = /** @type {Record<string, unknown>} */ (scope.$map)?.index;
|
|
635
|
+
const singleExprMatch = str.match(/^\$\{(.+)\}$/s);
|
|
636
|
+
if (singleExprMatch) {
|
|
637
|
+
const fn = new Function("state", "$map", "item", "index", `return (${singleExprMatch[1]})`);
|
|
638
|
+
return fn(scope, scope.$map, item, index);
|
|
639
|
+
}
|
|
640
|
+
const fn = new Function("state", "$map", "item", "index", `return \`${str}\``);
|
|
641
|
+
return fn(scope, scope.$map, item, index);
|
|
642
|
+
} catch {
|
|
643
|
+
return null;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
579
647
|
/**
|
|
580
648
|
* Recursively resolve template strings in a document tree against a scope. Mutates the document in
|
|
581
649
|
* place — evaluates ${...} in innerHTML, textContent, style values, and attribute values.
|
|
@@ -617,6 +685,34 @@ function resolveDocTemplates(node, scope) {
|
|
|
617
685
|
}
|
|
618
686
|
}
|
|
619
687
|
const rawChildren = /** @type {unknown} */ (node.children);
|
|
688
|
+
if (
|
|
689
|
+
rawChildren &&
|
|
690
|
+
typeof rawChildren === "object" &&
|
|
691
|
+
!Array.isArray(rawChildren) &&
|
|
692
|
+
/** @type {JxMutableNode} */ (rawChildren).$prototype === "Array"
|
|
693
|
+
) {
|
|
694
|
+
const arrayDef = /** @type {JxMutableNode} */ (rawChildren);
|
|
695
|
+
const itemsSrc = arrayDef.items;
|
|
696
|
+
let items = null;
|
|
697
|
+
if (itemsSrc && typeof itemsSrc === "object" && /** @type {JxMutableNode} */ (itemsSrc).$ref) {
|
|
698
|
+
const ref = /** @type {string} */ (/** @type {JxMutableNode} */ (itemsSrc).$ref);
|
|
699
|
+
items = resolveRefValue(ref, scope);
|
|
700
|
+
} else if (Array.isArray(itemsSrc)) {
|
|
701
|
+
items = itemsSrc;
|
|
702
|
+
}
|
|
703
|
+
if (Array.isArray(items) && arrayDef.map) {
|
|
704
|
+
node.children = items.map((item, index) => {
|
|
705
|
+
const childScope = Object.create(scope);
|
|
706
|
+
childScope.$map = { item, index };
|
|
707
|
+
childScope["$map/item"] = item;
|
|
708
|
+
childScope["$map/index"] = index;
|
|
709
|
+
const expanded = expandMapTemplate(arrayDef.map, childScope);
|
|
710
|
+
resolveDocTemplates(expanded, childScope);
|
|
711
|
+
return expanded;
|
|
712
|
+
});
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
620
716
|
if (typeof rawChildren === "string" && isTemplateString(rawChildren)) {
|
|
621
717
|
const resolved = evaluateStaticTemplate(rawChildren, scope);
|
|
622
718
|
if (Array.isArray(resolved)) {
|
|
@@ -23,6 +23,8 @@ import {
|
|
|
23
23
|
escapeHtml,
|
|
24
24
|
DEFAULT_REACTIVITY_SRC,
|
|
25
25
|
DEFAULT_LIT_HTML_SRC,
|
|
26
|
+
compileExpression,
|
|
27
|
+
isMutating,
|
|
26
28
|
} from "../shared.js";
|
|
27
29
|
|
|
28
30
|
/**
|
|
@@ -85,6 +87,19 @@ export function compileClient(raw, opts) {
|
|
|
85
87
|
|
|
86
88
|
const d = /** @type {JxMutableNode} */ (def);
|
|
87
89
|
|
|
90
|
+
// $expression: Shape 5
|
|
91
|
+
if ("$expression" in d) {
|
|
92
|
+
const node = /** @type {any} */ (d).$expression;
|
|
93
|
+
if (isMutating(node.operator)) {
|
|
94
|
+
const compiled = compileExpression(node, { statePrefix: "state", eventParam: "e" });
|
|
95
|
+
onEntries.push([key, { args: ["state", "e"], body: compiled }]);
|
|
96
|
+
} else {
|
|
97
|
+
const compiled = compileExpression(node, { statePrefix: "state", eventParam: "e" });
|
|
98
|
+
computedEntries.push([key, `() => ${compiled}`]);
|
|
99
|
+
}
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
88
103
|
// $prototype: "Function"
|
|
89
104
|
if (d.$prototype === "Function") {
|
|
90
105
|
const args = d.parameters ?? d.arguments;
|
|
@@ -188,7 +203,7 @@ ${importmapEntries.join(",\n")}
|
|
|
188
203
|
}
|
|
189
204
|
</script>
|
|
190
205
|
${styleBlock}
|
|
191
|
-
<script type="module" src="
|
|
206
|
+
<script type="module" src="./${modulePath}"></script>
|
|
192
207
|
</head>
|
|
193
208
|
<body>
|
|
194
209
|
${bodyContent}
|
|
@@ -274,6 +289,15 @@ function buildClientNode(def, raw, context, bindings, handlers, counter) {
|
|
|
274
289
|
bindAttrs.push(`@${eventName}="${key}"`);
|
|
275
290
|
handlers.set(key, { args: v.parameters ?? v.arguments ?? ["state", "event"], body: v.body });
|
|
276
291
|
needsBind = true;
|
|
292
|
+
} else if (val && typeof val === "object" && "$expression" in /** @type {any} */ (val)) {
|
|
293
|
+
const key = `_h${counter.h++}`;
|
|
294
|
+
bindAttrs.push(`@${eventName}="${key}"`);
|
|
295
|
+
const compiled = compileExpression(/** @type {any} */ (val).$expression, {
|
|
296
|
+
statePrefix: "state",
|
|
297
|
+
eventParam: "e",
|
|
298
|
+
});
|
|
299
|
+
handlers.set(key, { args: ["state", "e"], body: compiled });
|
|
300
|
+
needsBind = true;
|
|
277
301
|
}
|
|
278
302
|
}
|
|
279
303
|
|
|
@@ -500,6 +524,12 @@ function emitLitMapTemplate(def) {
|
|
|
500
524
|
) {
|
|
501
525
|
const body = mapRefsToLit(/** @type {string} */ (/** @type {JxMutableNode} */ (val).body));
|
|
502
526
|
attrs += " @" + eventName + "=${(e) => { " + body + " }}";
|
|
527
|
+
} else if (val && typeof val === "object" && "$expression" in /** @type {any} */ (val)) {
|
|
528
|
+
const compiled = compileExpression(/** @type {any} */ (val).$expression, {
|
|
529
|
+
statePrefix: "state",
|
|
530
|
+
eventParam: "e",
|
|
531
|
+
});
|
|
532
|
+
attrs += " @" + eventName + "=${(e) => { state.$map = { item, index }; " + compiled + "; }}";
|
|
503
533
|
}
|
|
504
534
|
}
|
|
505
535
|
|
|
@@ -6,7 +6,14 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { camelToKebab, RESERVED_KEYS } from "@jxsuite/runtime";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
escapeHtml,
|
|
11
|
+
tagNameToClassName,
|
|
12
|
+
isSchemaOnly,
|
|
13
|
+
collectStyles,
|
|
14
|
+
compileExpression,
|
|
15
|
+
isMutating,
|
|
16
|
+
} from "../shared.js";
|
|
10
17
|
|
|
11
18
|
/**
|
|
12
19
|
* Compile a Jx custom element document to a JS module string.
|
|
@@ -230,7 +237,14 @@ export function emitElementModule(doc, className, elementImports) {
|
|
|
230
237
|
|
|
231
238
|
for (const [key, def] of Object.entries(defs)) {
|
|
232
239
|
const d = /** @type {JxMutableNode} */ (def);
|
|
233
|
-
if (d && typeof d === "object" && !Array.isArray(d) &&
|
|
240
|
+
if (d && typeof d === "object" && !Array.isArray(d) && "$expression" in d) {
|
|
241
|
+
const node = /** @type {any} */ (d).$expression;
|
|
242
|
+
if (isMutating(node.operator)) {
|
|
243
|
+
functionEntries.push([key, d]);
|
|
244
|
+
} else {
|
|
245
|
+
computedEntries.push([key, d]);
|
|
246
|
+
}
|
|
247
|
+
} else if (d && typeof d === "object" && !Array.isArray(d) && d.$prototype === "Function") {
|
|
234
248
|
if (typeof d.body === "string" && d.body.includes("return")) {
|
|
235
249
|
computedEntries.push([key, d]);
|
|
236
250
|
} else {
|
|
@@ -255,7 +269,10 @@ export function emitElementModule(doc, className, elementImports) {
|
|
|
255
269
|
// Emit functions: this.state.fnName = (state) => { body } or imported $src function
|
|
256
270
|
for (const [key, def] of functionEntries) {
|
|
257
271
|
lines.push("");
|
|
258
|
-
if (def
|
|
272
|
+
if ("$expression" in def) {
|
|
273
|
+
const compiled = compileExpression(def.$expression, { statePrefix: "s", eventParam: "e" });
|
|
274
|
+
lines.push(` this.state.${key} = (s, e) => { ${compiled}; };`);
|
|
275
|
+
} else if (def.$src) {
|
|
259
276
|
// $src function — wrap imported function so it receives state
|
|
260
277
|
const args = def.parameters ?? def.arguments ?? ["state"];
|
|
261
278
|
const paramList = args.join(", ");
|
|
@@ -272,7 +289,13 @@ export function emitElementModule(doc, className, elementImports) {
|
|
|
272
289
|
// Emit computed signals — $src or inline body
|
|
273
290
|
for (const [key, def] of computedEntries) {
|
|
274
291
|
lines.push("");
|
|
275
|
-
if (def
|
|
292
|
+
if ("$expression" in def) {
|
|
293
|
+
const compiled = compileExpression(def.$expression, {
|
|
294
|
+
statePrefix: "this.state",
|
|
295
|
+
eventParam: "e",
|
|
296
|
+
});
|
|
297
|
+
lines.push(` this.state.${key} = computed(() => ${compiled});`);
|
|
298
|
+
} else if (def.$src) {
|
|
276
299
|
lines.push(` this.state.${key} = computed(() => ${key}(this.state));`);
|
|
277
300
|
} else {
|
|
278
301
|
lines.push(` this.state.${key} = computed(() => {`);
|
|
@@ -491,6 +514,12 @@ function emitLitNode(def, indent) {
|
|
|
491
514
|
parts.push(
|
|
492
515
|
`@${eventName}="\${(e) => ${refToExpr(/** @type {string} */ (/** @type {JxMutableNode} */ (val).$ref))}(s, e)}"`,
|
|
493
516
|
);
|
|
517
|
+
} else if (val && typeof val === "object" && "$expression" in /** @type {any} */ (val)) {
|
|
518
|
+
const compiled = compileExpression(/** @type {any} */ (val).$expression, {
|
|
519
|
+
statePrefix: "s",
|
|
520
|
+
eventParam: "e",
|
|
521
|
+
});
|
|
522
|
+
parts.push(`@${eventName}="\${(e) => { ${compiled}; }}"`);
|
|
494
523
|
} else if (
|
|
495
524
|
val &&
|
|
496
525
|
typeof val === "object" &&
|
|
@@ -561,6 +590,12 @@ function emitMappedArray(arrayDef, indent) {
|
|
|
561
590
|
parts.push(
|
|
562
591
|
`@${eventName}="\${(e) => ${refToExpr(/** @type {string} */ (/** @type {JxMutableNode} */ (val).$ref))}(s, e)}"`,
|
|
563
592
|
);
|
|
593
|
+
} else if (val && typeof val === "object" && "$expression" in /** @type {any} */ (val)) {
|
|
594
|
+
const compiled = compileExpression(/** @type {any} */ (val).$expression, {
|
|
595
|
+
statePrefix: "s",
|
|
596
|
+
eventParam: "e",
|
|
597
|
+
});
|
|
598
|
+
parts.push(`@${eventName}="\${(e) => { ${compiled}; }}"`);
|
|
564
599
|
}
|
|
565
600
|
}
|
|
566
601
|
|