@sigil-dev/compiler 0.9.2 → 0.9.3

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/README.md +162 -162
  2. package/index.ts +4 -4
  3. package/jsr.json +8 -8
  4. package/package.json +1 -1
  5. package/src/babel/handlers/dom/derived.ts +62 -62
  6. package/src/babel/handlers/dom/effect.ts +15 -15
  7. package/src/babel/handlers/dom/globals.ts +105 -105
  8. package/src/babel/handlers/dom/state.ts +107 -104
  9. package/src/babel/handlers/ssr/derived.ts +27 -27
  10. package/src/babel/handlers/ssr/state.ts +11 -11
  11. package/src/babel/index.ts +594 -589
  12. package/src/babel/jsx/anchor-mount.ts +389 -389
  13. package/src/babel/jsx/children.ts +124 -124
  14. package/src/babel/jsx/element.ts +831 -831
  15. package/src/babel/jsx/fragment.ts +282 -282
  16. package/src/babel/jsx/index.ts +2 -2
  17. package/src/babel/jsx/keyed-list.ts +278 -278
  18. package/src/babel/jsx/ssr.ts +319 -319
  19. package/src/babel/jsx/text-node.ts +192 -192
  20. package/src/babel/jsx/utils.ts +179 -179
  21. package/src/babel/util/bind.ts +139 -139
  22. package/src/babel/util/css.ts +151 -151
  23. package/src/babel/util/dead-code.ts +28 -28
  24. package/src/babel/util/helpers.ts +46 -46
  25. package/src/babel/util/magic.ts +11 -11
  26. package/src/bun-plugin.ts +92 -90
  27. package/src/vite/index.ts +58 -58
  28. package/test/async-components.test.ts +24 -24
  29. package/test/components.test.ts +129 -129
  30. package/test/containsSignal.test.ts +288 -288
  31. package/test/fragments.test.ts +90 -90
  32. package/test/helpers/transform.ts +45 -48
  33. package/test/hydration.test.ts +147 -147
  34. package/test/jsx.test.ts +197 -197
  35. package/test/keyed-lists.test.ts +88 -88
  36. package/test/reactivity.test.ts +225 -225
  37. package/test/scoped-css.test.ts +129 -129
  38. package/test/ssr.test.ts +130 -130
  39. package/test/tree-shaking.test.ts +78 -78
  40. package/tsconfig.json +30 -30
@@ -1,278 +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
- }
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
+ }