@sigil-dev/compiler 0.7.5 → 0.7.6

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.
@@ -1,269 +1,430 @@
1
- import { type PluginObject, types as t, template } from "@babel/core";
2
- import { handleDerived } from "./handlers/dom/derived.ts";
3
- import { handleEffect } from "./handlers/dom/effect.ts";
4
- import { handleState } from "./handlers/dom/state.ts";
5
- import { handleDerivedSSR } from "./handlers/ssr/derived.ts";
6
- import { handleStateSSR } from "./handlers/ssr/state.ts";
7
- import { processElement, processFragment } from "./jsx/index.ts";
8
- import { processElementSSR, processFragmentSSR } from "./jsx/ssr.ts";
9
- import { getMacro } from "./util/helpers.ts";
10
- import { warnDeadReactivity } from "./util/dead-code.ts";
11
-
12
- const SSR_HELPERS = template.statements.ast(`
13
- const __SAFE = Symbol.for('sigil.safe');
14
- class __H {
15
- constructor(v) { this.v = v; this[__SAFE] = true; }
16
- toString() { return this.v; }
17
- }
18
- const __h = (v) => {
19
- if (v?.[__SAFE]) return v;
20
- if (Array.isArray(v)) return new __H(v.map(x => x?.[__SAFE] ? x.v : __e(x)).join(''));
21
- return new __H(String(v ?? ''));
22
- };
23
- const __e = (v) => {
24
- if (v?.[__SAFE]) return v.v;
25
- if (Array.isArray(v)) return v.map(x => x?.[__SAFE] ? x.v : __e(x)).join('');
26
- if (v === false || v === null || v === undefined) return '';
27
- return String(v).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
28
- };
29
- `);
30
- /**
31
- * Recursively convert key={expr} to data-key={expr} in JSX tree.
32
- * Needed for SSR output so hydration can map items by key.
33
- */
34
- function convertKeyToDataKey(node: t.JSXElement): void {
35
- for (let i = 0; i < node.openingElement.attributes.length; i++) {
36
- const attr = node.openingElement.attributes[i];
37
- if (
38
- t.isJSXAttribute(attr) &&
39
- t.isJSXIdentifier(attr.name) &&
40
- attr.name.name === "key"
41
- ) {
42
- node.openingElement.attributes[i] = t.jsxAttribute(
43
- t.jsxIdentifier("data-key"),
44
- attr.value,
45
- );
46
- }
47
- }
48
- for (const child of node.children) {
49
- if (t.isJSXElement(child)) {
50
- convertKeyToDataKey(child);
51
- } else if (
52
- t.isJSXExpressionContainer(child) &&
53
- t.isJSXElement(child.expression)
54
- ) {
55
- convertKeyToDataKey(child.expression);
56
- }
57
- }
58
- }
59
-
60
- interface SigilOptions {
61
- hash?: string;
62
- mode?: "dom" | "ssr" | "hydrate";
63
- }
64
-
65
- export default function sigilPlugin(): PluginObject {
66
- const signals = new Set<string>();
67
- const storeSignals = new Set<string>();
68
- let scopedHash: string | undefined;
69
- let isSSR = false;
70
- let isHydrate = false;
71
- let didTransform = false;
72
-
73
- return {
74
- name: "sigil",
75
- visitor: {
76
- Program: {
77
- enter(path, state) {
78
- signals.clear();
79
- storeSignals.clear();
80
- const opts = (state.opts || {}) as SigilOptions;
81
- scopedHash = opts.hash;
82
- didTransform = false;
83
- isSSR = opts.mode === "ssr";
84
- isHydrate = opts.mode === "hydrate";
85
-
86
- if (isSSR) {
87
- path.unshiftContainer("body", SSR_HELPERS);
88
- return;
89
- }
90
-
91
-
92
-
93
- },
94
- exit(path, state) {
95
- warnDeadReactivity(path, signals, storeSignals, state.filename);
96
-
97
- // Only inject runtime import if this file actually used sigil transforms
98
- if (!didTransform || isSSR) return;
99
-
100
- const hasRuntime = path.node.body.some(
101
- n => t.isImportDeclaration(n) && n.source.value === "@sigil-dev/runtime"
102
- );
103
- if (hasRuntime) return;
104
-
105
- path.unshiftContainer("body", t.importDeclaration(
106
- [
107
- t.importSpecifier(t.identifier("createSignal"), t.identifier("createSignal")),
108
- t.importSpecifier(t.identifier("createEffect"), t.identifier("createEffect")),
109
- t.importSpecifier(t.identifier("createMemo"), t.identifier("createMemo")),
110
- t.importSpecifier(t.identifier("reconcile"), t.identifier("reconcile")),
111
- t.importSpecifier(t.identifier("claim"), t.identifier("claim")),
112
- t.importSpecifier(t.identifier("claimText"), t.identifier("claimText")),
113
- t.importSpecifier(t.identifier("claimComment"), t.identifier("claimComment")),
114
- t.importSpecifier(t.identifier("hydrateKeyedList"), t.identifier("hydrateKeyedList")),
115
- t.importSpecifier(t.identifier("insert"), t.identifier("insert")),
116
- t.importSpecifier(t.identifier("getHydrationNodes"), t.identifier("getHydrationNodes")),
117
- t.importSpecifier(t.identifier("pushHydrationNodes"), t.identifier("pushHydrationNodes")),
118
- t.importSpecifier(t.identifier("popHydrationNodes"), t.identifier("popHydrationNodes")),
119
- ],
120
- t.stringLiteral("@sigil-dev/runtime"),
121
- ));
122
- }
123
- },
124
- VariableDeclaration(path) {
125
- for (const declarator of path.get("declarations")) {
126
- const macro = getMacro(declarator);
127
- if (!macro) continue;
128
- didTransform = true;
129
- const { name, init } = macro;
130
- const varName = t.isIdentifier(declarator.node.id)
131
- ? declarator.node.id.name
132
- : null;
133
-
134
- if (name === "$state" || name === "$store") {
135
- isSSR
136
- ? handleStateSSR(path, declarator, init)
137
- : handleState(path, declarator, init, signals);
138
- if (name === "$store" && varName) {
139
- storeSignals.add(varName);
140
- }
141
- } else if (name === "$derived") {
142
- isSSR
143
- ? handleDerivedSSR(path, declarator, init)
144
- : handleDerived(path, declarator, init, signals);
145
- }
146
- }
147
- },
148
- ExpressionStatement(path) {
149
- if (isSSR) {
150
- // drop $effect entirely in SSR
151
- const expr = path.get("expression");
152
- if (expr.isCallExpression()) {
153
- const callee = expr.get("callee");
154
- if (callee.isIdentifier() && callee.node.name === "$effect") {
155
- didTransform = true;
156
- path.remove();
157
- }
158
- }
159
- return;
160
- }
161
- handleEffect(path);
162
- },
163
- JSXElement(path) {
164
- didTransform = true;
165
- // console.log(
166
- // "[sigil] JSXElement visitor fired, parent:",
167
- // path.parentPath?.type,
168
- // "isSSR:", isSSR
169
- // );
170
- if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
171
- return;
172
- if (isSSR) {
173
- try {
174
- convertKeyToDataKey(path.node);
175
- path.replaceWith(
176
- t.callExpression(t.identifier("__h"), [
177
- processElementSSR(path.node, signals),
178
- ]),
179
- );
180
- } catch (e) {
181
- console.error("[sigil] processElementSSR threw:", path.toString());
182
- console.error(e);
183
- throw e;
184
- }
185
- return;
186
- }
187
-
188
- const statements: t.Statement[] = [];
189
- const genId = (() => { let i = 0; return () => `_el${i++}`; })();
190
-
191
- const root = processElement(
192
- path.node, statements, genId, signals, scopedHash,
193
- isHydrate,
194
- isHydrate ? "__nodes" : undefined,
195
- );
196
-
197
- const body: t.Statement[] = [];
198
-
199
- if (isHydrate) {
200
- // DECLARE __nodes locally — every IIFE owns its own context
201
- body.push(
202
- t.variableDeclaration("const", [
203
- t.variableDeclarator(
204
- t.identifier("__nodes"),
205
- t.callExpression(t.identifier("getHydrationNodes"), [])
206
- )
207
- ])
208
- );
209
- }
210
-
211
- body.push(...statements);
212
- body.push(t.returnStatement(t.identifier(root)));
213
-
214
- path.replaceWith(
215
- t.callExpression(
216
- t.arrowFunctionExpression([], t.blockStatement(body)),
217
- []
218
- )
219
- );
220
- },
221
- JSXFragment(path) {
222
- didTransform = true;
223
- if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
224
- return;
225
- if (isSSR) {
226
- path.replaceWith(
227
- t.callExpression(t.identifier("__h"), [
228
- processFragmentSSR(path.node, signals),
229
- ]),
230
- );
231
- return;
232
- }
233
-
234
- const statements: t.Statement[] = [];
235
- const genId = (() => { let i = 0; return () => `_el${i++}`; })();
236
-
237
- const root = processFragment(
238
- path.node, statements, genId, signals, scopedHash,
239
- isHydrate,
240
- isHydrate ? "__nodes" : undefined,
241
- );
242
-
243
- const body: t.Statement[] = [];
244
-
245
- if (isHydrate) {
246
- // DECLARE __nodes locally — every IIFE owns its own context
247
- body.push(
248
- t.variableDeclaration("const", [
249
- t.variableDeclarator(
250
- t.identifier("__nodes"),
251
- t.callExpression(t.identifier("getHydrationNodes"), [])
252
- )
253
- ])
254
- );
255
- }
256
-
257
- body.push(...statements);
258
- body.push(t.returnStatement(t.identifier(root)));
259
-
260
- path.replaceWith(
261
- t.callExpression(
262
- t.arrowFunctionExpression([], t.blockStatement(body)),
263
- []
264
- )
265
- );
266
- },
267
- },
268
- };
269
- }
1
+ import { type PluginObject, types as t, template } from "@babel/core";
2
+ import { handleDerived } from "./handlers/dom/derived.ts";
3
+ import { handleEffect } from "./handlers/dom/effect.ts";
4
+ import { handleState } from "./handlers/dom/state.ts";
5
+ import { handleDerivedSSR } from "./handlers/ssr/derived.ts";
6
+ import { handleStateSSR } from "./handlers/ssr/state.ts";
7
+ import { processElement, processFragment } from "./jsx/index.ts";
8
+ import { processElementSSR, processFragmentSSR } from "./jsx/ssr.ts";
9
+ import { getMacro } from "./util/helpers.ts";
10
+ import { MAGIC } from "./util/magic.ts";
11
+ import { warnDeadReactivity } from "./util/dead-code.ts";
12
+
13
+ const SSR_HELPERS = template.statements.ast(`
14
+ const __SAFE = Symbol.for('sigil.safe');
15
+ class __H {
16
+ constructor(v) { this.v = v; this[__SAFE] = true; }
17
+ toString() { return this.v; }
18
+ }
19
+ const __h = (v) => {
20
+ if (v?.[__SAFE]) return v;
21
+ if (Array.isArray(v)) return new __H(v.map(x => x?.[__SAFE] ? x.v : __e(x)).join(''));
22
+ return new __H(String(v ?? ''));
23
+ };
24
+ const __e = (v) => {
25
+ if (v?.[__SAFE]) return v.v;
26
+ if (Array.isArray(v)) return v.map(x => x?.[__SAFE] ? x.v : __e(x)).join('');
27
+ if (v === false || v === null || v === undefined) return '';
28
+ return String(v).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
29
+ };
30
+ `);
31
+ /**
32
+ * Recursively convert key={expr} to data-key={expr} in JSX tree.
33
+ * Needed for SSR output so hydration can map items by key.
34
+ */
35
+ function convertKeyToDataKey(node: t.JSXElement): void {
36
+ for (let i = 0; i < node.openingElement.attributes.length; i++) {
37
+ const attr = node.openingElement.attributes[i];
38
+ if (
39
+ t.isJSXAttribute(attr) &&
40
+ t.isJSXIdentifier(attr.name) &&
41
+ attr.name.name === "key"
42
+ ) {
43
+ node.openingElement.attributes[i] = t.jsxAttribute(
44
+ t.jsxIdentifier("data-key"),
45
+ attr.value,
46
+ );
47
+ }
48
+ }
49
+ for (const child of node.children) {
50
+ if (t.isJSXElement(child)) {
51
+ convertKeyToDataKey(child);
52
+ } else if (
53
+ t.isJSXExpressionContainer(child) &&
54
+ t.isJSXElement(child.expression)
55
+ ) {
56
+ convertKeyToDataKey(child.expression);
57
+ }
58
+ }
59
+ }
60
+
61
+ interface SigilOptions {
62
+ hash?: string;
63
+ mode?: "dom" | "ssr" | "hydrate";
64
+ }
65
+
66
+ export default function sigilPlugin(): PluginObject {
67
+ const signals = new Set<string>();
68
+ const storeSignals = new Set<string>();
69
+ let scopedHash: string | undefined;
70
+ let isSSR = false;
71
+ let isHydrate = false;
72
+ let didTransform = false;
73
+
74
+ return {
75
+ name: "sigil",
76
+ visitor: {
77
+ Program: {
78
+ enter(path, state) {
79
+ signals.clear();
80
+ storeSignals.clear();
81
+ const opts = (state.opts || {}) as SigilOptions;
82
+ scopedHash = opts.hash;
83
+ didTransform = false;
84
+ isSSR = opts.mode === "ssr";
85
+ isHydrate = opts.mode === "hydrate";
86
+
87
+ if (isSSR) {
88
+ path.unshiftContainer("body", SSR_HELPERS);
89
+ return;
90
+ }
91
+
92
+
93
+
94
+ },
95
+ exit(path, state) {
96
+ warnDeadReactivity(path, signals, storeSignals, state.filename);
97
+
98
+ // Only inject runtime import if this file actually used sigil transforms
99
+ if (!didTransform || isSSR) return;
100
+
101
+ const requiredSpecifiers = [
102
+ "createSignal", "createRawSignal", "createEffect", "createMemo", "reconcile",
103
+ "claim", "claimText", "claimComment", "hydrateKeyedList",
104
+ "insert", "getHydrationNodes", "pushHydrationNodes", "popHydrationNodes",
105
+ "snapshot", "tracking", "createEffectPre", "createInspectEffect",
106
+ ];
107
+
108
+ const runtimeImport = path.node.body.find(
109
+ (n): n is t.ImportDeclaration => t.isImportDeclaration(n) && n.source.value === "@sigil-dev/runtime"
110
+ );
111
+
112
+ if (runtimeImport) {
113
+ const existingNames = new Set(
114
+ runtimeImport.specifiers
115
+ .filter(t.isImportSpecifier)
116
+ .map(s => t.isIdentifier(s.imported) ? s.imported.name : (s.imported as t.StringLiteral).value)
117
+ );
118
+ for (const name of requiredSpecifiers) {
119
+ if (!existingNames.has(name)) {
120
+ runtimeImport.specifiers.push(
121
+ t.importSpecifier(t.identifier(name), t.identifier(name))
122
+ );
123
+ }
124
+ }
125
+ } else {
126
+ path.unshiftContainer("body", t.importDeclaration(
127
+ requiredSpecifiers.map(name =>
128
+ t.importSpecifier(t.identifier(name), t.identifier(name))
129
+ ),
130
+ t.stringLiteral("@sigil-dev/runtime"),
131
+ ));
132
+ }
133
+ }
134
+ },
135
+ VariableDeclaration(path) {
136
+ for (const declarator of path.get("declarations")) {
137
+ const macro = getMacro(declarator);
138
+ if (!macro) continue;
139
+ didTransform = true;
140
+ const { name, init } = macro;
141
+ const varName = t.isIdentifier(declarator.node.id)
142
+ ? declarator.node.id.name
143
+ : null;
144
+
145
+ if (name === "$state" || name === "$store" || name === "$state.raw") {
146
+ if (name === "$state.raw") {
147
+ if (!isSSR) {
148
+ const args = init.node.arguments;
149
+ init.replaceWith(
150
+ t.callExpression(t.identifier(MAGIC.createRawSignal), args),
151
+ );
152
+ }
153
+ continue;
154
+ }
155
+ isSSR
156
+ ? handleStateSSR(path, declarator, init, signals)
157
+ : handleState(path, declarator, init, signals);
158
+ if (name === "$store" && varName) {
159
+ storeSignals.add(varName);
160
+ }
161
+ } else if (name === "$derived") {
162
+ isSSR
163
+ ? handleDerivedSSR(path, declarator, init, signals)
164
+ : handleDerived(path, declarator, init, signals);
165
+ }
166
+ }
167
+ },
168
+ ExpressionStatement(path) {
169
+ if (isSSR) {
170
+ // drop $effect, $inspect, and $debug entirely in SSR
171
+ const expr = path.get("expression");
172
+ if (expr.isCallExpression()) {
173
+ const callee = expr.get("callee");
174
+ if (callee.isIdentifier() && (callee.node.name === "$effect" || callee.node.name === "$inspect" || callee.node.name === "$debug")) {
175
+ didTransform = true;
176
+ path.remove();
177
+ }
178
+ }
179
+ return;
180
+ }
181
+ handleEffect(path);
182
+ // Handle $debug(expr) expression statements
183
+ const expr = path.get("expression");
184
+ if (expr.isCallExpression()) {
185
+ const callee = expr.get("callee");
186
+ if (callee.isIdentifier({ name: "$debug" })) {
187
+ didTransform = true;
188
+ path.replaceWith(
189
+ t.ifStatement(
190
+ t.memberExpression(
191
+ t.memberExpression(
192
+ t.memberExpression(
193
+ t.identifier("import"),
194
+ t.identifier("meta"),
195
+ ),
196
+ t.identifier("env"),
197
+ ),
198
+ t.identifier("DEV"),
199
+ ),
200
+ t.blockStatement([
201
+ t.debuggerStatement(),
202
+ t.expressionStatement(
203
+ t.callExpression(
204
+ t.memberExpression(
205
+ t.identifier("console"),
206
+ t.identifier("log"),
207
+ ),
208
+ expr.node.arguments,
209
+ ),
210
+ ),
211
+ ]),
212
+ ),
213
+ );
214
+ }
215
+ }
216
+ },
217
+ CallExpression(path) {
218
+ if (isSSR) return;
219
+ const callee = path.get("callee");
220
+
221
+ // Helper: wrap signal identifiers in call expressions
222
+ const wrapSignal = (expr: t.Expression): t.Expression => {
223
+ if (t.isIdentifier(expr) && signals.has(expr.name)) {
224
+ return t.callExpression(expr, []);
225
+ }
226
+ return expr;
227
+ };
228
+
229
+ // Handle member expression callees: $state.snapshot(x), $effect.tracking(), $effect.pre(fn)
230
+ if (callee.isMemberExpression()) {
231
+ const object = callee.get("object");
232
+ const property = callee.get("property");
233
+ if (object.isIdentifier() && property.isIdentifier()) {
234
+ const obj = object.node.name;
235
+ const prop = property.node.name;
236
+
237
+ if (obj === "$state" && prop === "snapshot") {
238
+ didTransform = true;
239
+ path.get("callee").replaceWith(t.identifier(MAGIC.snapshot));
240
+ return;
241
+ }
242
+
243
+ if (obj === "$effect" && prop === "tracking") {
244
+ didTransform = true;
245
+ path.get("callee").replaceWith(t.identifier(MAGIC.tracking));
246
+ return;
247
+ }
248
+
249
+ if (obj === "$effect" && prop === "pre") {
250
+ didTransform = true;
251
+ path.get("callee").replaceWith(t.identifier(MAGIC.createEffectPre));
252
+ return;
253
+ }
254
+ }
255
+ }
256
+
257
+ // Handle $inspect(count).with(cb) chain
258
+ if (callee.isMemberExpression()) {
259
+ const property = callee.get("property");
260
+ if (property.isIdentifier({ name: "with" })) {
261
+ const object = callee.get("object");
262
+ if (object.isCallExpression()) {
263
+ const innerCallee = object.get("callee");
264
+ if (innerCallee.isIdentifier({ name: "$inspect" })) {
265
+ didTransform = true;
266
+ const args = object.node.arguments.map(wrapSignal);
267
+ const cb = path.node.arguments[0];
268
+ path.replaceWith(
269
+ t.callExpression(t.identifier(MAGIC.createInspectEffect), [
270
+ t.arrowFunctionExpression([], t.arrayExpression(args)),
271
+ cb,
272
+ ]),
273
+ );
274
+ return;
275
+ }
276
+ }
277
+ }
278
+ }
279
+
280
+ // Handle standalone $debug(...) — not as expression statement
281
+ if (callee.isIdentifier({ name: "$debug" })) {
282
+ didTransform = true;
283
+ path.replaceWith(
284
+ t.ifStatement(
285
+ t.memberExpression(
286
+ t.memberExpression(
287
+ t.memberExpression(
288
+ t.identifier("import"),
289
+ t.identifier("meta"),
290
+ ),
291
+ t.identifier("env"),
292
+ ),
293
+ t.identifier("DEV"),
294
+ ),
295
+ t.blockStatement([
296
+ t.debuggerStatement(),
297
+ t.expressionStatement(
298
+ t.callExpression(
299
+ t.memberExpression(
300
+ t.identifier("console"),
301
+ t.identifier("log"),
302
+ ),
303
+ path.node.arguments,
304
+ ),
305
+ ),
306
+ ]),
307
+ ),
308
+ );
309
+ return;
310
+ }
311
+
312
+ // Handle standalone $inspect(...)
313
+ if (callee.isIdentifier({ name: "$inspect" })) {
314
+ didTransform = true;
315
+ const args = path.node.arguments.map(wrapSignal);
316
+ path.replaceWith(
317
+ t.callExpression(t.identifier(MAGIC.createInspectEffect), [
318
+ t.arrowFunctionExpression([], t.arrayExpression(args)),
319
+ ]),
320
+ );
321
+ return;
322
+ }
323
+ },
324
+ JSXElement(path) {
325
+ didTransform = true;
326
+ // console.log(
327
+ // "[sigil] JSXElement visitor fired, parent:",
328
+ // path.parentPath?.type,
329
+ // "isSSR:", isSSR
330
+ // );
331
+ if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
332
+ return;
333
+ if (isSSR) {
334
+ try {
335
+ convertKeyToDataKey(path.node);
336
+ path.replaceWith(
337
+ t.callExpression(t.identifier("__h"), [
338
+ processElementSSR(path.node, signals),
339
+ ]),
340
+ );
341
+ } catch (e) {
342
+ console.error("[sigil] processElementSSR threw:", path.toString());
343
+ console.error(e);
344
+ throw e;
345
+ }
346
+ return;
347
+ }
348
+
349
+ const statements: t.Statement[] = [];
350
+ const genId = (() => { let i = 0; return () => `_el${i++}`; })();
351
+
352
+ const root = processElement(
353
+ path.node, statements, genId, signals, scopedHash,
354
+ isHydrate,
355
+ isHydrate ? "__nodes" : undefined,
356
+ );
357
+
358
+ const body: t.Statement[] = [];
359
+
360
+ if (isHydrate) {
361
+ // DECLARE __nodes locally — every IIFE owns its own context
362
+ body.push(
363
+ t.variableDeclaration("const", [
364
+ t.variableDeclarator(
365
+ t.identifier("__nodes"),
366
+ t.callExpression(t.identifier("getHydrationNodes"), [])
367
+ )
368
+ ])
369
+ );
370
+ }
371
+
372
+ body.push(...statements);
373
+ body.push(t.returnStatement(t.identifier(root)));
374
+
375
+ path.replaceWith(
376
+ t.callExpression(
377
+ t.arrowFunctionExpression([], t.blockStatement(body)),
378
+ []
379
+ )
380
+ );
381
+ },
382
+ JSXFragment(path) {
383
+ didTransform = true;
384
+ if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
385
+ return;
386
+ if (isSSR) {
387
+ path.replaceWith(
388
+ t.callExpression(t.identifier("__h"), [
389
+ processFragmentSSR(path.node, signals),
390
+ ]),
391
+ );
392
+ return;
393
+ }
394
+
395
+ const statements: t.Statement[] = [];
396
+ const genId = (() => { let i = 0; return () => `_el${i++}`; })();
397
+
398
+ const root = processFragment(
399
+ path.node, statements, genId, signals, scopedHash,
400
+ isHydrate,
401
+ isHydrate ? "__nodes" : undefined,
402
+ );
403
+
404
+ const body: t.Statement[] = [];
405
+
406
+ if (isHydrate) {
407
+ // DECLARE __nodes locally — every IIFE owns its own context
408
+ body.push(
409
+ t.variableDeclaration("const", [
410
+ t.variableDeclarator(
411
+ t.identifier("__nodes"),
412
+ t.callExpression(t.identifier("getHydrationNodes"), [])
413
+ )
414
+ ])
415
+ );
416
+ }
417
+
418
+ body.push(...statements);
419
+ body.push(t.returnStatement(t.identifier(root)));
420
+
421
+ path.replaceWith(
422
+ t.callExpression(
423
+ t.arrowFunctionExpression([], t.blockStatement(body)),
424
+ []
425
+ )
426
+ );
427
+ },
428
+ },
429
+ };
430
+ }