@jxsuite/compiler 0.28.2 → 0.28.4

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.
@@ -5,7 +5,7 @@
5
5
  * file.
6
6
  */
7
7
 
8
- import $RefParser from "@apidevtools/json-schema-ref-parser";
8
+ import { dereference } from "@apidevtools/json-schema-ref-parser";
9
9
  import { collectServerEntries } from "../shared.ts";
10
10
  import type { JxElement } from "@jxsuite/schema/types";
11
11
 
@@ -30,9 +30,11 @@ export async function compileServer(
30
30
  } = {},
31
31
  ) {
32
32
  const { baseUrl = "/_jx/server" } = opts;
33
- const doc = (await $RefParser.dereference(sourcePath)) as JxElement;
33
+ const doc = (await dereference(sourcePath)) as JxElement;
34
34
  const entries = collectServerEntries(doc);
35
- if (entries.length === 0) return null;
35
+ if (entries.length === 0) {
36
+ return null;
37
+ }
36
38
 
37
39
  const imports = entries
38
40
  .map(({ exportName, src }) => `import { ${exportName} } from '${src}'`)
@@ -73,7 +75,9 @@ export function compileSiteServer(
73
75
  } = {},
74
76
  ) {
75
77
  const { baseUrl = "/_jx/server", adapter = null } = opts;
76
- if (entries.length === 0 && !adapter) return null;
78
+ if (entries.length === 0 && !adapter) {
79
+ return null;
80
+ }
77
81
 
78
82
  const imports = entries
79
83
  .map(({ exportName, src }) => `import { ${exportName} } from '${src}'`)
@@ -82,7 +86,7 @@ export function compileSiteServer(
82
86
  const routes = entries.map(({ exportName }) => buildRoute(exportName, baseUrl)).join("\n");
83
87
 
84
88
  // Workers static assets and Pages advanced mode (_worker.js) both route every request
85
- // through the worker — unmatched paths must fall through to the ASSETS binding.
89
+ // Through the worker — unmatched paths must fall through to the ASSETS binding.
86
90
  const adapterBlock =
87
91
  adapter === "cloudflare-workers" || adapter === "cloudflare-pages"
88
92
  ? "\napp.all('*', (c) => c.env.ASSETS.fetch(c.req.raw))\n"
@@ -6,29 +6,34 @@
6
6
  */
7
7
 
8
8
  import {
9
- isNodeDynamic,
10
- createCompileContext,
11
- resolveStaticValue,
12
9
  buildAttrs,
13
10
  compileStyles,
11
+ createCompileContext,
14
12
  escapeHtml,
13
+ isNodeDynamic,
14
+ resolveStaticValue,
15
15
  } from "../shared.ts";
16
16
  import { emitElementModule } from "./compile-element.ts";
17
- import type { JxStyle, JxMutableNode } from "@jxsuite/schema/types";
17
+ import type { JxDocument, JxMutableNode, JxStyle } from "@jxsuite/schema/types";
18
18
 
19
19
  /**
20
20
  * Compile a static document to HTML, with dynamic subtrees as islands.
21
21
  *
22
- * @param {JxMutableNode | Record<string, any>} raw - Raw JSON document (with $ref pointers
23
- * preserved)
22
+ * @param {JxDocument} raw - Raw JSON document (with $ref pointers preserved)
24
23
  * @param {Record<string, unknown>} opts
25
24
  * @returns {{ html: string; files: { path: string; content: string; tagName: string }[] }}
26
25
  */
27
26
  export function compileStaticPage(
28
- raw: JxMutableNode | Record<string, any>,
29
- opts: Record<string, unknown>,
27
+ raw: JxDocument,
28
+ opts: {
29
+ title?: string;
30
+ reactivitySrc?: string;
31
+ litHtmlSrc?: string;
32
+ projectStyle?: JxStyle | null;
33
+ [key: string]: unknown;
34
+ },
30
35
  ) {
31
- const { title, reactivitySrc, litHtmlSrc } = opts as JxMutableNode;
36
+ const { title = "Jx App", reactivitySrc, litHtmlSrc } = opts;
32
37
 
33
38
  const rootContext = createCompileContext(raw, null, raw.state ?? {}, raw.$media ?? {});
34
39
  const styleBlock = compileStyles(
@@ -45,10 +50,10 @@ export function compileStaticPage(
45
50
  let moduleScripts = "";
46
51
  if (islands.length > 0) {
47
52
  for (const island of islands) {
48
- const moduleContent = emitElementModule(island.def, island.className, []);
53
+ const moduleContent = emitElementModule(island.def as JxDocument, island.className, []);
49
54
  files.push({
50
- path: `_islands/${island.tagName}.js`,
51
55
  content: moduleContent,
56
+ path: `_islands/${island.tagName}.js`,
52
57
  tagName: island.tagName,
53
58
  });
54
59
  }
@@ -82,7 +87,7 @@ export function compileStaticPage(
82
87
  </body>
83
88
  </html>`;
84
89
 
85
- return { html, files };
90
+ return { files, html };
86
91
  }
87
92
 
88
93
  // ─── Node compilation ─────────────────────────────────────────────────────────
@@ -95,7 +100,7 @@ export function compileStaticPage(
95
100
  * @param {boolean} dynamic
96
101
  * @param {JxMutableNode} raw
97
102
  * @param {any} context
98
- * @param {{ def: any; tagName: string; className: string }[]} islands
103
+ * @param {{ def: JxMutableNode; tagName: string; className: string }[]} islands
99
104
  * @returns {string}
100
105
  */
101
106
  function compileNode(
@@ -116,7 +121,9 @@ function compileNode(
116
121
  if (typeof def === "number" || typeof def === "boolean") {
117
122
  return escapeHtml(String(def));
118
123
  }
119
- if (!def || typeof def !== "object") return "";
124
+ if (!def || typeof def !== "object") {
125
+ return "";
126
+ }
120
127
 
121
128
  const nextContext = createCompileContext(
122
129
  raw,
@@ -130,7 +137,7 @@ function compileNode(
130
137
  const tagName = `jx-island-${n}`;
131
138
  const className = `JxIsland${n}`;
132
139
  const elementDef = { ...(raw ?? def), tagName };
133
- islands.push({ def: elementDef, tagName, className });
140
+ islands.push({ className, def: elementDef, tagName });
134
141
  return `<${tagName}></${tagName}>`;
135
142
  }
136
143
 
@@ -148,7 +155,7 @@ function compileNode(
148
155
  * @param {JxMutableNode} def
149
156
  * @param {JxMutableNode} raw
150
157
  * @param {any} context
151
- * @param {{ def: any; tagName: string; className: string }[]} islands
158
+ * @param {{ def: JxMutableNode; tagName: string; className: string }[]} islands
152
159
  * @returns {string}
153
160
  */
154
161
  function buildInnerWithIslands(
@@ -167,13 +174,14 @@ function buildInnerWithIslands(
167
174
  const value = resolveStaticValue(source.textContent, context.scope);
168
175
  return value == null ? "" : escapeHtml(String(value));
169
176
  }
170
- if (source.innerHTML)
177
+ if (source.innerHTML) {
171
178
  return (resolveStaticValue(source.innerHTML, context.scope) as string) ?? source.innerHTML;
179
+ }
172
180
  if (Array.isArray(source.children)) {
173
- const rawChildren = raw?.children;
181
+ const rawChildren = Array.isArray(raw?.children) ? raw.children : undefined;
174
182
  return source.children
175
183
  .map((c, i: number) => {
176
- const child = c as unknown as JxMutableNode;
184
+ const child = c as JxMutableNode;
177
185
  const childDynamic = isNodeDynamic(child);
178
186
  const childRaw = (rawChildren?.[i] ?? c) as JxMutableNode;
179
187
  return compileNode(child, childDynamic, childRaw, context, islands);