@jxsuite/compiler 9.1.0 → 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.
@@ -38,6 +38,7 @@ import {
38
38
  compileExpression,
39
39
  compileStatements,
40
40
  compileStyles,
41
+ contentPropertyText,
41
42
  createCompileContext,
42
43
  emitFormulaFn,
43
44
  emitRequestFetch,
@@ -47,9 +48,20 @@ import {
47
48
  isRefObject,
48
49
  isSchemaOnly,
49
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,
50
61
  LIT_PART_GUARD,
51
62
  PREFORMATTED_TAGS,
52
63
  pureSchemeOf,
64
+ isExpandedInstance,
53
65
  resolveStaticValue,
54
66
  serverCallHelperSource,
55
67
  collectFunctionImports,
@@ -75,8 +87,9 @@ import {
75
87
  isTagExpression,
76
88
  tagNameCandidates,
77
89
  } from "@jxsuite/schema/guards";
78
- import type { ClientClasses, CompileContext, SrcImports } from "../shared.ts";
90
+ import type { ClientClasses, CompileContext, MappedArrayFeatures, SrcImports } from "../shared.ts";
79
91
  import {
92
+ canonicalMapRef,
80
93
  escapeToken,
81
94
  objectKey,
82
95
  refAccessor,
@@ -157,6 +170,8 @@ export function compileClient(
157
170
  title?: string;
158
171
  reactivitySrc?: string;
159
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;
160
175
  modulePath?: string;
161
176
  projectStyle?: JxStyle | null;
162
177
  prePaintScheme?: boolean;
@@ -365,13 +380,15 @@ export function compileClient(
365
380
  reactivitySrc,
366
381
  opts.rewriteSrc,
367
382
  clientClasses,
383
+ mappedArrayFeatures(raw),
368
384
  );
369
385
 
370
- // Build importmap entries — always include lit-html since compiled custom elements need it
371
- const importmapEntries = [
372
- ` "@vue/reactivity": "${reactivitySrc}"`,
373
- ` "lit-html": "${litHtmlSrc}"`,
374
- ];
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
+ });
375
392
 
376
393
  // Forced-scheme contract (spec §9.5): restore the persisted scheme before any styles apply
377
394
  const prePaint =
@@ -386,13 +403,7 @@ export function compileClient(
386
403
  <meta charset="utf-8">
387
404
  <meta name="viewport" content="width=device-width, initial-scale=1">
388
405
  <title>${escapeHtml(title)}</title>
389
- ${prePaint}<script type="importmap">
390
- {
391
- "imports": {
392
- ${importmapEntries.join(",\n")}
393
- }
394
- }
395
- </script>
406
+ ${prePaint}${importMap}
396
407
  ${styleBlock}
397
408
  <script type="module" src="./${modulePath}"></script>
398
409
  </head>
@@ -657,6 +668,8 @@ function buildClientNode(
657
668
  const bindAttrStrSwitch = ` ${bindAttrs.join(" ")}`;
658
669
  return `<${tag}${staticAttrs} data-bind${bindAttrStrSwitch}></${tag}>`;
659
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);
660
673
  if (source.textContent !== undefined && !needsBind) {
661
674
  const value = resolveStaticValue(source.textContent, nextContext.scope);
662
675
  inner = value == null ? "" : escapeHtml(String(value));
@@ -666,8 +679,13 @@ function buildClientNode(
666
679
  } else if (source.innerHTML) {
667
680
  // ResolveStaticValue may return null if innerHTML contains `${` from rendered content
668
681
  // (e.g., code examples) that isn't an actual template expression. Fall back to raw value.
669
- const resolved = resolveStaticValue(source.innerHTML, nextContext.scope);
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);
670
686
  inner = resolved == null ? source.innerHTML : String(resolved);
687
+ } else if (contentText !== undefined) {
688
+ inner = contentText;
671
689
  } else if (isMappedArray(source.children)) {
672
690
  // ─── Mapped array → lit-html render binding ───
673
691
  counter.needsLit = true;
@@ -675,17 +693,8 @@ function buildClientNode(
675
693
  counter.l += 1;
676
694
  const arrayDef = source.children;
677
695
 
678
- // Resolve items source expression
679
- const itemsExpr = isRefObject(arrayDef.items)
680
- ? stateRead(arrayDef.items.$ref)
681
- : JSON.stringify(arrayDef.items);
682
-
683
- // Compile the map template to a lit-html template string
684
- const litTemplate = emitLitMapTemplate(arrayDef.map);
685
- bindings.set(
686
- listKey,
687
- `() => (${itemsExpr} ?? []).map((item, index) => html\`${litTemplate}\`)`,
688
- );
696
+ // The list, arranged and keyed as the document asks, over the map template (`emitClientList`).
697
+ bindings.set(listKey, `() => ${emitClientList(arrayDef)}`);
689
698
 
690
699
  bindAttrs.push(`:render="${listKey}"`);
691
700
  needsBind = true;
@@ -813,6 +822,11 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
813
822
  attrs += ` ${k}="${mapRefsToLit(v)}"`;
814
823
  continue;
815
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
+ }
816
830
  /* A boolean is the attribute's PRESENCE or its text depending on the family its NAME puts it
817
831
  in — `booleanAttrValue` owns that judgement, and the static emitter in `shared.ts` and the
818
832
  runtime's `applyAttributes` both defer to it, so a prerendered page cannot change meaning
@@ -900,9 +914,31 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
900
914
  }
901
915
  }
902
916
 
903
- // Non-reserved properties that render as attributes
904
- if (def.contentEditable) {
905
- attrs += ` contenteditable="${def.contentEditable}"`;
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
+ }
906
942
  }
907
943
 
908
944
  /* A row can hold a component instance, whose children its slots redistribute; a child part
@@ -916,8 +952,10 @@ function emitLitMapTemplate(def: JxMutableNode | null | undefined, preformatted
916
952
  if (isTemplateString(tc)) {
917
953
  inner = `${mapRefsToLit(tc)}${guard}`;
918
954
  } else if (isRefObject(def.textContent)) {
919
- const path = refToBindingKey(def.textContent.$ref);
920
- inner = `\${state.${path}}${guard}`;
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}`;
921
959
  } else {
922
960
  inner = escapeHtml(tc);
923
961
  }
@@ -964,17 +1002,68 @@ function emitChildLit(c: JxMutableNode | JsonPrimitive, preformatted = false): s
964
1002
  }
965
1003
 
966
1004
  /**
967
- * Emit a lit-html `${(items ?? []).map(...)}` hole for a mapped array, expanding its template
968
- * inline among sibling nodes (wrapper-less).
1005
+ * Emit a lit-html hole for a mapped array, expanding its template inline among sibling nodes
1006
+ * (wrapper-less).
969
1007
  *
970
1008
  * @param {RepeaterSlot} arrayDef
971
1009
  * @returns {string}
972
1010
  */
973
1011
  function emitArrayHole(arrayDef: RepeaterSlot): string {
974
- const { items } = arrayDef;
975
- const itemsExpr = isRefObject(items) ? stateRead(items.$ref) : JSON.stringify(items ?? []);
976
- const tpl = emitLitMapTemplate(arrayDef.map);
977
- return `\${(${itemsExpr} ?? []).map((item, index) => html\`${tpl}\`)}`;
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})`;
978
1067
  }
979
1068
 
980
1069
  /**
@@ -1024,6 +1113,7 @@ function emitClientModule(
1024
1113
  _reactivitySrc: string,
1025
1114
  rewriteSrc: ((specifier: string) => string) | undefined,
1026
1115
  clientClasses: ClientClasses,
1116
+ lists: MappedArrayFeatures,
1027
1117
  ) {
1028
1118
  const lines: string[] = [];
1029
1119
  const { needsLit, textParts } = counter;
@@ -1036,12 +1126,19 @@ function emitClientModule(
1036
1126
  if (needsComputed) {
1037
1127
  reactivityImports.push("computed");
1038
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
+ }
1039
1133
  lines.push(`import { ${reactivityImports.join(", ")} } from '@vue/reactivity';`);
1040
1134
 
1041
1135
  // Lit-html imports (only when arrays are present)
1042
1136
  if (needsLit) {
1043
1137
  lines.push("import { html, render } from 'lit-html';");
1044
1138
  }
1139
+ if (lists.keyed) {
1140
+ lines.push(...KEYED_LIST_IMPORTS);
1141
+ }
1045
1142
 
1046
1143
  // $src imports — bundleable specifiers (npm:…, ./relative) are rewritten to their /assets/
1047
1144
  // Bundle URL; the site build bundles them after all documents compile (spec.md §12).
@@ -1051,6 +1148,14 @@ function emitClientModule(
1051
1148
  /* The boolean-attribute rule, inlined because this module loads with `@vue/reactivity` and
1052
1149
  nothing else — see `attrHelperSource`. The hydration script below is its only caller. */
1053
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
+ }
1054
1159
  if (serverCalls.length > 0) {
1055
1160
  lines.push("", serverCallHelperSource());
1056
1161
  }
@@ -1283,7 +1388,8 @@ function isNonDeclarationValue(
1283
1388
  * @param {string} ref
1284
1389
  * @returns {string}
1285
1390
  */
1286
- function mapRefToClientExpr(ref: string) {
1391
+ function mapRefToClientExpr(written: string) {
1392
+ const ref = canonicalMapRef(written);
1287
1393
  if (ref.startsWith("$map/")) {
1288
1394
  // The loop variable is an emitted identifier, so its first segment is the base, not a member.
1289
1395
  const [head, ...rest] = refSegments(ref.slice("$map/".length));