@sigil-dev/compiler 0.9.3 → 0.9.5

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.
Files changed (40) hide show
  1. package/index.ts +4 -4
  2. package/jsr.json +8 -8
  3. package/package.json +9 -8
  4. package/src/babel/handlers/dom/derived.ts +62 -62
  5. package/src/babel/handlers/dom/effect.ts +15 -15
  6. package/src/babel/handlers/dom/globals.ts +105 -105
  7. package/src/babel/handlers/dom/state.ts +107 -107
  8. package/src/babel/handlers/ssr/derived.ts +27 -27
  9. package/src/babel/handlers/ssr/state.ts +11 -11
  10. package/src/babel/index.ts +13 -0
  11. package/src/babel/jsx/anchor-mount.ts +385 -389
  12. package/src/babel/jsx/children.ts +124 -124
  13. package/src/babel/jsx/element.ts +848 -831
  14. package/src/babel/jsx/fragment.ts +282 -282
  15. package/src/babel/jsx/index.ts +2 -2
  16. package/src/babel/jsx/keyed-list.ts +309 -278
  17. package/src/babel/jsx/ssr.ts +351 -319
  18. package/src/babel/jsx/text-node.ts +192 -192
  19. package/src/babel/jsx/utils.ts +179 -179
  20. package/src/babel/util/bind.ts +139 -139
  21. package/src/babel/util/css.ts +151 -151
  22. package/src/babel/util/dead-code.ts +28 -28
  23. package/src/babel/util/helpers.ts +46 -46
  24. package/src/babel/util/magic.ts +11 -11
  25. package/src/bun-plugin.ts +92 -92
  26. package/src/vite/index.ts +58 -58
  27. package/test/async-components.test.ts +59 -59
  28. package/test/components.test.ts +129 -129
  29. package/test/containsSignal.test.ts +288 -288
  30. package/test/fragments.test.ts +90 -90
  31. package/test/helpers/transform.ts +45 -45
  32. package/test/hydration.test.ts +147 -147
  33. package/test/jsx.test.ts +227 -197
  34. package/test/keyed-lists.test.ts +126 -88
  35. package/test/reactivity.test.ts +225 -225
  36. package/test/scoped-css.test.ts +129 -129
  37. package/test/ssr.test.ts +130 -130
  38. package/test/style-object.test.ts +13 -0
  39. package/test/tree-shaking.test.ts +78 -78
  40. package/tsconfig.json +30 -30
@@ -1,278 +1,309 @@
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
- }
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} />) — or, since unkeyed
16
+ * maps are also reconciled (index-keyed), signal.map(item => <Tag />).
17
+ * Returns info needed to generate keyed list code, or null.
18
+ * `hasKey` is false for the unkeyed form; buildKeyedList then keys by index.
19
+ */
20
+ export function findKeyedMapExpr(
21
+ expr: t.Expression,
22
+ signals: Set<string>,
23
+ ): {
24
+ callExpr: t.CallExpression;
25
+ keyExpr: t.Expression | null;
26
+ itemParam: t.Identifier;
27
+ jsxBody: t.JSXElement;
28
+ signalExpr: t.Expression;
29
+ hasKey: boolean;
30
+ } | null {
31
+ if (!t.isCallExpression(expr)) return null;
32
+ const callee = expr.callee;
33
+ if (!t.isMemberExpression(callee)) return null;
34
+ const prop = callee.property;
35
+ if (!t.isIdentifier(prop) || prop.name !== "map") return null;
36
+
37
+ const obj = callee.object;
38
+ if (!t.isExpression(obj)) return null;
39
+ if (!containsSignal(obj, signals)) return null;
40
+
41
+ if (expr.arguments.length !== 1) return null;
42
+ const arrowFn = expr.arguments[0];
43
+ if (!t.isArrowFunctionExpression(arrowFn)) return null;
44
+ if (arrowFn.params.length !== 1) return null;
45
+ const itemParam = arrowFn.params[0];
46
+ if (!t.isIdentifier(itemParam)) return null;
47
+
48
+ const body = arrowFn.body;
49
+ if (!t.isJSXElement(body)) return null;
50
+
51
+ const keyAttr = body.openingElement.attributes.find(
52
+ (a) =>
53
+ t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && a.name.name === "key",
54
+ );
55
+ if (
56
+ keyAttr &&
57
+ t.isJSXAttribute(keyAttr) &&
58
+ t.isJSXExpressionContainer(keyAttr.value)
59
+ ) {
60
+ return {
61
+ callExpr: expr,
62
+ keyExpr: keyAttr.value.expression as t.Expression,
63
+ itemParam,
64
+ jsxBody: body,
65
+ signalExpr: obj,
66
+ hasKey: true,
67
+ };
68
+ }
69
+
70
+ // Unkeyed map — still reconciled, keyed by position.
71
+ return {
72
+ callExpr: expr,
73
+ keyExpr: null,
74
+ itemParam,
75
+ jsxBody: body,
76
+ signalExpr: obj,
77
+ hasKey: false,
78
+ };
79
+ }
80
+
81
+ /**
82
+ * Generate keyed list code using reconcile().
83
+ * In hydrate mode, uses hydrateKeyedList() to claim server-rendered nodes.
84
+ */
85
+ export function buildKeyedList(
86
+ varName: string,
87
+ keyed: NonNullable<ReturnType<typeof findKeyedMapExpr>>,
88
+ statements: t.Statement[],
89
+ genId: () => string,
90
+ signals: Set<string>,
91
+ processElement: ProcessElementFn,
92
+ hash?: string,
93
+ hydrate?: boolean,
94
+ nodesVar?: string,
95
+ parentVar?: string,
96
+ ): void {
97
+ const anchorVar = genId();
98
+ const keyMapVar = genId();
99
+
100
+ // Key extractor: the user's key={} expression wrapped in an arrow, or
101
+ // index-keying for unkeyed maps. reconcile passes (item, index) so
102
+ // (_item, _i) => _i works.
103
+ const keyFn = t.arrowFunctionExpression(
104
+ keyed.hasKey
105
+ ? [keyed.itemParam]
106
+ : [t.identifier("_item"), t.identifier("_i")],
107
+ keyed.keyExpr ?? t.identifier("_i"),
108
+ );
109
+
110
+ // Strip key prop from JSX before processing.
111
+ // In SSR mode, the Babel plugin already converted key → data-key.
112
+ // In DOM/hydrate mode, we don't need either.
113
+ keyed.jsxBody.openingElement.attributes =
114
+ keyed.jsxBody.openingElement.attributes.filter(
115
+ (a) =>
116
+ !(
117
+ t.isJSXAttribute(a) &&
118
+ t.isJSXIdentifier(a.name) &&
119
+ (a.name.name === "key" || a.name.name === "data-key")
120
+ ),
121
+ );
122
+
123
+ if (hydrate) {
124
+ // ─── Hydration path: use hydrateKeyedList() ───
125
+ // Strip key prop from JSX before processing
126
+ keyed.jsxBody.openingElement.attributes =
127
+ keyed.jsxBody.openingElement.attributes.filter(
128
+ (a) =>
129
+ !(
130
+ t.isJSXAttribute(a) &&
131
+ t.isJSXIdentifier(a.name) &&
132
+ a.name.name === "key"
133
+ ),
134
+ );
135
+
136
+ // Process the JSX element to get createNode body
137
+ const createNodeStmts: t.Statement[] = [];
138
+ const jsxVar = processElement(
139
+ keyed.jsxBody,
140
+ createNodeStmts,
141
+ genId,
142
+ signals,
143
+ hash,
144
+ hydrate,
145
+ nodesVar,
146
+ );
147
+
148
+ // const { anchor, keyMap } = hydrateKeyedList(__nodes, signal, keyFn, createNode, parent)
149
+ const resultVar = genId();
150
+ const hydrateArgs: t.Expression[] = [
151
+ t.identifier(nodesVar ?? "__nodes"),
152
+ keyed.signalExpr,
153
+ keyFn,
154
+ t.arrowFunctionExpression(
155
+ [keyed.itemParam],
156
+ t.blockStatement([
157
+ ...createNodeStmts,
158
+ t.returnStatement(t.identifier(jsxVar)),
159
+ ]),
160
+ ),
161
+ ];
162
+ if (hydrate && parentVar) hydrateArgs.push(t.identifier(parentVar));
163
+ statements.push(
164
+ t.variableDeclaration("const", [
165
+ t.variableDeclarator(
166
+ t.identifier(resultVar),
167
+ t.callExpression(t.identifier("hydrateKeyedList"), hydrateArgs),
168
+ ),
169
+ ]),
170
+ );
171
+
172
+ // const anchor = result.anchor
173
+ statements.push(
174
+ t.variableDeclaration("const", [
175
+ t.variableDeclarator(
176
+ t.identifier(anchorVar),
177
+ t.memberExpression(t.identifier(resultVar), t.identifier("anchor")),
178
+ ),
179
+ ]),
180
+ );
181
+
182
+ // const keyMap = result.keyMap
183
+ statements.push(
184
+ t.variableDeclaration("const", [
185
+ t.variableDeclarator(
186
+ t.identifier(keyMapVar),
187
+ t.memberExpression(t.identifier(resultVar), t.identifier("keyMap")),
188
+ ),
189
+ ]),
190
+ );
191
+
192
+ // createEffect(() => reconcile(anchor, keyMap, signal, keyFn, createNode))
193
+ statements.push(
194
+ t.expressionStatement(
195
+ t.callExpression(t.identifier("createEffect"), [
196
+ t.arrowFunctionExpression(
197
+ [],
198
+ t.blockStatement([
199
+ t.expressionStatement(
200
+ t.callExpression(t.identifier("reconcile"), [
201
+ t.identifier(anchorVar),
202
+ t.identifier(keyMapVar),
203
+ keyed.signalExpr,
204
+ keyFn,
205
+ t.arrowFunctionExpression(
206
+ [keyed.itemParam],
207
+ t.blockStatement([
208
+ ...createNodeStmts,
209
+ t.returnStatement(t.identifier(jsxVar)),
210
+ ]),
211
+ ),
212
+ ]),
213
+ ),
214
+ ]),
215
+ ),
216
+ ]),
217
+ ),
218
+ );
219
+ return;
220
+ }
221
+
222
+ // ─── DOM path (existing code) ───
223
+
224
+ // const _anchor = document.createComment('')
225
+ statements.push(
226
+ t.variableDeclaration("const", [
227
+ t.variableDeclarator(
228
+ t.identifier(anchorVar),
229
+ t.callExpression(
230
+ t.memberExpression(
231
+ t.identifier("document"),
232
+ t.identifier("createComment"),
233
+ ),
234
+ [t.stringLiteral("")],
235
+ ),
236
+ ),
237
+ ]),
238
+ );
239
+
240
+ // parent.append(_anchor)
241
+ statements.push(
242
+ t.expressionStatement(
243
+ t.callExpression(
244
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
245
+ [t.identifier(anchorVar)],
246
+ ),
247
+ ),
248
+ );
249
+
250
+ // const _keyMap = new Map()
251
+ statements.push(
252
+ t.variableDeclaration("const", [
253
+ t.variableDeclarator(
254
+ t.identifier(keyMapVar),
255
+ t.newExpression(t.identifier("Map"), []),
256
+ ),
257
+ ]),
258
+ );
259
+
260
+ // Strip key prop from JSX before processing
261
+ keyed.jsxBody.openingElement.attributes =
262
+ keyed.jsxBody.openingElement.attributes.filter(
263
+ (a) =>
264
+ !(
265
+ t.isJSXAttribute(a) &&
266
+ t.isJSXIdentifier(a.name) &&
267
+ a.name.name === "key"
268
+ ),
269
+ );
270
+
271
+ // Process the JSX element to get createNode body
272
+ const createNodeStmts: t.Statement[] = [];
273
+ const jsxVar = processElement(
274
+ keyed.jsxBody,
275
+ createNodeStmts,
276
+ genId,
277
+ signals,
278
+ hash,
279
+ );
280
+
281
+ // createEffect(() => reconcile(...))
282
+ // In DOM mode, use raw key expression. Keys are compared by value.
283
+ statements.push(
284
+ t.expressionStatement(
285
+ t.callExpression(t.identifier("createEffect"), [
286
+ t.arrowFunctionExpression(
287
+ [],
288
+ t.blockStatement([
289
+ t.expressionStatement(
290
+ t.callExpression(t.identifier("reconcile"), [
291
+ t.identifier(anchorVar),
292
+ t.identifier(keyMapVar),
293
+ keyed.signalExpr,
294
+ keyFn,
295
+ t.arrowFunctionExpression(
296
+ [keyed.itemParam],
297
+ t.blockStatement([
298
+ ...createNodeStmts,
299
+ t.returnStatement(t.identifier(jsxVar)),
300
+ ]),
301
+ ),
302
+ ]),
303
+ ),
304
+ ]),
305
+ ),
306
+ ]),
307
+ ),
308
+ );
309
+ }