@sigil-dev/compiler 0.7.4 → 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.
- package/package.json +5 -1
- package/src/babel/handlers/dom/derived.ts +31 -1
- package/src/babel/handlers/dom/globals.ts +102 -0
- package/src/babel/handlers/ssr/state.ts +10 -10
- package/src/babel/index.ts +430 -257
- package/src/babel/jsx/children.ts +124 -124
- package/src/babel/jsx/element.ts +803 -582
- package/src/babel/jsx/ssr.ts +319 -310
- package/src/babel/jsx/utils.ts +95 -89
- package/src/babel/util/bind.ts +126 -135
- package/src/babel/util/css.ts +151 -151
- package/src/babel/util/helpers.ts +16 -2
- package/src/babel/util/magic.ts +5 -0
- package/src/bun-plugin.ts +86 -45
- package/src/vite/index.ts +7 -2
- package/test/jsx.test.ts +195 -195
- package/test/reactivity.test.ts +147 -1
package/src/babel/jsx/element.ts
CHANGED
|
@@ -1,582 +1,803 @@
|
|
|
1
|
-
import { types as t } from "@babel/core";
|
|
2
|
-
import {generate} from "@babel/generator"
|
|
3
|
-
import { buildBind, buildProxyBind } from "../util/bind";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
genId
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
t.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
)
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
1
|
+
import { types as t } from "@babel/core";
|
|
2
|
+
import {generate} from "@babel/generator"
|
|
3
|
+
import { buildBind, buildProxyBind } from "../util/bind";
|
|
4
|
+
import { handleGlobalComponent, isGlobalComponent } from "../handlers/dom/globals";
|
|
5
|
+
import { buildAnchorMount } from "./anchor-mount";
|
|
6
|
+
import { collectChildren } from "./children";
|
|
7
|
+
import { processFragment } from "./fragment";
|
|
8
|
+
import { buildKeyedList, findKeyedMapExpr } from "./keyed-list";
|
|
9
|
+
import { buildTextNode } from "./text-node";
|
|
10
|
+
import {
|
|
11
|
+
ATTR_MAP,
|
|
12
|
+
buildHydrationScope,
|
|
13
|
+
containsSignal,
|
|
14
|
+
getCreateElement,
|
|
15
|
+
isPrimitive,
|
|
16
|
+
} from "./utils";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Build a property-access expression for a JSX attribute name.
|
|
20
|
+
* Dashed names (data-*, aria-*) require computed access (e.g. el["data-id"])
|
|
21
|
+
* because t.identifier() rejects names with dashes.
|
|
22
|
+
*/
|
|
23
|
+
function attrAccess(varName: string, attrName: string): t.MemberExpression {
|
|
24
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(attrName)) {
|
|
25
|
+
return t.memberExpression(t.identifier(varName), t.identifier(attrName));
|
|
26
|
+
}
|
|
27
|
+
return t.memberExpression(
|
|
28
|
+
t.identifier(varName),
|
|
29
|
+
t.stringLiteral(attrName),
|
|
30
|
+
true,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Build an object property key (identifier for valid names, string literal for dashed).
|
|
36
|
+
*/
|
|
37
|
+
function attrKey(attrName: string): t.Identifier | t.StringLiteral {
|
|
38
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(attrName)) {
|
|
39
|
+
return t.identifier(attrName);
|
|
40
|
+
}
|
|
41
|
+
return t.stringLiteral(attrName);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Add classList.add(hash) statement for scoped CSS.
|
|
46
|
+
*/
|
|
47
|
+
function addScopedClass(
|
|
48
|
+
varName: string,
|
|
49
|
+
hash: string,
|
|
50
|
+
statements: t.Statement[],
|
|
51
|
+
): void {
|
|
52
|
+
statements.push(
|
|
53
|
+
t.expressionStatement(
|
|
54
|
+
t.callExpression(
|
|
55
|
+
t.memberExpression(
|
|
56
|
+
t.memberExpression(t.identifier(varName), t.identifier("classList")),
|
|
57
|
+
t.identifier("add"),
|
|
58
|
+
),
|
|
59
|
+
[t.stringLiteral(hash)],
|
|
60
|
+
),
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function processElement(
|
|
66
|
+
node: t.JSXElement,
|
|
67
|
+
statements: t.Statement[],
|
|
68
|
+
genId: () => string,
|
|
69
|
+
signals: Set<string>,
|
|
70
|
+
hash?: string,
|
|
71
|
+
hydrate?: boolean,
|
|
72
|
+
nodesVar?: string,
|
|
73
|
+
parentVar?: string,
|
|
74
|
+
): string {
|
|
75
|
+
const varName = genId();
|
|
76
|
+
const tag = (node.openingElement.name as t.JSXIdentifier).name;
|
|
77
|
+
const isComponent = /^[A-Z]/.test(tag);
|
|
78
|
+
|
|
79
|
+
if (isComponent) {
|
|
80
|
+
// Global components: Window, Document, Body
|
|
81
|
+
if (isGlobalComponent(tag)) {
|
|
82
|
+
return handleGlobalComponent(tag, node, statements, genId);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Component: call the function with a props object
|
|
86
|
+
const propsObj = t.objectExpression([]);
|
|
87
|
+
|
|
88
|
+
// Add attributes to props (including spread)
|
|
89
|
+
for (const attr of node.openingElement.attributes) {
|
|
90
|
+
if (t.isJSXSpreadAttribute(attr)) {
|
|
91
|
+
// {…obj} — spread into props
|
|
92
|
+
propsObj.properties.push(t.spreadElement(attr.argument));
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (!t.isJSXAttribute(attr)) continue;
|
|
96
|
+
const attrName = (attr.name as t.JSXIdentifier).name;
|
|
97
|
+
|
|
98
|
+
if (t.isStringLiteral(attr.value)) {
|
|
99
|
+
propsObj.properties.push(
|
|
100
|
+
t.objectProperty(attrKey(attrName), attr.value),
|
|
101
|
+
);
|
|
102
|
+
} else if (t.isJSXExpressionContainer(attr.value)) {
|
|
103
|
+
const expr = attr.value.expression as t.Expression;
|
|
104
|
+
propsObj.properties.push(
|
|
105
|
+
t.objectProperty(attrKey(attrName), expr),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Add children to props
|
|
111
|
+
const childrenExpr = collectChildren(
|
|
112
|
+
node,
|
|
113
|
+
statements,
|
|
114
|
+
genId,
|
|
115
|
+
signals,
|
|
116
|
+
hash,
|
|
117
|
+
hydrate,
|
|
118
|
+
nodesVar,
|
|
119
|
+
);
|
|
120
|
+
if (childrenExpr) {
|
|
121
|
+
propsObj.properties.push(
|
|
122
|
+
t.objectProperty(t.identifier("children"), childrenExpr),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const componentCall = t.callExpression(t.identifier(tag), [propsObj]);
|
|
127
|
+
|
|
128
|
+
if (hydrate && nodesVar) {
|
|
129
|
+
// Push the parent's child pool before calling the component so that
|
|
130
|
+
// getHydrationNodes() inside the component returns the right scope.
|
|
131
|
+
// In SSR hydration the component claims its root node from that pool
|
|
132
|
+
// (it's already in the DOM). In empty-pool / SPA mode the component
|
|
133
|
+
// creates a fresh node, so we explicitly insert it when the pool is empty.
|
|
134
|
+
statements.push(
|
|
135
|
+
t.expressionStatement(
|
|
136
|
+
t.callExpression(t.identifier("pushHydrationNodes"), [
|
|
137
|
+
t.identifier(nodesVar),
|
|
138
|
+
]),
|
|
139
|
+
),
|
|
140
|
+
);
|
|
141
|
+
statements.push(
|
|
142
|
+
t.variableDeclaration("const", [
|
|
143
|
+
t.variableDeclarator(t.identifier(varName), componentCall),
|
|
144
|
+
]),
|
|
145
|
+
);
|
|
146
|
+
statements.push(
|
|
147
|
+
t.expressionStatement(
|
|
148
|
+
t.callExpression(t.identifier("popHydrationNodes"), []),
|
|
149
|
+
),
|
|
150
|
+
);
|
|
151
|
+
if (parentVar) {
|
|
152
|
+
// Insert only when the pool was empty (fresh DOM); if the pool had
|
|
153
|
+
// nodes the component already claimed its SSR element (already in DOM).
|
|
154
|
+
statements.push(
|
|
155
|
+
t.ifStatement(
|
|
156
|
+
t.binaryExpression(
|
|
157
|
+
"===",
|
|
158
|
+
t.memberExpression(
|
|
159
|
+
t.identifier(nodesVar),
|
|
160
|
+
t.identifier("length"),
|
|
161
|
+
),
|
|
162
|
+
t.numericLiteral(0),
|
|
163
|
+
),
|
|
164
|
+
t.expressionStatement(
|
|
165
|
+
t.callExpression(t.identifier("insert"), [
|
|
166
|
+
t.identifier(parentVar),
|
|
167
|
+
t.identifier(varName),
|
|
168
|
+
]),
|
|
169
|
+
),
|
|
170
|
+
),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
statements.push(
|
|
175
|
+
t.variableDeclaration("const", [
|
|
176
|
+
t.variableDeclarator(t.identifier(varName), componentCall),
|
|
177
|
+
]),
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return varName;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Native element: claim (hydrate) or create (dom)
|
|
185
|
+
const currentNodesVar = nodesVar ?? "__nodes";
|
|
186
|
+
statements.push(
|
|
187
|
+
t.variableDeclaration("const", [
|
|
188
|
+
t.variableDeclarator(
|
|
189
|
+
t.identifier(varName),
|
|
190
|
+
getCreateElement(
|
|
191
|
+
tag,
|
|
192
|
+
!!hydrate,
|
|
193
|
+
currentNodesVar,
|
|
194
|
+
hydrate ? parentVar : undefined,
|
|
195
|
+
),
|
|
196
|
+
),
|
|
197
|
+
]),
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
// In hydrate mode, scope to this element's children for descendants
|
|
201
|
+
let childNodesVar: string | undefined;
|
|
202
|
+
if (hydrate) {
|
|
203
|
+
childNodesVar = genId();
|
|
204
|
+
buildHydrationScope(varName, statements, childNodesVar);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// attributes
|
|
208
|
+
const seenAttrs = new Set<string>();
|
|
209
|
+
for (const attr of node.openingElement.attributes) {
|
|
210
|
+
if (t.isJSXSpreadAttribute(attr)) {
|
|
211
|
+
// {…obj} — spread onto native element
|
|
212
|
+
statements.push(
|
|
213
|
+
t.expressionStatement(
|
|
214
|
+
t.callExpression(t.identifier("Object.assign"), [
|
|
215
|
+
t.identifier(varName),
|
|
216
|
+
attr.argument as t.Expression,
|
|
217
|
+
]),
|
|
218
|
+
),
|
|
219
|
+
);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (!t.isJSXAttribute(attr)) continue;
|
|
223
|
+
let attrName: string;
|
|
224
|
+
let attrNamespace: string | undefined;
|
|
225
|
+
if (t.isJSXNamespacedName(attr.name)) {
|
|
226
|
+
attrNamespace = attr.name.namespace.name;
|
|
227
|
+
attrName = attr.name.name.name;
|
|
228
|
+
} else {
|
|
229
|
+
attrName = (attr.name as t.JSXIdentifier).name;
|
|
230
|
+
}
|
|
231
|
+
const realAttr = ATTR_MAP[attrName] ?? attrName;
|
|
232
|
+
|
|
233
|
+
if (seenAttrs.has(realAttr)) {
|
|
234
|
+
throw new TypeError(
|
|
235
|
+
`Duplicate attribute "${realAttr}" (via "${attrName}"). Use class or className, not both.`,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
seenAttrs.add(realAttr);
|
|
239
|
+
if (attrName === "use") {
|
|
240
|
+
// use={directive} or use={[directive, params]}
|
|
241
|
+
const expr = (attr.value as t.JSXExpressionContainer)
|
|
242
|
+
.expression as t.Expression;
|
|
243
|
+
statements.push(
|
|
244
|
+
t.expressionStatement(
|
|
245
|
+
t.callExpression(t.identifier("applyDirective"), [
|
|
246
|
+
t.identifier(varName),
|
|
247
|
+
expr,
|
|
248
|
+
]),
|
|
249
|
+
),
|
|
250
|
+
);
|
|
251
|
+
} else if (/^on[A-Z]/.test(attrName)) {
|
|
252
|
+
// Standard JSX: onClick, onSubmit, onMouseenter, etc.
|
|
253
|
+
// Convert camelCase to lowercase: onClick -> click, onMouseenter -> mouseenter
|
|
254
|
+
const event = attrName.slice(2).toLowerCase();
|
|
255
|
+
const handler = (attr.value as t.JSXExpressionContainer)
|
|
256
|
+
.expression as t.Expression;
|
|
257
|
+
statements.push(
|
|
258
|
+
t.expressionStatement(
|
|
259
|
+
t.callExpression(
|
|
260
|
+
t.memberExpression(
|
|
261
|
+
t.identifier(varName),
|
|
262
|
+
t.identifier("addEventListener"),
|
|
263
|
+
),
|
|
264
|
+
[t.stringLiteral(event), handler],
|
|
265
|
+
),
|
|
266
|
+
),
|
|
267
|
+
);
|
|
268
|
+
} else if (attrNamespace === "bind") {
|
|
269
|
+
const bindProp = attrName;
|
|
270
|
+
const signal = (attr.value as t.JSXExpressionContainer)
|
|
271
|
+
.expression as t.Expression;
|
|
272
|
+
|
|
273
|
+
if (bindProp === "this") {
|
|
274
|
+
statements.push(
|
|
275
|
+
t.expressionStatement(
|
|
276
|
+
t.callExpression(
|
|
277
|
+
t.memberExpression(
|
|
278
|
+
(signal as t.CallExpression).callee as t.Expression,
|
|
279
|
+
t.identifier("set"),
|
|
280
|
+
),
|
|
281
|
+
[t.identifier(varName)],
|
|
282
|
+
),
|
|
283
|
+
),
|
|
284
|
+
);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (bindProp === "group") {
|
|
289
|
+
// Scan element attributes for type and value
|
|
290
|
+
let inputType = "";
|
|
291
|
+
let inputValue: t.StringLiteral | undefined;
|
|
292
|
+
for (const a of node.openingElement.attributes) {
|
|
293
|
+
if (t.isJSXAttribute(a) && t.isJSXIdentifier(a.name)) {
|
|
294
|
+
if (a.name.name === "type" && t.isStringLiteral(a.value)) {
|
|
295
|
+
inputType = a.value.value;
|
|
296
|
+
}
|
|
297
|
+
if (a.name.name === "value" && t.isStringLiteral(a.value)) {
|
|
298
|
+
inputValue = a.value;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (inputType === "radio" || inputType === "") {
|
|
304
|
+
const isTopLevel = t.isCallExpression(signal) &&
|
|
305
|
+
t.isIdentifier((signal as t.CallExpression).callee) &&
|
|
306
|
+
signals.has(((signal as t.CallExpression).callee as t.Identifier).name);
|
|
307
|
+
|
|
308
|
+
if (isTopLevel) {
|
|
309
|
+
const signalId = (signal as t.CallExpression).callee as t.Identifier;
|
|
310
|
+
statements.push(
|
|
311
|
+
t.expressionStatement(
|
|
312
|
+
t.callExpression(
|
|
313
|
+
t.memberExpression(t.identifier(varName), t.identifier("addEventListener")),
|
|
314
|
+
[
|
|
315
|
+
t.stringLiteral("change"),
|
|
316
|
+
t.arrowFunctionExpression(
|
|
317
|
+
[],
|
|
318
|
+
t.callExpression(
|
|
319
|
+
t.memberExpression(signalId, t.identifier("set")),
|
|
320
|
+
[
|
|
321
|
+
t.memberExpression(
|
|
322
|
+
t.identifier(varName),
|
|
323
|
+
t.identifier("value"),
|
|
324
|
+
),
|
|
325
|
+
],
|
|
326
|
+
),
|
|
327
|
+
),
|
|
328
|
+
],
|
|
329
|
+
),
|
|
330
|
+
),
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
} else if (inputType === "checkbox") {
|
|
334
|
+
const isTopLevel = t.isCallExpression(signal) &&
|
|
335
|
+
t.isIdentifier((signal as t.CallExpression).callee) &&
|
|
336
|
+
signals.has(((signal as t.CallExpression).callee as t.Identifier).name);
|
|
337
|
+
|
|
338
|
+
if (isTopLevel) {
|
|
339
|
+
const callExpr = signal as t.CallExpression;
|
|
340
|
+
statements.push(
|
|
341
|
+
t.expressionStatement(
|
|
342
|
+
t.callExpression(
|
|
343
|
+
t.memberExpression(t.identifier(varName), t.identifier("addEventListener")),
|
|
344
|
+
[
|
|
345
|
+
t.stringLiteral("change"),
|
|
346
|
+
t.arrowFunctionExpression(
|
|
347
|
+
[],
|
|
348
|
+
t.blockStatement([
|
|
349
|
+
t.ifStatement(
|
|
350
|
+
t.memberExpression(t.identifier(varName), t.identifier("checked")),
|
|
351
|
+
// checked: push value into array
|
|
352
|
+
t.blockStatement([
|
|
353
|
+
t.expressionStatement(
|
|
354
|
+
t.callExpression(
|
|
355
|
+
t.memberExpression(
|
|
356
|
+
t.callExpression(callExpr, []),
|
|
357
|
+
t.identifier("push"),
|
|
358
|
+
),
|
|
359
|
+
[
|
|
360
|
+
t.memberExpression(
|
|
361
|
+
t.identifier(varName),
|
|
362
|
+
t.identifier("value"),
|
|
363
|
+
),
|
|
364
|
+
],
|
|
365
|
+
),
|
|
366
|
+
),
|
|
367
|
+
]),
|
|
368
|
+
// unchecked: splice value out of array
|
|
369
|
+
t.blockStatement([
|
|
370
|
+
t.expressionStatement(
|
|
371
|
+
t.callExpression(
|
|
372
|
+
t.memberExpression(
|
|
373
|
+
t.callExpression(callExpr, []),
|
|
374
|
+
t.identifier("splice"),
|
|
375
|
+
),
|
|
376
|
+
[
|
|
377
|
+
t.callExpression(
|
|
378
|
+
t.memberExpression(
|
|
379
|
+
t.callExpression(callExpr, []),
|
|
380
|
+
t.identifier("indexOf"),
|
|
381
|
+
),
|
|
382
|
+
[
|
|
383
|
+
t.memberExpression(
|
|
384
|
+
t.identifier(varName),
|
|
385
|
+
t.identifier("value"),
|
|
386
|
+
),
|
|
387
|
+
],
|
|
388
|
+
),
|
|
389
|
+
t.numericLiteral(1),
|
|
390
|
+
],
|
|
391
|
+
),
|
|
392
|
+
),
|
|
393
|
+
]),
|
|
394
|
+
),
|
|
395
|
+
]),
|
|
396
|
+
),
|
|
397
|
+
],
|
|
398
|
+
),
|
|
399
|
+
),
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
} else {
|
|
403
|
+
console.warn(
|
|
404
|
+
`[sigil] bind:group requires type="radio" or type="checkbox" on the element. ` +
|
|
405
|
+
`Got: type="${inputType}".`
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Generic bind:innerHTML, bind:textContent, bind:value, bind:checked, etc.
|
|
412
|
+
const isTopLevelSignal = t.isCallExpression(signal) &&
|
|
413
|
+
t.isIdentifier((signal as t.CallExpression).callee) &&
|
|
414
|
+
signals.has(((signal as t.CallExpression).callee as t.Identifier).name);
|
|
415
|
+
|
|
416
|
+
const isMemberOfSignal = t.isMemberExpression(signal) &&
|
|
417
|
+
t.isCallExpression((signal as t.MemberExpression).object) &&
|
|
418
|
+
t.isIdentifier(((signal as t.MemberExpression).object as t.CallExpression).callee) &&
|
|
419
|
+
signals.has((((signal as t.MemberExpression).object as t.CallExpression).callee as t.Identifier).name);
|
|
420
|
+
|
|
421
|
+
if (isTopLevelSignal) {
|
|
422
|
+
buildBind(varName, bindProp, attr, statements);
|
|
423
|
+
} else if (isMemberOfSignal) {
|
|
424
|
+
buildProxyBind(varName, bindProp, attr, statements, signal);
|
|
425
|
+
} else {
|
|
426
|
+
console.warn(
|
|
427
|
+
`[sigil] bind:${bindProp}={...} requires a $state signal or $state object property. ` +
|
|
428
|
+
`Got: ${generate(signal).code}. ` +
|
|
429
|
+
`Hint: change to a $state() call.`
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
} else if (t.isStringLiteral(attr.value)) {
|
|
433
|
+
statements.push(
|
|
434
|
+
t.expressionStatement(
|
|
435
|
+
t.assignmentExpression(
|
|
436
|
+
"=",
|
|
437
|
+
attrAccess(varName, realAttr),
|
|
438
|
+
attr.value,
|
|
439
|
+
),
|
|
440
|
+
),
|
|
441
|
+
);
|
|
442
|
+
} else if (t.isJSXExpressionContainer(attr.value)) {
|
|
443
|
+
const expr = attr.value.expression as t.Expression;
|
|
444
|
+
// style={{ color: "red", fontSize: val }} — per-property assignment
|
|
445
|
+
if (attrName === "style" && t.isObjectExpression(expr)) {
|
|
446
|
+
for (const prop of expr.properties) {
|
|
447
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
448
|
+
const key = t.isIdentifier(prop.key)
|
|
449
|
+
? prop.key.name
|
|
450
|
+
: t.isStringLiteral(prop.key)
|
|
451
|
+
? prop.key.value
|
|
452
|
+
: null;
|
|
453
|
+
if (!key) continue;
|
|
454
|
+
const val = prop.value as t.Expression;
|
|
455
|
+
const styleProp = t.memberExpression(
|
|
456
|
+
t.memberExpression(t.identifier(varName), t.identifier("style")),
|
|
457
|
+
t.identifier(key),
|
|
458
|
+
);
|
|
459
|
+
if (containsSignal(val, signals)) {
|
|
460
|
+
statements.push(
|
|
461
|
+
t.expressionStatement(
|
|
462
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
463
|
+
t.arrowFunctionExpression(
|
|
464
|
+
[],
|
|
465
|
+
t.blockStatement([
|
|
466
|
+
t.expressionStatement(
|
|
467
|
+
t.assignmentExpression("=", styleProp, val),
|
|
468
|
+
),
|
|
469
|
+
]),
|
|
470
|
+
),
|
|
471
|
+
]),
|
|
472
|
+
),
|
|
473
|
+
);
|
|
474
|
+
} else {
|
|
475
|
+
statements.push(
|
|
476
|
+
t.expressionStatement(
|
|
477
|
+
t.assignmentExpression("=", styleProp, val),
|
|
478
|
+
),
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
// done with this attribute
|
|
483
|
+
} else if (containsSignal(expr, signals)) {
|
|
484
|
+
// Dynamic class with scoped CSS: use classList.value to preserve hash
|
|
485
|
+
if (attrName === "class" && hash) {
|
|
486
|
+
statements.push(
|
|
487
|
+
t.expressionStatement(
|
|
488
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
489
|
+
t.arrowFunctionExpression(
|
|
490
|
+
[],
|
|
491
|
+
t.blockStatement([
|
|
492
|
+
t.expressionStatement(
|
|
493
|
+
t.assignmentExpression(
|
|
494
|
+
"=",
|
|
495
|
+
t.memberExpression(
|
|
496
|
+
t.memberExpression(
|
|
497
|
+
t.identifier(varName),
|
|
498
|
+
t.identifier("classList"),
|
|
499
|
+
),
|
|
500
|
+
t.identifier("value"),
|
|
501
|
+
),
|
|
502
|
+
expr,
|
|
503
|
+
),
|
|
504
|
+
),
|
|
505
|
+
t.expressionStatement(
|
|
506
|
+
t.callExpression(
|
|
507
|
+
t.memberExpression(
|
|
508
|
+
t.memberExpression(
|
|
509
|
+
t.identifier(varName),
|
|
510
|
+
t.identifier("classList"),
|
|
511
|
+
),
|
|
512
|
+
t.identifier("add"),
|
|
513
|
+
),
|
|
514
|
+
[t.stringLiteral(hash)],
|
|
515
|
+
),
|
|
516
|
+
),
|
|
517
|
+
]),
|
|
518
|
+
),
|
|
519
|
+
]),
|
|
520
|
+
),
|
|
521
|
+
);
|
|
522
|
+
} else {
|
|
523
|
+
// createEffect(() => varName.realAttr = expr)
|
|
524
|
+
statements.push(
|
|
525
|
+
t.expressionStatement(
|
|
526
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
527
|
+
t.arrowFunctionExpression(
|
|
528
|
+
[],
|
|
529
|
+
t.blockStatement([
|
|
530
|
+
t.expressionStatement(
|
|
531
|
+
t.assignmentExpression(
|
|
532
|
+
"=",
|
|
533
|
+
attrAccess(varName, realAttr),
|
|
534
|
+
expr,
|
|
535
|
+
),
|
|
536
|
+
),
|
|
537
|
+
]),
|
|
538
|
+
),
|
|
539
|
+
]),
|
|
540
|
+
),
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
} else {
|
|
544
|
+
// varName.realAttr = expr
|
|
545
|
+
statements.push(
|
|
546
|
+
t.expressionStatement(
|
|
547
|
+
t.assignmentExpression(
|
|
548
|
+
"=",
|
|
549
|
+
attrAccess(varName, realAttr),
|
|
550
|
+
expr,
|
|
551
|
+
),
|
|
552
|
+
),
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Add scoped CSS hash to classList (if hash is present)
|
|
559
|
+
if (hash) {
|
|
560
|
+
addScopedClass(varName, hash, statements);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// children
|
|
564
|
+
for (const child of node.children) {
|
|
565
|
+
if (t.isJSXElement(child)) {
|
|
566
|
+
const childVar = processElement(
|
|
567
|
+
child,
|
|
568
|
+
statements,
|
|
569
|
+
genId,
|
|
570
|
+
signals,
|
|
571
|
+
hash,
|
|
572
|
+
hydrate,
|
|
573
|
+
childNodesVar,
|
|
574
|
+
hydrate ? varName : undefined,
|
|
575
|
+
);
|
|
576
|
+
if (!hydrate) {
|
|
577
|
+
statements.push(
|
|
578
|
+
t.expressionStatement(
|
|
579
|
+
t.callExpression(
|
|
580
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
581
|
+
[t.identifier(childVar)],
|
|
582
|
+
),
|
|
583
|
+
),
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
} else if (t.isJSXFragment(child)) {
|
|
587
|
+
const childVar = processFragment(
|
|
588
|
+
child,
|
|
589
|
+
statements,
|
|
590
|
+
genId,
|
|
591
|
+
signals,
|
|
592
|
+
hash,
|
|
593
|
+
hydrate,
|
|
594
|
+
childNodesVar,
|
|
595
|
+
hydrate ? varName : undefined,
|
|
596
|
+
);
|
|
597
|
+
if (!hydrate) {
|
|
598
|
+
statements.push(
|
|
599
|
+
t.expressionStatement(
|
|
600
|
+
t.callExpression(
|
|
601
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
602
|
+
[t.identifier(childVar)],
|
|
603
|
+
),
|
|
604
|
+
),
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
} else if (t.isJSXText(child)) {
|
|
608
|
+
const text = child.value;
|
|
609
|
+
if (!text.trim()) continue;
|
|
610
|
+
if (hydrate) {
|
|
611
|
+
// SSR hydration: text already in DOM from SSR, do nothing.
|
|
612
|
+
// SPA navigation: childNodesVar pool is empty, create text node.
|
|
613
|
+
const poolLen = t.memberExpression(
|
|
614
|
+
t.identifier(childNodesVar!),
|
|
615
|
+
t.identifier("length"),
|
|
616
|
+
);
|
|
617
|
+
statements.push(
|
|
618
|
+
t.ifStatement(
|
|
619
|
+
t.binaryExpression("===", poolLen, t.numericLiteral(0)),
|
|
620
|
+
t.expressionStatement(
|
|
621
|
+
t.callExpression(
|
|
622
|
+
t.memberExpression(
|
|
623
|
+
t.identifier(varName),
|
|
624
|
+
t.identifier("append"),
|
|
625
|
+
),
|
|
626
|
+
[
|
|
627
|
+
t.callExpression(
|
|
628
|
+
t.memberExpression(
|
|
629
|
+
t.identifier("document"),
|
|
630
|
+
t.identifier("createTextNode"),
|
|
631
|
+
),
|
|
632
|
+
[t.stringLiteral(text.replace(/\s*\n\s*/g, " "))],
|
|
633
|
+
),
|
|
634
|
+
],
|
|
635
|
+
),
|
|
636
|
+
),
|
|
637
|
+
),
|
|
638
|
+
);
|
|
639
|
+
} else {
|
|
640
|
+
statements.push(
|
|
641
|
+
t.expressionStatement(
|
|
642
|
+
t.callExpression(
|
|
643
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
644
|
+
[
|
|
645
|
+
t.callExpression(
|
|
646
|
+
t.memberExpression(
|
|
647
|
+
t.identifier("document"),
|
|
648
|
+
t.identifier("createTextNode"),
|
|
649
|
+
),
|
|
650
|
+
[t.stringLiteral(text)],
|
|
651
|
+
),
|
|
652
|
+
],
|
|
653
|
+
),
|
|
654
|
+
),
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
} else if (t.isJSXExpressionContainer(child)) {
|
|
658
|
+
if (t.isJSXEmptyExpression(child.expression)) continue;
|
|
659
|
+
const expr = child.expression as t.Expression;
|
|
660
|
+
if (t.isJSXElement(expr)) {
|
|
661
|
+
const childVar = processElement(
|
|
662
|
+
expr,
|
|
663
|
+
statements,
|
|
664
|
+
genId,
|
|
665
|
+
signals,
|
|
666
|
+
hash,
|
|
667
|
+
hydrate,
|
|
668
|
+
childNodesVar,
|
|
669
|
+
hydrate ? varName : undefined,
|
|
670
|
+
);
|
|
671
|
+
if (!hydrate) {
|
|
672
|
+
statements.push(
|
|
673
|
+
t.expressionStatement(
|
|
674
|
+
t.callExpression(
|
|
675
|
+
t.memberExpression(
|
|
676
|
+
t.identifier(varName),
|
|
677
|
+
t.identifier("append"),
|
|
678
|
+
),
|
|
679
|
+
[t.identifier(childVar)],
|
|
680
|
+
),
|
|
681
|
+
),
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
} else if (t.isJSXFragment(expr)) {
|
|
685
|
+
const childVar = processFragment(
|
|
686
|
+
expr,
|
|
687
|
+
statements,
|
|
688
|
+
genId,
|
|
689
|
+
signals,
|
|
690
|
+
hash,
|
|
691
|
+
hydrate,
|
|
692
|
+
childNodesVar,
|
|
693
|
+
hydrate ? varName : undefined,
|
|
694
|
+
);
|
|
695
|
+
if (!hydrate) {
|
|
696
|
+
statements.push(
|
|
697
|
+
t.expressionStatement(
|
|
698
|
+
t.callExpression(
|
|
699
|
+
t.memberExpression(
|
|
700
|
+
t.identifier(varName),
|
|
701
|
+
t.identifier("append"),
|
|
702
|
+
),
|
|
703
|
+
[t.identifier(childVar)],
|
|
704
|
+
),
|
|
705
|
+
),
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
} else if (containsSignal(expr, signals)) {
|
|
709
|
+
if (isPrimitive(expr)) {
|
|
710
|
+
// fast path: text node + textContent
|
|
711
|
+
buildTextNode(
|
|
712
|
+
varName,
|
|
713
|
+
expr,
|
|
714
|
+
statements,
|
|
715
|
+
genId,
|
|
716
|
+
hydrate,
|
|
717
|
+
childNodesVar,
|
|
718
|
+
hydrate ? varName : undefined,
|
|
719
|
+
);
|
|
720
|
+
} else {
|
|
721
|
+
// Check for keyed list pattern
|
|
722
|
+
const keyed = findKeyedMapExpr(expr, signals);
|
|
723
|
+
if (keyed) {
|
|
724
|
+
buildKeyedList(
|
|
725
|
+
varName,
|
|
726
|
+
keyed,
|
|
727
|
+
statements,
|
|
728
|
+
genId,
|
|
729
|
+
signals,
|
|
730
|
+
processElement,
|
|
731
|
+
hash,
|
|
732
|
+
hydrate,
|
|
733
|
+
childNodesVar,
|
|
734
|
+
hydrate ? varName : undefined,
|
|
735
|
+
);
|
|
736
|
+
} else {
|
|
737
|
+
buildAnchorMount(
|
|
738
|
+
varName,
|
|
739
|
+
expr,
|
|
740
|
+
statements,
|
|
741
|
+
genId,
|
|
742
|
+
hydrate,
|
|
743
|
+
childNodesVar,
|
|
744
|
+
hydrate ? varName : undefined,
|
|
745
|
+
);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
} else {
|
|
749
|
+
// non-reactive: set once
|
|
750
|
+
if (hydrate) {
|
|
751
|
+
const claimCommentArgs: t.Expression[] = [
|
|
752
|
+
t.identifier(childNodesVar!),
|
|
753
|
+
t.stringLiteral("g"),
|
|
754
|
+
];
|
|
755
|
+
if (varName) claimCommentArgs.push(t.identifier(varName));
|
|
756
|
+
const poolLen = t.memberExpression(
|
|
757
|
+
t.identifier(childNodesVar!),
|
|
758
|
+
t.identifier("length"),
|
|
759
|
+
);
|
|
760
|
+
// Pool has elements (initial load): SSR content already in DOM, just consume delimiters
|
|
761
|
+
const claimG = t.expressionStatement(
|
|
762
|
+
t.callExpression(t.identifier("claimComment"), claimCommentArgs),
|
|
763
|
+
);
|
|
764
|
+
const claimSlashG = (() => {
|
|
765
|
+
const args: t.Expression[] = [
|
|
766
|
+
t.identifier(childNodesVar!),
|
|
767
|
+
t.stringLiteral("/g"),
|
|
768
|
+
];
|
|
769
|
+
if (varName) args.push(t.identifier(varName));
|
|
770
|
+
return t.expressionStatement(
|
|
771
|
+
t.callExpression(t.identifier("claimComment"), args),
|
|
772
|
+
);
|
|
773
|
+
})();
|
|
774
|
+
// Pool empty (SPA nav): create and insert fresh elements
|
|
775
|
+
const insertCall = t.expressionStatement(
|
|
776
|
+
t.callExpression(t.identifier("insert"), [
|
|
777
|
+
t.identifier(varName),
|
|
778
|
+
expr,
|
|
779
|
+
]),
|
|
780
|
+
);
|
|
781
|
+
statements.push(
|
|
782
|
+
t.ifStatement(
|
|
783
|
+
t.binaryExpression(">", poolLen, t.numericLiteral(0)),
|
|
784
|
+
t.blockStatement([claimG, claimSlashG]),
|
|
785
|
+
t.blockStatement([insertCall]),
|
|
786
|
+
),
|
|
787
|
+
);
|
|
788
|
+
} else {
|
|
789
|
+
statements.push(
|
|
790
|
+
t.expressionStatement(
|
|
791
|
+
t.callExpression(t.identifier("insert"), [
|
|
792
|
+
t.identifier(varName),
|
|
793
|
+
expr,
|
|
794
|
+
]),
|
|
795
|
+
),
|
|
796
|
+
);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
return varName;
|
|
803
|
+
}
|