@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.
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Compile-static.js — Static HTML compilation
3
3
  *
4
- * Compiles fully static Jx documents to plain HTML/CSS with zero JS. Dynamic child subtrees become
5
- * hydration islands (custom elements).
4
+ * Compiles a document `isDynamic` judged static to plain HTML/CSS with zero JS. `compile()` routes
5
+ * nothing else here: a document with any dynamic node, at any depth, is compiled whole by
6
+ * `compile-client.ts` (compiler.md §8.1, §9.1).
6
7
  */
7
8
 
8
9
  import {
@@ -12,7 +13,6 @@ import {
12
13
  compileStyles,
13
14
  createCompileContext,
14
15
  escapeHtml,
15
- isNodeDynamic,
16
16
  PREFORMATTED_TAGS,
17
17
  pureSchemeOf,
18
18
  resolveStaticTagName,
@@ -20,7 +20,6 @@ import {
20
20
  SELF_CLOSING,
21
21
  } from "../shared.ts";
22
22
  import type { CompileContext } from "../shared.ts";
23
- import { emitElementModule } from "./compile-element.ts";
24
23
  import { isBoolean, isNonNullObject, isNumber, isString } from "@jxsuite/schema/guards";
25
24
  import type { JsonPrimitive, JxDocument, JxMutableNode, JxStyle } from "@jxsuite/schema/types";
26
25
 
@@ -30,67 +29,36 @@ import type { JsonPrimitive, JxDocument, JxMutableNode, JxStyle } from "@jxsuite
30
29
  */
31
30
  type StaticChild = JxMutableNode | JsonPrimitive;
32
31
 
33
- /** A dynamic subtree lifted out of the static page, emitted as its own element module. */
34
- interface Island {
35
- def: JxDocument;
32
+ /** A file a compilation emits beside its HTML; the static target emits none. */
33
+ interface EmittedFile {
34
+ path: string;
35
+ content: string;
36
36
  tagName: string;
37
- className: string;
38
37
  }
39
38
 
40
39
  /**
41
- * Compile a static document to HTML, with dynamic subtrees as islands.
40
+ * Compile a static document to HTML. The page carries no module, import map or script: `files` is
41
+ * always empty, and is returned so every route of `compile()` has one shape.
42
42
  *
43
43
  * @param {JxDocument} raw - Raw JSON document (with $ref pointers preserved)
44
- * @param opts - Page title, CDN sources, project style, pre-paint switch and island `$src`
45
- * rewriting
44
+ * @param opts - Page title, project style and pre-paint switch
46
45
  * @returns {{ html: string; files: { path: string; content: string; tagName: string }[] }}
47
46
  */
48
47
  export function compileStaticPage(
49
48
  raw: JxDocument,
50
49
  opts: {
51
50
  title?: string;
52
- reactivitySrc?: string;
53
- litHtmlSrc?: string;
54
51
  projectStyle?: JxStyle | null;
55
52
  prePaintScheme?: boolean;
56
- /** Rewrite an island's Function-def `$src` specifier to its served URL. */
57
- rewriteSrc?: (specifier: string) => string;
58
53
  },
59
54
  ) {
60
- const { title = "Jx App", reactivitySrc, litHtmlSrc } = opts;
55
+ const { title = "Jx App" } = opts;
61
56
 
62
57
  const rootContext = createCompileContext(raw, null, raw.state ?? {}, raw.$media ?? {});
63
58
  const styleBlock = compileStyles(raw, raw.$media ?? {}, opts.projectStyle ?? null);
64
- const islands: Island[] = [];
65
- const bodyContent = compileNode(raw, false, rootContext, islands);
66
-
67
- /** @type {{ path: string; content: string; tagName: string }[]} */
68
- const files = [];
69
- let importMap = "";
70
- let moduleScripts = "";
71
- if (islands.length > 0) {
72
- for (const island of islands) {
73
- const moduleContent = emitElementModule(island.def, island.className, [], opts.rewriteSrc);
74
- files.push({
75
- content: moduleContent,
76
- path: `_islands/${island.tagName}.js`,
77
- tagName: island.tagName,
78
- });
79
- }
80
- importMap = `<script type="importmap">
81
- {
82
- "imports": {
83
- "@vue/reactivity": "${reactivitySrc}",
84
- "lit-html": "${litHtmlSrc}"
85
- }
86
- }
87
- </script>`;
88
- moduleScripts = files
89
- .map(
90
- (/** @type {{ path: string }} */ f) => `<script type="module" src="./${f.path}"></script>`,
91
- )
92
- .join("\n ");
93
- }
59
+ const bodyContent = compileNode(raw, rootContext);
60
+
61
+ const files: EmittedFile[] = [];
94
62
 
95
63
  // Forced-scheme contract (spec §9.5): restore the persisted scheme before any styles apply
96
64
  const prePaint =
@@ -105,12 +73,11 @@ export function compileStaticPage(
105
73
  <meta charset="utf-8">
106
74
  <meta name="viewport" content="width=device-width, initial-scale=1">
107
75
  <title>${escapeHtml(title)}</title>
108
- ${prePaint}${importMap}
76
+ ${prePaint}
109
77
  ${styleBlock}
110
78
  </head>
111
79
  <body>
112
80
  ${bodyContent}
113
- ${moduleScripts}
114
81
  </body>
115
82
  </html>`;
116
83
 
@@ -120,24 +87,13 @@ export function compileStaticPage(
120
87
  // ─── Node compilation ─────────────────────────────────────────────────────────
121
88
 
122
89
  /**
123
- * Compile a single Jx node to an HTML string. Dynamic nodes become hydration islands; static nodes
124
- * become plain HTML.
125
- *
126
- * The node is read as authored — `$ref` pointers preserved — because an island's element module
127
- * resolves them itself at runtime.
90
+ * Compile a single Jx node to an HTML string.
128
91
  *
129
92
  * @param {StaticChild} def
130
- * @param {boolean} dynamic
131
93
  * @param {CompileContext} context
132
- * @param {Island[]} islands
133
94
  * @returns {string}
134
95
  */
135
- function compileNode(
136
- def: StaticChild,
137
- dynamic: boolean,
138
- context: CompileContext,
139
- islands: Island[],
140
- ): string {
96
+ function compileNode(def: StaticChild, context: CompileContext): string {
141
97
  // String children are text nodes
142
98
  if (isString(def)) {
143
99
  return escapeHtml(def);
@@ -156,18 +112,6 @@ function compileNode(
156
112
  def.$media ?? context.media,
157
113
  );
158
114
 
159
- if (dynamic) {
160
- const n = islands.length;
161
- const tagName = `jx-island-${n}`;
162
- const className = `JxIsland${n}`;
163
- /* SAFETY: JxDocument is the element-node shape with `tagName` narrowed to a literal string,
164
- and the island's `tagName` is the literal `jx-island-N` assigned here; every other member is
165
- the authored node's own, carried over unchanged. */
166
- const elementDef = { ...def, tagName } as JxDocument;
167
- islands.push({ className, def: elementDef, tagName });
168
- return `<${tagName}></${tagName}>`;
169
- }
170
-
171
115
  // Resolved against the same scope the attributes are, so the prerendered element and its
172
116
  // Attributes come from one decision rather than two.
173
117
  const tag = resolveStaticTagName(def.tagName, nextContext.scope);
@@ -181,25 +125,19 @@ function compileNode(
181
125
 
182
126
  // `white-space` inherits, so once inside a <pre> the whole subtree stays whitespace-significant.
183
127
  const preformatted = context.preformatted === true || PREFORMATTED_TAGS.has(tag);
184
- const inner = buildInnerWithIslands(def, { ...nextContext, preformatted }, islands);
128
+ const inner = buildStaticInner(def, { ...nextContext, preformatted });
185
129
 
186
130
  return `<${tag}${attrs}>${inner}</${tag}>`;
187
131
  }
188
132
 
189
133
  /**
190
- * Build the inner HTML (textContent or children) for a node. For children, emit islands only for
191
- * those that are actually dynamic.
134
+ * Build the inner HTML (textContent, innerHTML or children) for a node.
192
135
  *
193
136
  * @param {JxMutableNode} def
194
137
  * @param {CompileContext} context
195
- * @param {Island[]} islands
196
138
  * @returns {string}
197
139
  */
198
- function buildInnerWithIslands(
199
- def: JxMutableNode,
200
- context: CompileContext,
201
- islands: Island[],
202
- ): string {
140
+ function buildStaticInner(def: JxMutableNode, context: CompileContext): string {
203
141
  if (def.textContent !== undefined) {
204
142
  const value = resolveStaticValue(def.textContent, context.scope);
205
143
  return value == null ? "" : escapeHtml(String(value));
@@ -211,7 +149,7 @@ function buildInnerWithIslands(
211
149
  }
212
150
  if (Array.isArray(def.children)) {
213
151
  return def.children
214
- .map((child) => compileNode(child, isNodeDynamic(child), context, islands))
152
+ .map((child) => compileNode(child, context))
215
153
  .join(childSeparator(context.preformatted));
216
154
  }
217
155
  return "";