@sigil-dev/compiler 0.9.4 → 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 +2 -1
  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 +8 -0
  11. package/src/babel/jsx/anchor-mount.ts +385 -385
  12. package/src/babel/jsx/children.ts +124 -124
  13. package/src/babel/jsx/element.ts +848 -850
  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 -309
  17. package/src/babel/jsx/ssr.ts +351 -324
  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 -225
  34. package/test/keyed-lists.test.ts +126 -126
  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,309 +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} />) — 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
- }
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
+ }