@sigil-dev/compiler 0.3.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/README.md +3 -0
- package/index.ts +4 -0
- package/jsr.json +8 -0
- package/package.json +24 -0
- package/src/babel/handlers/dom/derived.ts +32 -0
- package/src/babel/handlers/dom/effect.ts +15 -0
- package/src/babel/handlers/dom/state.ts +78 -0
- package/src/babel/handlers/ssr/derived.ts +26 -0
- package/src/babel/handlers/ssr/state.ts +10 -0
- package/src/babel/index.ts +239 -0
- package/src/babel/jsx/anchor-mount.ts +366 -0
- package/src/babel/jsx/children.ts +124 -0
- package/src/babel/jsx/element.ts +520 -0
- package/src/babel/jsx/fragment.ts +279 -0
- package/src/babel/jsx/index.ts +2 -0
- package/src/babel/jsx/keyed-list.ts +278 -0
- package/src/babel/jsx/ssr.ts +309 -0
- package/src/babel/jsx/text-node.ts +192 -0
- package/src/babel/jsx/utils.ts +124 -0
- package/src/babel/util/bind.ts +75 -0
- package/src/babel/util/css.ts +151 -0
- package/src/babel/util/helpers.ts +32 -0
- package/src/babel/util/magic.ts +5 -0
- package/src/bun-plugin.ts +45 -0
- package/src/vite/index.ts +53 -0
- package/test/components.test.ts +129 -0
- package/test/fragments.test.ts +90 -0
- package/test/helpers/transform.ts +26 -0
- package/test/hydration.test.ts +147 -0
- package/test/jsx.test.ts +120 -0
- package/test/keyed-lists.test.ts +88 -0
- package/test/reactivity.test.ts +79 -0
- package/test/scoped-css.test.ts +129 -0
- package/test/ssr.test.ts +130 -0
- package/tsconfig.json +30 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { types as t } from "@babel/core";
|
|
2
|
+
import { buildAnchorMount } from "./anchor-mount";
|
|
3
|
+
import { processElement } from "./element";
|
|
4
|
+
import { buildKeyedList, findKeyedMapExpr } from "./keyed-list";
|
|
5
|
+
import { buildTextNode } from "./text-node";
|
|
6
|
+
import { containsSignal, isPrimitive } from "./utils";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Process a JSX fragment (<>...</>).
|
|
10
|
+
* In DOM mode: creates a DocumentFragment and appends all children to it.
|
|
11
|
+
* In hydrate mode (inside element): children claim from parent's node pool; no fragment created.
|
|
12
|
+
* In hydrate mode (top-level, no parentVar): creates a DocumentFragment so the IIFE returns
|
|
13
|
+
* a valid node for SPA navigation (slot.replaceChildren). SSR hydration ignores the return value.
|
|
14
|
+
*/
|
|
15
|
+
export function processFragment(
|
|
16
|
+
node: t.JSXFragment,
|
|
17
|
+
statements: t.Statement[],
|
|
18
|
+
genId: () => string,
|
|
19
|
+
signals: Set<string>,
|
|
20
|
+
hash?: string,
|
|
21
|
+
hydrate?: boolean,
|
|
22
|
+
nodesVar?: string,
|
|
23
|
+
parentVar?: string,
|
|
24
|
+
): string {
|
|
25
|
+
const varName = genId();
|
|
26
|
+
const currentNodesVar = nodesVar ?? "__nodes";
|
|
27
|
+
|
|
28
|
+
// Where to append children in DOM / SPA mode:
|
|
29
|
+
// DOM mode → varName (the DocumentFragment)
|
|
30
|
+
// hydrate inside elem → parentVar (the real parent element)
|
|
31
|
+
// hydrate top-level → varName (a DocumentFragment we create below)
|
|
32
|
+
const effectiveParent = hydrate ? (parentVar ?? varName) : varName;
|
|
33
|
+
|
|
34
|
+
// Create a DocumentFragment when:
|
|
35
|
+
// - DOM mode: always (it IS the container)
|
|
36
|
+
// - Hydrate top-level (no parentVar): needed so the IIFE can return a real node for SPA nav
|
|
37
|
+
// (SSR hydration ignores the return value, so an empty fragment is harmless)
|
|
38
|
+
if (!hydrate || !parentVar) {
|
|
39
|
+
statements.push(
|
|
40
|
+
t.variableDeclaration("const", [
|
|
41
|
+
t.variableDeclarator(
|
|
42
|
+
t.identifier(varName),
|
|
43
|
+
t.callExpression(
|
|
44
|
+
t.memberExpression(
|
|
45
|
+
t.identifier("document"),
|
|
46
|
+
t.identifier("createDocumentFragment"),
|
|
47
|
+
),
|
|
48
|
+
[],
|
|
49
|
+
),
|
|
50
|
+
),
|
|
51
|
+
]),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Process each child and append to fragment (or claim in hydrate mode)
|
|
56
|
+
for (const child of node.children) {
|
|
57
|
+
if (t.isJSXElement(child)) {
|
|
58
|
+
const childVar = processElement(
|
|
59
|
+
child,
|
|
60
|
+
statements,
|
|
61
|
+
genId,
|
|
62
|
+
signals,
|
|
63
|
+
hash,
|
|
64
|
+
hydrate,
|
|
65
|
+
currentNodesVar,
|
|
66
|
+
hydrate ? effectiveParent : undefined,
|
|
67
|
+
);
|
|
68
|
+
if (!hydrate) {
|
|
69
|
+
statements.push(
|
|
70
|
+
t.expressionStatement(
|
|
71
|
+
t.callExpression(
|
|
72
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
73
|
+
[t.identifier(childVar)],
|
|
74
|
+
),
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
} else if (t.isJSXText(child)) {
|
|
79
|
+
const text = child.value.trim();
|
|
80
|
+
if (!text) continue;
|
|
81
|
+
if (hydrate) {
|
|
82
|
+
// SSR hydration: text already in DOM from SSR, do nothing.
|
|
83
|
+
// SPA navigation: pool is empty, create text node and append to effectiveParent.
|
|
84
|
+
const poolLen = t.memberExpression(
|
|
85
|
+
t.identifier(currentNodesVar),
|
|
86
|
+
t.identifier("length"),
|
|
87
|
+
);
|
|
88
|
+
statements.push(
|
|
89
|
+
t.ifStatement(
|
|
90
|
+
t.binaryExpression("===", poolLen, t.numericLiteral(0)),
|
|
91
|
+
t.expressionStatement(
|
|
92
|
+
t.callExpression(
|
|
93
|
+
t.memberExpression(t.identifier(effectiveParent), t.identifier("append")),
|
|
94
|
+
[
|
|
95
|
+
t.callExpression(
|
|
96
|
+
t.memberExpression(
|
|
97
|
+
t.identifier("document"),
|
|
98
|
+
t.identifier("createTextNode"),
|
|
99
|
+
),
|
|
100
|
+
[t.stringLiteral(text)],
|
|
101
|
+
),
|
|
102
|
+
],
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
} else {
|
|
108
|
+
statements.push(
|
|
109
|
+
t.expressionStatement(
|
|
110
|
+
t.callExpression(
|
|
111
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
112
|
+
[
|
|
113
|
+
t.callExpression(
|
|
114
|
+
t.memberExpression(
|
|
115
|
+
t.identifier("document"),
|
|
116
|
+
t.identifier("createTextNode"),
|
|
117
|
+
),
|
|
118
|
+
[t.stringLiteral(text)],
|
|
119
|
+
),
|
|
120
|
+
],
|
|
121
|
+
),
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
} else if (t.isJSXExpressionContainer(child)) {
|
|
126
|
+
if (t.isJSXEmptyExpression(child.expression)) continue;
|
|
127
|
+
const expr = child.expression as t.Expression;
|
|
128
|
+
if (t.isJSXElement(expr)) {
|
|
129
|
+
const childVar = processElement(
|
|
130
|
+
expr,
|
|
131
|
+
statements,
|
|
132
|
+
genId,
|
|
133
|
+
signals,
|
|
134
|
+
hash,
|
|
135
|
+
hydrate,
|
|
136
|
+
currentNodesVar,
|
|
137
|
+
hydrate ? effectiveParent : undefined,
|
|
138
|
+
);
|
|
139
|
+
if (!hydrate) {
|
|
140
|
+
statements.push(
|
|
141
|
+
t.expressionStatement(
|
|
142
|
+
t.callExpression(
|
|
143
|
+
t.memberExpression(
|
|
144
|
+
t.identifier(varName),
|
|
145
|
+
t.identifier("append"),
|
|
146
|
+
),
|
|
147
|
+
[t.identifier(childVar)],
|
|
148
|
+
),
|
|
149
|
+
),
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
} else if (t.isJSXFragment(expr)) {
|
|
153
|
+
const childVar = processFragment(
|
|
154
|
+
expr,
|
|
155
|
+
statements,
|
|
156
|
+
genId,
|
|
157
|
+
signals,
|
|
158
|
+
hash,
|
|
159
|
+
hydrate,
|
|
160
|
+
currentNodesVar,
|
|
161
|
+
hydrate ? effectiveParent : undefined,
|
|
162
|
+
);
|
|
163
|
+
if (!hydrate) {
|
|
164
|
+
statements.push(
|
|
165
|
+
t.expressionStatement(
|
|
166
|
+
t.callExpression(
|
|
167
|
+
t.memberExpression(
|
|
168
|
+
t.identifier(varName),
|
|
169
|
+
t.identifier("append"),
|
|
170
|
+
),
|
|
171
|
+
[t.identifier(childVar)],
|
|
172
|
+
),
|
|
173
|
+
),
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
} else if (containsSignal(expr, signals)) {
|
|
177
|
+
if (isPrimitive(expr)) {
|
|
178
|
+
buildTextNode(
|
|
179
|
+
varName,
|
|
180
|
+
expr,
|
|
181
|
+
statements,
|
|
182
|
+
genId,
|
|
183
|
+
hydrate,
|
|
184
|
+
currentNodesVar,
|
|
185
|
+
hydrate ? effectiveParent : undefined,
|
|
186
|
+
);
|
|
187
|
+
} else {
|
|
188
|
+
// Check for keyed list pattern
|
|
189
|
+
const keyed = findKeyedMapExpr(expr, signals);
|
|
190
|
+
if (keyed) {
|
|
191
|
+
buildKeyedList(
|
|
192
|
+
varName,
|
|
193
|
+
keyed,
|
|
194
|
+
statements,
|
|
195
|
+
genId,
|
|
196
|
+
signals,
|
|
197
|
+
processElement,
|
|
198
|
+
hash,
|
|
199
|
+
hydrate,
|
|
200
|
+
currentNodesVar,
|
|
201
|
+
hydrate ? effectiveParent : undefined,
|
|
202
|
+
);
|
|
203
|
+
} else {
|
|
204
|
+
buildAnchorMount(
|
|
205
|
+
varName,
|
|
206
|
+
expr,
|
|
207
|
+
statements,
|
|
208
|
+
genId,
|
|
209
|
+
hydrate,
|
|
210
|
+
currentNodesVar,
|
|
211
|
+
hydrate ? effectiveParent : undefined,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
} else {
|
|
216
|
+
if (hydrate) {
|
|
217
|
+
// Non-reactive expression in hydrate fragment
|
|
218
|
+
const claimCommentArgs: t.Expression[] = [
|
|
219
|
+
t.identifier(currentNodesVar),
|
|
220
|
+
t.stringLiteral("g"),
|
|
221
|
+
t.identifier(effectiveParent),
|
|
222
|
+
];
|
|
223
|
+
const poolLen = t.memberExpression(
|
|
224
|
+
t.identifier(currentNodesVar),
|
|
225
|
+
t.identifier("length"),
|
|
226
|
+
);
|
|
227
|
+
const claimG = t.expressionStatement(
|
|
228
|
+
t.callExpression(t.identifier("claimComment"), claimCommentArgs),
|
|
229
|
+
);
|
|
230
|
+
const claimSlashG = t.expressionStatement(
|
|
231
|
+
t.callExpression(t.identifier("claimComment"), [
|
|
232
|
+
t.identifier(currentNodesVar),
|
|
233
|
+
t.stringLiteral("/g"),
|
|
234
|
+
t.identifier(effectiveParent),
|
|
235
|
+
]),
|
|
236
|
+
);
|
|
237
|
+
// SPA navigation: pool empty, insert into effectiveParent
|
|
238
|
+
const elseBranch = t.blockStatement([
|
|
239
|
+
t.expressionStatement(
|
|
240
|
+
t.callExpression(t.identifier("insert"), [
|
|
241
|
+
t.identifier(effectiveParent),
|
|
242
|
+
expr,
|
|
243
|
+
]),
|
|
244
|
+
),
|
|
245
|
+
]);
|
|
246
|
+
statements.push(
|
|
247
|
+
t.ifStatement(
|
|
248
|
+
t.binaryExpression(">", poolLen, t.numericLiteral(0)),
|
|
249
|
+
t.blockStatement([claimG, claimSlashG]),
|
|
250
|
+
elseBranch,
|
|
251
|
+
),
|
|
252
|
+
);
|
|
253
|
+
} else {
|
|
254
|
+
statements.push(
|
|
255
|
+
t.expressionStatement(
|
|
256
|
+
t.callExpression(
|
|
257
|
+
t.memberExpression(
|
|
258
|
+
t.identifier(varName),
|
|
259
|
+
t.identifier("append"),
|
|
260
|
+
),
|
|
261
|
+
[
|
|
262
|
+
t.callExpression(
|
|
263
|
+
t.memberExpression(
|
|
264
|
+
t.identifier("document"),
|
|
265
|
+
t.identifier("createTextNode"),
|
|
266
|
+
),
|
|
267
|
+
[expr],
|
|
268
|
+
),
|
|
269
|
+
],
|
|
270
|
+
),
|
|
271
|
+
),
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return varName;
|
|
279
|
+
}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { types as t } from "@babel/core";
|
|
2
|
+
import { containsSignal } from "./utils";
|
|
3
|
+
|
|
4
|
+
export type ProcessElementFn = (
|
|
5
|
+
node: t.JSXElement,
|
|
6
|
+
statements: t.Statement[],
|
|
7
|
+
genId: () => string,
|
|
8
|
+
signals: Set<string>,
|
|
9
|
+
hash?: string,
|
|
10
|
+
hydrate?: boolean,
|
|
11
|
+
nodesVar?: string,
|
|
12
|
+
) => string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Detect pattern: signal.map(item => <Tag key={expr} />)
|
|
16
|
+
* Returns info needed to generate keyed list code, or null.
|
|
17
|
+
*/
|
|
18
|
+
export function findKeyedMapExpr(
|
|
19
|
+
expr: t.Expression,
|
|
20
|
+
signals: Set<string>,
|
|
21
|
+
): {
|
|
22
|
+
callExpr: t.CallExpression;
|
|
23
|
+
keyExpr: t.Expression;
|
|
24
|
+
itemParam: t.Identifier;
|
|
25
|
+
jsxBody: t.JSXElement;
|
|
26
|
+
signalExpr: t.Expression;
|
|
27
|
+
} | null {
|
|
28
|
+
if (!t.isCallExpression(expr)) return null;
|
|
29
|
+
const callee = expr.callee;
|
|
30
|
+
if (!t.isMemberExpression(callee)) return null;
|
|
31
|
+
const prop = callee.property;
|
|
32
|
+
if (!t.isIdentifier(prop) || prop.name !== "map") return null;
|
|
33
|
+
|
|
34
|
+
const obj = callee.object;
|
|
35
|
+
if (!t.isExpression(obj)) return null;
|
|
36
|
+
if (!containsSignal(obj, signals)) return null;
|
|
37
|
+
|
|
38
|
+
if (expr.arguments.length !== 1) return null;
|
|
39
|
+
const arrowFn = expr.arguments[0];
|
|
40
|
+
if (!t.isArrowFunctionExpression(arrowFn)) return null;
|
|
41
|
+
if (arrowFn.params.length !== 1) return null;
|
|
42
|
+
const itemParam = arrowFn.params[0];
|
|
43
|
+
if (!t.isIdentifier(itemParam)) return null;
|
|
44
|
+
|
|
45
|
+
const body = arrowFn.body;
|
|
46
|
+
if (!t.isJSXElement(body)) return null;
|
|
47
|
+
|
|
48
|
+
const keyAttr = body.openingElement.attributes.find(
|
|
49
|
+
(a) =>
|
|
50
|
+
t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && a.name.name === "key",
|
|
51
|
+
);
|
|
52
|
+
if (!keyAttr || !t.isJSXAttribute(keyAttr)) return null;
|
|
53
|
+
if (!t.isJSXExpressionContainer(keyAttr.value)) return null;
|
|
54
|
+
|
|
55
|
+
const keyExpr = keyAttr.value.expression as t.Expression;
|
|
56
|
+
|
|
57
|
+
return { callExpr: expr, keyExpr, itemParam, jsxBody: body, signalExpr: obj };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Generate keyed list code using reconcile().
|
|
62
|
+
* In hydrate mode, uses hydrateKeyedList() to claim server-rendered nodes.
|
|
63
|
+
*/
|
|
64
|
+
export function buildKeyedList(
|
|
65
|
+
varName: string,
|
|
66
|
+
keyed: NonNullable<ReturnType<typeof findKeyedMapExpr>>,
|
|
67
|
+
statements: t.Statement[],
|
|
68
|
+
genId: () => string,
|
|
69
|
+
signals: Set<string>,
|
|
70
|
+
processElement: ProcessElementFn,
|
|
71
|
+
hash?: string,
|
|
72
|
+
hydrate?: boolean,
|
|
73
|
+
nodesVar?: string,
|
|
74
|
+
parentVar?: string,
|
|
75
|
+
): void {
|
|
76
|
+
const anchorVar = genId();
|
|
77
|
+
const keyMapVar = genId();
|
|
78
|
+
|
|
79
|
+
// Strip key prop from JSX before processing.
|
|
80
|
+
// In SSR mode, the Babel plugin already converted key → data-key.
|
|
81
|
+
// In DOM/hydrate mode, we don't need either.
|
|
82
|
+
keyed.jsxBody.openingElement.attributes =
|
|
83
|
+
keyed.jsxBody.openingElement.attributes.filter(
|
|
84
|
+
(a) =>
|
|
85
|
+
!(
|
|
86
|
+
t.isJSXAttribute(a) &&
|
|
87
|
+
t.isJSXIdentifier(a.name) &&
|
|
88
|
+
(a.name.name === "key" || a.name.name === "data-key")
|
|
89
|
+
),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
if (hydrate) {
|
|
93
|
+
// ─── Hydration path: use hydrateKeyedList() ───
|
|
94
|
+
// Strip key prop from JSX before processing
|
|
95
|
+
keyed.jsxBody.openingElement.attributes =
|
|
96
|
+
keyed.jsxBody.openingElement.attributes.filter(
|
|
97
|
+
(a) =>
|
|
98
|
+
!(
|
|
99
|
+
t.isJSXAttribute(a) &&
|
|
100
|
+
t.isJSXIdentifier(a.name) &&
|
|
101
|
+
a.name.name === "key"
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// Process the JSX element to get createNode body
|
|
106
|
+
const createNodeStmts: t.Statement[] = [];
|
|
107
|
+
const jsxVar = processElement(
|
|
108
|
+
keyed.jsxBody,
|
|
109
|
+
createNodeStmts,
|
|
110
|
+
genId,
|
|
111
|
+
signals,
|
|
112
|
+
hash,
|
|
113
|
+
hydrate,
|
|
114
|
+
nodesVar,
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// const { anchor, keyMap } = hydrateKeyedList(__nodes, signal, keyFn, createNode, parent)
|
|
118
|
+
const resultVar = genId();
|
|
119
|
+
const hydrateArgs: t.Expression[] = [
|
|
120
|
+
t.identifier(nodesVar ?? "__nodes"),
|
|
121
|
+
keyed.signalExpr,
|
|
122
|
+
t.arrowFunctionExpression([keyed.itemParam], keyed.keyExpr),
|
|
123
|
+
t.arrowFunctionExpression(
|
|
124
|
+
[keyed.itemParam],
|
|
125
|
+
t.blockStatement([
|
|
126
|
+
...createNodeStmts,
|
|
127
|
+
t.returnStatement(t.identifier(jsxVar)),
|
|
128
|
+
]),
|
|
129
|
+
),
|
|
130
|
+
];
|
|
131
|
+
if (hydrate && parentVar) hydrateArgs.push(t.identifier(parentVar));
|
|
132
|
+
statements.push(
|
|
133
|
+
t.variableDeclaration("const", [
|
|
134
|
+
t.variableDeclarator(
|
|
135
|
+
t.identifier(resultVar),
|
|
136
|
+
t.callExpression(t.identifier("hydrateKeyedList"), hydrateArgs),
|
|
137
|
+
),
|
|
138
|
+
]),
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// const anchor = result.anchor
|
|
142
|
+
statements.push(
|
|
143
|
+
t.variableDeclaration("const", [
|
|
144
|
+
t.variableDeclarator(
|
|
145
|
+
t.identifier(anchorVar),
|
|
146
|
+
t.memberExpression(t.identifier(resultVar), t.identifier("anchor")),
|
|
147
|
+
),
|
|
148
|
+
]),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// const keyMap = result.keyMap
|
|
152
|
+
statements.push(
|
|
153
|
+
t.variableDeclaration("const", [
|
|
154
|
+
t.variableDeclarator(
|
|
155
|
+
t.identifier(keyMapVar),
|
|
156
|
+
t.memberExpression(t.identifier(resultVar), t.identifier("keyMap")),
|
|
157
|
+
),
|
|
158
|
+
]),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// createEffect(() => reconcile(anchor, keyMap, signal, keyFn, createNode))
|
|
162
|
+
statements.push(
|
|
163
|
+
t.expressionStatement(
|
|
164
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
165
|
+
t.arrowFunctionExpression(
|
|
166
|
+
[],
|
|
167
|
+
t.blockStatement([
|
|
168
|
+
t.expressionStatement(
|
|
169
|
+
t.callExpression(t.identifier("reconcile"), [
|
|
170
|
+
t.identifier(anchorVar),
|
|
171
|
+
t.identifier(keyMapVar),
|
|
172
|
+
keyed.signalExpr,
|
|
173
|
+
t.arrowFunctionExpression([keyed.itemParam], keyed.keyExpr),
|
|
174
|
+
t.arrowFunctionExpression(
|
|
175
|
+
[keyed.itemParam],
|
|
176
|
+
t.blockStatement([
|
|
177
|
+
...createNodeStmts,
|
|
178
|
+
t.returnStatement(t.identifier(jsxVar)),
|
|
179
|
+
]),
|
|
180
|
+
),
|
|
181
|
+
]),
|
|
182
|
+
),
|
|
183
|
+
]),
|
|
184
|
+
),
|
|
185
|
+
]),
|
|
186
|
+
),
|
|
187
|
+
);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ─── DOM path (existing code) ───
|
|
192
|
+
|
|
193
|
+
// const _anchor = document.createComment('')
|
|
194
|
+
statements.push(
|
|
195
|
+
t.variableDeclaration("const", [
|
|
196
|
+
t.variableDeclarator(
|
|
197
|
+
t.identifier(anchorVar),
|
|
198
|
+
t.callExpression(
|
|
199
|
+
t.memberExpression(
|
|
200
|
+
t.identifier("document"),
|
|
201
|
+
t.identifier("createComment"),
|
|
202
|
+
),
|
|
203
|
+
[t.stringLiteral("")],
|
|
204
|
+
),
|
|
205
|
+
),
|
|
206
|
+
]),
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
// parent.append(_anchor)
|
|
210
|
+
statements.push(
|
|
211
|
+
t.expressionStatement(
|
|
212
|
+
t.callExpression(
|
|
213
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
214
|
+
[t.identifier(anchorVar)],
|
|
215
|
+
),
|
|
216
|
+
),
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
// const _keyMap = new Map()
|
|
220
|
+
statements.push(
|
|
221
|
+
t.variableDeclaration("const", [
|
|
222
|
+
t.variableDeclarator(
|
|
223
|
+
t.identifier(keyMapVar),
|
|
224
|
+
t.newExpression(t.identifier("Map"), []),
|
|
225
|
+
),
|
|
226
|
+
]),
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
// Strip key prop from JSX before processing
|
|
230
|
+
keyed.jsxBody.openingElement.attributes =
|
|
231
|
+
keyed.jsxBody.openingElement.attributes.filter(
|
|
232
|
+
(a) =>
|
|
233
|
+
!(
|
|
234
|
+
t.isJSXAttribute(a) &&
|
|
235
|
+
t.isJSXIdentifier(a.name) &&
|
|
236
|
+
a.name.name === "key"
|
|
237
|
+
),
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
// Process the JSX element to get createNode body
|
|
241
|
+
const createNodeStmts: t.Statement[] = [];
|
|
242
|
+
const jsxVar = processElement(
|
|
243
|
+
keyed.jsxBody,
|
|
244
|
+
createNodeStmts,
|
|
245
|
+
genId,
|
|
246
|
+
signals,
|
|
247
|
+
hash,
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
// createEffect(() => reconcile(...))
|
|
251
|
+
// In DOM mode, use raw key expression. Keys are compared by value.
|
|
252
|
+
statements.push(
|
|
253
|
+
t.expressionStatement(
|
|
254
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
255
|
+
t.arrowFunctionExpression(
|
|
256
|
+
[],
|
|
257
|
+
t.blockStatement([
|
|
258
|
+
t.expressionStatement(
|
|
259
|
+
t.callExpression(t.identifier("reconcile"), [
|
|
260
|
+
t.identifier(anchorVar),
|
|
261
|
+
t.identifier(keyMapVar),
|
|
262
|
+
keyed.signalExpr,
|
|
263
|
+
t.arrowFunctionExpression([keyed.itemParam], keyed.keyExpr),
|
|
264
|
+
t.arrowFunctionExpression(
|
|
265
|
+
[keyed.itemParam],
|
|
266
|
+
t.blockStatement([
|
|
267
|
+
...createNodeStmts,
|
|
268
|
+
t.returnStatement(t.identifier(jsxVar)),
|
|
269
|
+
]),
|
|
270
|
+
),
|
|
271
|
+
]),
|
|
272
|
+
),
|
|
273
|
+
]),
|
|
274
|
+
),
|
|
275
|
+
]),
|
|
276
|
+
),
|
|
277
|
+
);
|
|
278
|
+
}
|