@sigil-dev/compiler 0.9.32 → 0.10.0
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 +1 -1
- package/src/babel/index.ts +801 -763
- package/src/babel/jsx/conditional-return.ts +71 -0
- package/src/babel/jsx/keyed-list.ts +5 -1
- package/src/babel/jsx/textarea.ts +58 -0
- package/src/babel/jsx/utils.ts +3 -1
- package/src/babel/util/compile-state.ts +5 -0
- package/src/bun-plugin.ts +16 -3
- package/test/bun-plugin.test.ts +14 -1
- package/test/conditional-return.test.ts +82 -0
- package/test/hydration.test.ts +380 -315
- package/test/render-parity.test.ts +35 -0
package/src/babel/index.ts
CHANGED
|
@@ -1,763 +1,801 @@
|
|
|
1
|
-
import { type PluginObject, types as t, template } from "@babel/core";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
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
|
-
if (v
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
attr
|
|
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
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
let
|
|
149
|
-
let
|
|
150
|
-
|
|
151
|
-
let
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
let
|
|
155
|
-
|
|
156
|
-
let
|
|
157
|
-
let
|
|
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
|
-
const
|
|
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
|
-
if (macro === "$
|
|
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
|
-
if (obj === "$
|
|
511
|
-
didTransform = true;
|
|
512
|
-
path.get("callee").replaceWith(t.identifier(MAGIC.
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
if (obj === "$effect" && prop === "
|
|
517
|
-
didTransform = true;
|
|
518
|
-
path
|
|
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
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
);
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
return;
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
);
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
1
|
+
import { type NodePath, type PluginObject, types as t, template } from "@babel/core";
|
|
2
|
+
import { textareaChildrenToValue } from "./jsx/textarea.ts";
|
|
3
|
+
import { wrapConditionalReturns } from "./jsx/conditional-return.ts";
|
|
4
|
+
import { createResolver, markStaticUses } from "../analysis/hydration.ts";
|
|
5
|
+
import { handleDerived } from "./handlers/dom/derived.ts";
|
|
6
|
+
import { handleEffect } from "./handlers/dom/effect.ts";
|
|
7
|
+
import { handleState } from "./handlers/dom/state.ts";
|
|
8
|
+
import { handleDerivedSSR } from "./handlers/ssr/derived.ts";
|
|
9
|
+
import { handleStateSSR } from "./handlers/ssr/state.ts";
|
|
10
|
+
import { processElement, processFragment } from "./jsx/index.ts";
|
|
11
|
+
import { processElementSSR, processFragmentSSR } from "./jsx/ssr.ts";
|
|
12
|
+
import { SignalScope } from "./jsx/utils.ts";
|
|
13
|
+
import { compileState } from "./util/compile-state.ts";
|
|
14
|
+
import { warnDeadReactivity } from "./util/dead-code.ts";
|
|
15
|
+
import { getMacro } from "./util/helpers.ts";
|
|
16
|
+
import { checkLinks } from "./util/links";
|
|
17
|
+
import { MAGIC } from "./util/magic.ts";
|
|
18
|
+
import { keepOwnerAcrossAwaits } from "./util/owner-awaits.ts";
|
|
19
|
+
import {
|
|
20
|
+
collectImportRoots,
|
|
21
|
+
importRoots,
|
|
22
|
+
liftDestructuredProps,
|
|
23
|
+
propObjects,
|
|
24
|
+
} from "./util/props.ts";
|
|
25
|
+
import { collectReactiveFns } from "./util/reactive-fns.ts";
|
|
26
|
+
import { freeRuntimeNames, RUNTIME_CALLS } from "./util/runtime-names";
|
|
27
|
+
import { warnShadowedGlobals } from "./util/shadowed-globals.ts";
|
|
28
|
+
import { importedSignals, rewriteStateImports } from "./util/state-imports.ts";
|
|
29
|
+
import { applyStaticAssertions } from "./util/static-assertion.ts";
|
|
30
|
+
import { inlineStaticEnv } from "./util/static-env";
|
|
31
|
+
import { SVG_ATTR } from "./util/svg-attrs.ts";
|
|
32
|
+
import { beginWarnings } from "./util/warn.ts";
|
|
33
|
+
|
|
34
|
+
const SSR_HELPERS = template.statements.ast(`
|
|
35
|
+
const __SAFE = Symbol.for('sigil.safe');
|
|
36
|
+
class __H {
|
|
37
|
+
constructor(v) { this.v = v; this[__SAFE] = true; }
|
|
38
|
+
toString() { return this.v; }
|
|
39
|
+
}
|
|
40
|
+
// Component children: rendered when the component outputs them, so
|
|
41
|
+
// inside its call and under its owner (context it sets reaches them).
|
|
42
|
+
class __L {
|
|
43
|
+
constructor(f) { this.f = f; this[__SAFE] = true; }
|
|
44
|
+
get v() { if (this.f) { this.c = __h(this.f()).v; this.f = null; } return this.c; }
|
|
45
|
+
toString() { return this.v; }
|
|
46
|
+
}
|
|
47
|
+
const __lazy = (f) => new __L(f);
|
|
48
|
+
const __h = (v) => {
|
|
49
|
+
if (v?.[__SAFE]) return v;
|
|
50
|
+
if (Array.isArray(v)) return new __H(v.map(x => x?.[__SAFE] ? x.v : __e(x)).join(''));
|
|
51
|
+
return new __H(String(v ?? ''));
|
|
52
|
+
};
|
|
53
|
+
const __r = (v, tag) => {
|
|
54
|
+
if (v === false || v === null || v === undefined) return '';
|
|
55
|
+
return String(v).replace(new RegExp('</(' + tag + ')', 'gi'), '<\\\\/$1');
|
|
56
|
+
};
|
|
57
|
+
const __e = (v) => {
|
|
58
|
+
if (v?.[__SAFE]) return v.v;
|
|
59
|
+
if (Array.isArray(v)) return v.map(x => x?.[__SAFE] ? x.v : __e(x)).join('');
|
|
60
|
+
if (v === false || v === null || v === undefined) return '';
|
|
61
|
+
return String(v).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
|
|
62
|
+
};
|
|
63
|
+
// Keep in sync with cssName/cssValue/UNITLESS in @sigil-dev/runtime.
|
|
64
|
+
const __UNITLESS = new Set(['animation-iteration-count','aspect-ratio','border-image-outset','border-image-slice','border-image-width','column-count','columns','fill-opacity','flex','flex-grow','flex-shrink','flood-opacity','font-weight','grid-area','grid-column','grid-column-end','grid-column-start','grid-row','grid-row-end','grid-row-start','line-clamp','line-height','opacity','order','orphans','scale','stop-opacity','stroke-dasharray','stroke-dashoffset','stroke-miterlimit','stroke-opacity','stroke-width','tab-size','widows','z-index','zoom']);
|
|
65
|
+
const __css = (v) => {
|
|
66
|
+
if (v === null || v === undefined || v === false) return '';
|
|
67
|
+
if (typeof v !== 'object') return String(v);
|
|
68
|
+
return Object.entries(v).map(([k, val]) => {
|
|
69
|
+
let name = k.startsWith('--') ? k : k.replace(/[A-Z]/g, c => '-' + c.toLowerCase());
|
|
70
|
+
if (name.startsWith('ms-')) name = '-' + name;
|
|
71
|
+
if (val === null || val === undefined || val === false || val === '') return '';
|
|
72
|
+
if (typeof val === 'number' && val !== 0 && !name.startsWith('--') && !__UNITLESS.has(name)) val = val + 'px';
|
|
73
|
+
return name + ':' + val;
|
|
74
|
+
}).filter(Boolean).join(';');
|
|
75
|
+
};
|
|
76
|
+
// One attribute. null/undefined drop it; booleans are presence for plain
|
|
77
|
+
// names (checked, hidden) and spelled out for dashed ones (aria-hidden).
|
|
78
|
+
const __a = (n, v) => {
|
|
79
|
+
if (v === null || v === undefined) return '';
|
|
80
|
+
if (n === 'style') { const c = __css(v); return c ? ' style="' + __e(c) + '"' : ''; }
|
|
81
|
+
if (typeof v === 'boolean') return n.includes('-') ? ' ' + n + '="' + v + '"' : v ? ' ' + n : '';
|
|
82
|
+
return ' ' + n + '="' + __e(v) + '"';
|
|
83
|
+
};
|
|
84
|
+
const __SPREAD_ATTR = ${JSON.stringify({ className: "class", htmlFor: "for", ...SVG_ATTR })};
|
|
85
|
+
// <div {...props}>: what the client's spread() would set, as HTML.
|
|
86
|
+
const __s = (o) => {
|
|
87
|
+
if (!o) return '';
|
|
88
|
+
let out = '';
|
|
89
|
+
for (const [k, v] of Object.entries(o)) {
|
|
90
|
+
if (typeof v === 'function' || k === 'children' || k === 'key' || k === 'innerHTML') continue;
|
|
91
|
+
out += __a(__SPREAD_ATTR[k] ?? k, v);
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
};
|
|
95
|
+
// <select value={v}>: mark the matching <option>s selected.
|
|
96
|
+
const __sel = (html, v) => {
|
|
97
|
+
if (v === null || v === undefined) return html;
|
|
98
|
+
const want = new Set((Array.isArray(v) ? v : [v]).map(__e));
|
|
99
|
+
return html.replace(/<option\\b[^>]*>/g, (tag) => {
|
|
100
|
+
const m = / value="([^"]*)"/.exec(tag);
|
|
101
|
+
return m && want.has(m[1]) && !/ selected[ >=]/.test(tag) ? tag.slice(0, -1) + ' selected>' : tag;
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
`);
|
|
105
|
+
/**
|
|
106
|
+
* Recursively convert key={expr} to data-key={expr} in JSX tree.
|
|
107
|
+
* Needed for SSR output so hydration can map items by key.
|
|
108
|
+
*/
|
|
109
|
+
function convertKeyToDataKey(node: t.JSXElement): void {
|
|
110
|
+
for (let i = 0; i < node.openingElement.attributes.length; i++) {
|
|
111
|
+
const attr = node.openingElement.attributes[i];
|
|
112
|
+
if (
|
|
113
|
+
t.isJSXAttribute(attr) &&
|
|
114
|
+
t.isJSXIdentifier(attr.name) &&
|
|
115
|
+
attr.name.name === "key"
|
|
116
|
+
) {
|
|
117
|
+
node.openingElement.attributes[i] = t.jsxAttribute(
|
|
118
|
+
t.jsxIdentifier("data-key"),
|
|
119
|
+
attr.value,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
for (const child of node.children) {
|
|
124
|
+
if (t.isJSXElement(child)) {
|
|
125
|
+
convertKeyToDataKey(child);
|
|
126
|
+
} else if (
|
|
127
|
+
t.isJSXExpressionContainer(child) &&
|
|
128
|
+
t.isJSXElement(child.expression)
|
|
129
|
+
) {
|
|
130
|
+
convertKeyToDataKey(child.expression);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface SigilOptions {
|
|
136
|
+
hash?: string;
|
|
137
|
+
mode?: "dom" | "ssr" | "hydrate";
|
|
138
|
+
dev?: boolean;
|
|
139
|
+
/** Import aliases, prefix to absolute directory (`{ $lib: "/app/src/lib" }`). */
|
|
140
|
+
aliases?: Record<string, string>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export default function sigilPlugin(): PluginObject {
|
|
144
|
+
const signals = new SignalScope();
|
|
145
|
+
const storeSignals = new Set<string>();
|
|
146
|
+
const asyncFunctions = new Map<string, any>();
|
|
147
|
+
|
|
148
|
+
let scopedHash: string | undefined;
|
|
149
|
+
let isSSR = false;
|
|
150
|
+
let isHydrate = false;
|
|
151
|
+
let didTransform = false;
|
|
152
|
+
|
|
153
|
+
let isDev = false;
|
|
154
|
+
let filename = "";
|
|
155
|
+
|
|
156
|
+
let asyncDefaultPath: any = null;
|
|
157
|
+
let _asyncDefaultRef: string | null = null;
|
|
158
|
+
let hasLoadingExport = false;
|
|
159
|
+
let hasErrorExport = false;
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
name: "sigil",
|
|
163
|
+
visitor: {
|
|
164
|
+
Program: {
|
|
165
|
+
enter(path, state) {
|
|
166
|
+
signals.clear();
|
|
167
|
+
storeSignals.clear();
|
|
168
|
+
propObjects.clear();
|
|
169
|
+
importRoots.clear();
|
|
170
|
+
const opts = (state.opts || {}) as SigilOptions;
|
|
171
|
+
scopedHash = opts.hash;
|
|
172
|
+
didTransform = false;
|
|
173
|
+
isSSR = opts.mode === "ssr";
|
|
174
|
+
isHydrate = opts.mode === "hydrate";
|
|
175
|
+
isDev = opts.dev ?? false;
|
|
176
|
+
filename = state.filename ?? "";
|
|
177
|
+
compileState.file = filename;
|
|
178
|
+
beginWarnings(filename, opts.mode ?? "dom");
|
|
179
|
+
asyncDefaultPath = null;
|
|
180
|
+
hasLoadingExport = false;
|
|
181
|
+
hasErrorExport = false;
|
|
182
|
+
_asyncDefaultRef = null;
|
|
183
|
+
asyncFunctions.clear();
|
|
184
|
+
|
|
185
|
+
// Generated code calls the runtime by bare name (`insert(...)`),
|
|
186
|
+
// so a user variable of the same name would capture those calls.
|
|
187
|
+
// Rename the user's instead, before anything is generated.
|
|
188
|
+
freeRuntimeNames(path);
|
|
189
|
+
// literal <a href="/…"> against the app's routes
|
|
190
|
+
checkLinks(path);
|
|
191
|
+
// $env/static values as literals, so decided branches drop out
|
|
192
|
+
inlineStaticEnv(path, opts.mode ?? "dom");
|
|
193
|
+
// `return open ? <A /> : null` from a component stays live
|
|
194
|
+
if (wrapConditionalReturns(path)) didTransform = true;
|
|
195
|
+
|
|
196
|
+
// Before any rewriting, and in every mode: server and browser
|
|
197
|
+
// builds must agree on the verdict.
|
|
198
|
+
applyStaticAssertions(path, filename, state.file.code);
|
|
199
|
+
warnShadowedGlobals(path, filename);
|
|
200
|
+
// Which uses produce only HTML: the server fences them, the
|
|
201
|
+
// browser skips them. Not in `dom` mode, which hydrates nothing.
|
|
202
|
+
compileState.dev = isDev;
|
|
203
|
+
const resolve = createResolver(opts.aliases);
|
|
204
|
+
if (opts.mode === "ssr" || opts.mode === "hydrate") {
|
|
205
|
+
markStaticUses(path, filename, state.file.code, resolve);
|
|
206
|
+
}
|
|
207
|
+
compileState.ssrImports.clear();
|
|
208
|
+
// before anything is rewritten: which local functions build JSX
|
|
209
|
+
compileState.jsxFunctions = collectJsxFunctions(path);
|
|
210
|
+
// an async component's code after an await keeps its owner
|
|
211
|
+
const ownerNames = keepOwnerAcrossAwaits(path);
|
|
212
|
+
if (ownerNames.length) {
|
|
213
|
+
didTransform = true;
|
|
214
|
+
for (const n of ownerNames) compileState.ssrImports.add(n);
|
|
215
|
+
}
|
|
216
|
+
// imported state reads like local state: `count` → `count()`
|
|
217
|
+
if (
|
|
218
|
+
rewriteStateImports(path, filename, signals, resolve, {
|
|
219
|
+
ssr: isSSR,
|
|
220
|
+
dev: isDev,
|
|
221
|
+
})
|
|
222
|
+
)
|
|
223
|
+
didTransform = true;
|
|
224
|
+
|
|
225
|
+
if (isSSR) {
|
|
226
|
+
path.unshiftContainer("body", SSR_HELPERS);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
liftDestructuredProps(path, signals);
|
|
230
|
+
collectReactiveFns(path, signals);
|
|
231
|
+
collectImportRoots(path);
|
|
232
|
+
},
|
|
233
|
+
exit(path, state) {
|
|
234
|
+
warnDeadReactivity(
|
|
235
|
+
path,
|
|
236
|
+
new Set([...storeSignals, ...propObjects, ...importedSignals]),
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
if (asyncDefaultPath && !isSSR) {
|
|
240
|
+
let fnNode: t.FunctionDeclaration;
|
|
241
|
+
|
|
242
|
+
if (asyncDefaultPath.isExportDefaultDeclaration()) {
|
|
243
|
+
// export default async function Page() {}
|
|
244
|
+
fnNode = asyncDefaultPath.get("declaration")
|
|
245
|
+
.node as t.FunctionDeclaration;
|
|
246
|
+
} else {
|
|
247
|
+
// async function Page() {} + export default Page
|
|
248
|
+
fnNode = asyncDefaultPath.node as t.FunctionDeclaration;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const originalName = fnNode.id?.name ?? "Component";
|
|
252
|
+
const privateName = `__${originalName}`;
|
|
253
|
+
const propsId = t.identifier("__props");
|
|
254
|
+
|
|
255
|
+
const privateFn = t.functionDeclaration(
|
|
256
|
+
t.identifier(privateName),
|
|
257
|
+
fnNode.params,
|
|
258
|
+
fnNode.body,
|
|
259
|
+
false,
|
|
260
|
+
true,
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const wrapperExport = t.exportDefaultDeclaration(
|
|
264
|
+
t.functionDeclaration(
|
|
265
|
+
t.identifier(originalName),
|
|
266
|
+
[propsId],
|
|
267
|
+
t.blockStatement([
|
|
268
|
+
t.returnStatement(
|
|
269
|
+
t.callExpression(t.identifier("__asyncBoundary"), [
|
|
270
|
+
t.arrowFunctionExpression(
|
|
271
|
+
[],
|
|
272
|
+
t.callExpression(t.identifier(privateName), [propsId]),
|
|
273
|
+
),
|
|
274
|
+
hasLoadingExport
|
|
275
|
+
? t.identifier("loading")
|
|
276
|
+
: t.identifier("undefined"),
|
|
277
|
+
hasErrorExport
|
|
278
|
+
? t.identifier("error")
|
|
279
|
+
: t.identifier("undefined"),
|
|
280
|
+
]),
|
|
281
|
+
),
|
|
282
|
+
]),
|
|
283
|
+
),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
asyncDefaultPath.replaceWithMultiple([privateFn, wrapperExport]);
|
|
287
|
+
didTransform = true;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Server output only needs the ownership helpers it calls
|
|
291
|
+
if (isSSR) {
|
|
292
|
+
const existing = path.node.body.find(
|
|
293
|
+
(n): n is t.ImportDeclaration =>
|
|
294
|
+
t.isImportDeclaration(n) &&
|
|
295
|
+
n.source.value === "@sigil-dev/runtime" &&
|
|
296
|
+
n.importKind !== "type",
|
|
297
|
+
);
|
|
298
|
+
const have = new Set(
|
|
299
|
+
existing?.specifiers.map((sp) => sp.local.name) ?? [],
|
|
300
|
+
);
|
|
301
|
+
const names = [...compileState.ssrImports].filter(
|
|
302
|
+
(n) => !have.has(n),
|
|
303
|
+
);
|
|
304
|
+
if (existing) {
|
|
305
|
+
for (const n of names)
|
|
306
|
+
existing.specifiers.push(
|
|
307
|
+
t.importSpecifier(t.identifier(n), t.identifier(n)),
|
|
308
|
+
);
|
|
309
|
+
} else if (names.length) {
|
|
310
|
+
path.unshiftContainer(
|
|
311
|
+
"body",
|
|
312
|
+
t.importDeclaration(
|
|
313
|
+
names.map((n) =>
|
|
314
|
+
t.importSpecifier(t.identifier(n), t.identifier(n)),
|
|
315
|
+
),
|
|
316
|
+
t.stringLiteral("@sigil-dev/runtime"),
|
|
317
|
+
),
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
// Only inject runtime import if this file actually used sigil transforms
|
|
323
|
+
if (!didTransform) return;
|
|
324
|
+
|
|
325
|
+
const requiredSpecifiers = [
|
|
326
|
+
...RUNTIME_CALLS,
|
|
327
|
+
...(asyncDefaultPath ? ["__asyncBoundary"] : []),
|
|
328
|
+
];
|
|
329
|
+
const runtimeImport = path.node.body.find(
|
|
330
|
+
(n): n is t.ImportDeclaration =>
|
|
331
|
+
t.isImportDeclaration(n) &&
|
|
332
|
+
n.source.value === "@sigil-dev/runtime",
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
if (runtimeImport) {
|
|
336
|
+
// console.log(
|
|
337
|
+
// "[sigil exit] found existing import, specifier count:",
|
|
338
|
+
// runtimeImport.specifiers.length,
|
|
339
|
+
// );
|
|
340
|
+
const existingNames = new Set(
|
|
341
|
+
runtimeImport.specifiers
|
|
342
|
+
.filter(t.isImportSpecifier)
|
|
343
|
+
.map((s) =>
|
|
344
|
+
t.isIdentifier(s.imported)
|
|
345
|
+
? s.imported.name
|
|
346
|
+
: (s.imported as t.StringLiteral).value,
|
|
347
|
+
),
|
|
348
|
+
);
|
|
349
|
+
for (const name of requiredSpecifiers) {
|
|
350
|
+
if (!existingNames.has(name)) {
|
|
351
|
+
runtimeImport.specifiers.push(
|
|
352
|
+
t.importSpecifier(t.identifier(name), t.identifier(name)),
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
} else {
|
|
357
|
+
path.unshiftContainer(
|
|
358
|
+
"body",
|
|
359
|
+
t.importDeclaration(
|
|
360
|
+
requiredSpecifiers.map((name) =>
|
|
361
|
+
t.importSpecifier(t.identifier(name), t.identifier(name)),
|
|
362
|
+
),
|
|
363
|
+
t.stringLiteral("@sigil-dev/runtime"),
|
|
364
|
+
),
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
VariableDeclaration(path) {
|
|
370
|
+
for (const declarator of path.get("declarations")) {
|
|
371
|
+
const macro = getMacro(declarator);
|
|
372
|
+
if (!macro) continue;
|
|
373
|
+
didTransform = true;
|
|
374
|
+
const { name, init } = macro;
|
|
375
|
+
const varName = t.isIdentifier(declarator.node.id)
|
|
376
|
+
? declarator.node.id.name
|
|
377
|
+
: null;
|
|
378
|
+
|
|
379
|
+
if (name === "$state" || name === "$store" || name === "$state.raw") {
|
|
380
|
+
isSSR
|
|
381
|
+
? handleStateSSR(path, declarator, init, signals)
|
|
382
|
+
: handleState(
|
|
383
|
+
path,
|
|
384
|
+
declarator,
|
|
385
|
+
init,
|
|
386
|
+
signals,
|
|
387
|
+
isDev,
|
|
388
|
+
filename,
|
|
389
|
+
name === "$state.raw",
|
|
390
|
+
);
|
|
391
|
+
if (name === "$store" && varName) {
|
|
392
|
+
storeSignals.add(varName);
|
|
393
|
+
}
|
|
394
|
+
} else if (name === "$derived") {
|
|
395
|
+
isSSR
|
|
396
|
+
? handleDerivedSSR(path, declarator, init, signals)
|
|
397
|
+
: handleDerived(path, declarator, init, signals);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
ExpressionStatement(path) {
|
|
402
|
+
if (isSSR) {
|
|
403
|
+
// drop $effect, $effect.pre, $inspect (with or without .with),
|
|
404
|
+
// and $debug entirely in SSR: none of them run on the server
|
|
405
|
+
const expr = path.get("expression");
|
|
406
|
+
if (expr.isCallExpression()) {
|
|
407
|
+
const callee = expr.get("callee");
|
|
408
|
+
const dropped =
|
|
409
|
+
(callee.isIdentifier() &&
|
|
410
|
+
(callee.node.name === "$effect" ||
|
|
411
|
+
callee.node.name === "$inspect" ||
|
|
412
|
+
callee.node.name === "$debug")) ||
|
|
413
|
+
(callee.isMemberExpression() &&
|
|
414
|
+
t.isIdentifier(callee.node.object, { name: "$effect" }) &&
|
|
415
|
+
t.isIdentifier(callee.node.property, { name: "pre" })) ||
|
|
416
|
+
(callee.isMemberExpression() &&
|
|
417
|
+
t.isIdentifier(callee.node.property, { name: "with" }) &&
|
|
418
|
+
t.isCallExpression(callee.node.object) &&
|
|
419
|
+
t.isIdentifier(callee.node.object.callee, {
|
|
420
|
+
name: "$inspect",
|
|
421
|
+
}));
|
|
422
|
+
if (dropped) {
|
|
423
|
+
didTransform = true;
|
|
424
|
+
path.remove();
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
handleEffect(path);
|
|
430
|
+
// Handle $debug(expr) expression statements
|
|
431
|
+
const expr = path.get("expression");
|
|
432
|
+
if (expr.isCallExpression()) {
|
|
433
|
+
const callee = expr.get("callee");
|
|
434
|
+
if (callee.isIdentifier({ name: "$debug" })) {
|
|
435
|
+
didTransform = true;
|
|
436
|
+
path.replaceWith(
|
|
437
|
+
t.ifStatement(
|
|
438
|
+
t.memberExpression(
|
|
439
|
+
t.memberExpression(
|
|
440
|
+
t.memberExpression(
|
|
441
|
+
t.identifier("import"),
|
|
442
|
+
t.identifier("meta"),
|
|
443
|
+
),
|
|
444
|
+
t.identifier("env"),
|
|
445
|
+
),
|
|
446
|
+
t.identifier("DEV"),
|
|
447
|
+
),
|
|
448
|
+
t.blockStatement([
|
|
449
|
+
t.debuggerStatement(),
|
|
450
|
+
t.expressionStatement(
|
|
451
|
+
t.callExpression(
|
|
452
|
+
t.memberExpression(
|
|
453
|
+
t.identifier("console"),
|
|
454
|
+
t.identifier("log"),
|
|
455
|
+
),
|
|
456
|
+
expr.node.arguments,
|
|
457
|
+
),
|
|
458
|
+
),
|
|
459
|
+
]),
|
|
460
|
+
),
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
},
|
|
465
|
+
CallExpression(path) {
|
|
466
|
+
if (isSSR) {
|
|
467
|
+
const callee = path.get("callee");
|
|
468
|
+
if (
|
|
469
|
+
callee.isMemberExpression() &&
|
|
470
|
+
t.isIdentifier(callee.node.property) &&
|
|
471
|
+
(t.isIdentifier(callee.node.object, { name: "$effect" }) ||
|
|
472
|
+
t.isIdentifier(callee.node.object, { name: "$state" }))
|
|
473
|
+
) {
|
|
474
|
+
const macro = `${(callee.node.object as t.Identifier).name}.${callee.node.property.name}`;
|
|
475
|
+
// nothing is tracked while rendering on the server
|
|
476
|
+
if (macro === "$effect.tracking") {
|
|
477
|
+
didTransform = true;
|
|
478
|
+
path.replaceWith(t.booleanLiteral(false));
|
|
479
|
+
}
|
|
480
|
+
// server state holds plain values, and snapshot() returns a
|
|
481
|
+
// value that isn't a proxy unchanged, so the value is the snapshot
|
|
482
|
+
if (macro === "$state.snapshot") {
|
|
483
|
+
const [arg] = path.node.arguments;
|
|
484
|
+
didTransform = true;
|
|
485
|
+
path.replaceWith(
|
|
486
|
+
arg && t.isExpression(arg) ? arg : t.identifier("undefined"),
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
const callee = path.get("callee");
|
|
493
|
+
|
|
494
|
+
// Helper: wrap signal identifiers in call expressions
|
|
495
|
+
const wrapSignal = (expr: t.Expression): t.Expression => {
|
|
496
|
+
if (t.isIdentifier(expr) && signals.has(expr.name)) {
|
|
497
|
+
return t.callExpression(expr, []);
|
|
498
|
+
}
|
|
499
|
+
return expr;
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
// Handle member expression callees: $state.snapshot(x), $effect.tracking(), $effect.pre(fn)
|
|
503
|
+
if (callee.isMemberExpression()) {
|
|
504
|
+
const object = callee.get("object");
|
|
505
|
+
const property = callee.get("property");
|
|
506
|
+
if (object.isIdentifier() && property.isIdentifier()) {
|
|
507
|
+
const obj = object.node.name;
|
|
508
|
+
const prop = property.node.name;
|
|
509
|
+
|
|
510
|
+
if (obj === "$state" && prop === "snapshot") {
|
|
511
|
+
didTransform = true;
|
|
512
|
+
path.get("callee").replaceWith(t.identifier(MAGIC.snapshot));
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (obj === "$effect" && prop === "tracking") {
|
|
517
|
+
didTransform = true;
|
|
518
|
+
path.get("callee").replaceWith(t.identifier(MAGIC.tracking));
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (obj === "$effect" && prop === "pre") {
|
|
523
|
+
didTransform = true;
|
|
524
|
+
path
|
|
525
|
+
.get("callee")
|
|
526
|
+
.replaceWith(t.identifier(MAGIC.createEffectPre));
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// Handle $inspect(count).with(cb) chain
|
|
533
|
+
if (callee.isMemberExpression()) {
|
|
534
|
+
const property = callee.get("property");
|
|
535
|
+
if (property.isIdentifier({ name: "with" })) {
|
|
536
|
+
const object = callee.get("object");
|
|
537
|
+
if (object.isCallExpression()) {
|
|
538
|
+
const innerCallee = object.get("callee");
|
|
539
|
+
if (innerCallee.isIdentifier({ name: "$inspect" })) {
|
|
540
|
+
didTransform = true;
|
|
541
|
+
//@ts-expect-error Bizzare but it works if you read LSP
|
|
542
|
+
const args = object.node.arguments.map(wrapSignal);
|
|
543
|
+
const cb = path.node.arguments[0];
|
|
544
|
+
path.replaceWith(
|
|
545
|
+
t.callExpression(t.identifier(MAGIC.createInspectEffect), [
|
|
546
|
+
t.arrowFunctionExpression([], t.arrayExpression(args)),
|
|
547
|
+
//@ts-expect-error Bizzare but it works if you read LSP
|
|
548
|
+
cb,
|
|
549
|
+
]),
|
|
550
|
+
);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// Handle standalone $debug(...) — not as expression statement
|
|
558
|
+
if (callee.isIdentifier({ name: "$debug" })) {
|
|
559
|
+
didTransform = true;
|
|
560
|
+
path.replaceWith(
|
|
561
|
+
t.ifStatement(
|
|
562
|
+
t.memberExpression(
|
|
563
|
+
t.memberExpression(
|
|
564
|
+
t.memberExpression(
|
|
565
|
+
t.identifier("import"),
|
|
566
|
+
t.identifier("meta"),
|
|
567
|
+
),
|
|
568
|
+
t.identifier("env"),
|
|
569
|
+
),
|
|
570
|
+
t.identifier("DEV"),
|
|
571
|
+
),
|
|
572
|
+
t.blockStatement([
|
|
573
|
+
t.debuggerStatement(),
|
|
574
|
+
t.expressionStatement(
|
|
575
|
+
t.callExpression(
|
|
576
|
+
t.memberExpression(
|
|
577
|
+
t.identifier("console"),
|
|
578
|
+
t.identifier("log"),
|
|
579
|
+
),
|
|
580
|
+
path.node.arguments,
|
|
581
|
+
),
|
|
582
|
+
),
|
|
583
|
+
]),
|
|
584
|
+
),
|
|
585
|
+
);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Handle standalone $inspect(...)
|
|
590
|
+
if (callee.isIdentifier({ name: "$inspect" })) {
|
|
591
|
+
didTransform = true;
|
|
592
|
+
//@ts-expect-error Bizzare but it works if you read LSP
|
|
593
|
+
const args = path.node.arguments.map(wrapSignal);
|
|
594
|
+
path.replaceWith(
|
|
595
|
+
t.callExpression(t.identifier(MAGIC.createInspectEffect), [
|
|
596
|
+
t.arrowFunctionExpression([], t.arrayExpression(args)),
|
|
597
|
+
]),
|
|
598
|
+
);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
},
|
|
602
|
+
JSXElement(path) {
|
|
603
|
+
didTransform = true;
|
|
604
|
+
// console.log(
|
|
605
|
+
// "[sigil] JSXElement visitor fired, parent:",
|
|
606
|
+
// path.parentPath?.type,
|
|
607
|
+
// "isSSR:", isSSR
|
|
608
|
+
// );
|
|
609
|
+
if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
|
|
610
|
+
return;
|
|
611
|
+
textareaChildrenToValue(path.node);
|
|
612
|
+
if (isSSR) {
|
|
613
|
+
try {
|
|
614
|
+
convertKeyToDataKey(path.node);
|
|
615
|
+
path.replaceWith(
|
|
616
|
+
t.callExpression(t.identifier("__h"), [
|
|
617
|
+
processElementSSR(path.node, signals, scopedHash),
|
|
618
|
+
]),
|
|
619
|
+
);
|
|
620
|
+
} catch (e) {
|
|
621
|
+
console.error("[sigil] processElementSSR threw:", path.toString());
|
|
622
|
+
console.error(e);
|
|
623
|
+
throw e;
|
|
624
|
+
}
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const statements: t.Statement[] = [];
|
|
629
|
+
const genId = (() => {
|
|
630
|
+
let i = 0;
|
|
631
|
+
return () => `_el${i++}`;
|
|
632
|
+
})();
|
|
633
|
+
|
|
634
|
+
const root = processElement(
|
|
635
|
+
path.node,
|
|
636
|
+
statements,
|
|
637
|
+
genId,
|
|
638
|
+
signals,
|
|
639
|
+
scopedHash,
|
|
640
|
+
isHydrate,
|
|
641
|
+
isHydrate ? "__nodes" : undefined,
|
|
642
|
+
);
|
|
643
|
+
|
|
644
|
+
const body: t.Statement[] = [];
|
|
645
|
+
|
|
646
|
+
if (isHydrate) {
|
|
647
|
+
// DECLARE __nodes locally — every IIFE owns its own context
|
|
648
|
+
body.push(
|
|
649
|
+
t.variableDeclaration("const", [
|
|
650
|
+
t.variableDeclarator(
|
|
651
|
+
t.identifier("__nodes"),
|
|
652
|
+
t.callExpression(t.identifier("getHydrationNodes"), []),
|
|
653
|
+
),
|
|
654
|
+
]),
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
body.push(...statements);
|
|
659
|
+
body.push(t.returnStatement(t.identifier(root)));
|
|
660
|
+
|
|
661
|
+
path.replaceWith(
|
|
662
|
+
t.callExpression(
|
|
663
|
+
t.arrowFunctionExpression([], t.blockStatement(body)),
|
|
664
|
+
[],
|
|
665
|
+
),
|
|
666
|
+
);
|
|
667
|
+
},
|
|
668
|
+
JSXFragment(path) {
|
|
669
|
+
didTransform = true;
|
|
670
|
+
if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
|
|
671
|
+
return;
|
|
672
|
+
textareaChildrenToValue(path.node);
|
|
673
|
+
if (isSSR) {
|
|
674
|
+
path.replaceWith(
|
|
675
|
+
t.callExpression(t.identifier("__h"), [
|
|
676
|
+
processFragmentSSR(path.node, signals, scopedHash),
|
|
677
|
+
]),
|
|
678
|
+
);
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const statements: t.Statement[] = [];
|
|
683
|
+
const genId = (() => {
|
|
684
|
+
let i = 0;
|
|
685
|
+
return () => `_el${i++}`;
|
|
686
|
+
})();
|
|
687
|
+
|
|
688
|
+
const root = processFragment(
|
|
689
|
+
path.node,
|
|
690
|
+
statements,
|
|
691
|
+
genId,
|
|
692
|
+
signals,
|
|
693
|
+
scopedHash,
|
|
694
|
+
isHydrate,
|
|
695
|
+
isHydrate ? "__nodes" : undefined,
|
|
696
|
+
);
|
|
697
|
+
|
|
698
|
+
const body: t.Statement[] = [];
|
|
699
|
+
|
|
700
|
+
if (isHydrate) {
|
|
701
|
+
// DECLARE __nodes locally — every IIFE owns its own context
|
|
702
|
+
body.push(
|
|
703
|
+
t.variableDeclaration("const", [
|
|
704
|
+
t.variableDeclarator(
|
|
705
|
+
t.identifier("__nodes"),
|
|
706
|
+
t.callExpression(t.identifier("getHydrationNodes"), []),
|
|
707
|
+
),
|
|
708
|
+
]),
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
body.push(...statements);
|
|
713
|
+
body.push(t.returnStatement(t.identifier(root)));
|
|
714
|
+
|
|
715
|
+
path.replaceWith(
|
|
716
|
+
t.callExpression(
|
|
717
|
+
t.arrowFunctionExpression([], t.blockStatement(body)),
|
|
718
|
+
[],
|
|
719
|
+
),
|
|
720
|
+
);
|
|
721
|
+
},
|
|
722
|
+
FunctionDeclaration(path) {
|
|
723
|
+
if (isSSR) return;
|
|
724
|
+
const node = path.node;
|
|
725
|
+
if (node.async && node.id) {
|
|
726
|
+
asyncFunctions.set(node.id.name, path);
|
|
727
|
+
}
|
|
728
|
+
},
|
|
729
|
+
|
|
730
|
+
ExportDefaultDeclaration(path) {
|
|
731
|
+
const decl = path.get("declaration");
|
|
732
|
+
|
|
733
|
+
// export default async function Page() {}
|
|
734
|
+
if (
|
|
735
|
+
(decl.isFunctionDeclaration() || decl.isFunctionExpression()) &&
|
|
736
|
+
(decl.node as any).async &&
|
|
737
|
+
!isSSR
|
|
738
|
+
) {
|
|
739
|
+
asyncDefaultPath = path;
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// export default Page
|
|
744
|
+
if (decl.isIdentifier() && !isSSR) {
|
|
745
|
+
const name = (decl.node as t.Identifier).name;
|
|
746
|
+
if (asyncFunctions.has(name)) {
|
|
747
|
+
asyncDefaultPath = asyncFunctions.get(name);
|
|
748
|
+
_asyncDefaultRef = name;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
|
|
753
|
+
ExportNamedDeclaration(path) {
|
|
754
|
+
const decl = path.get("declaration");
|
|
755
|
+
if (decl.isFunctionDeclaration()) {
|
|
756
|
+
const name = (decl.node as t.FunctionDeclaration).id?.name;
|
|
757
|
+
if (name === "loading") hasLoadingExport = true;
|
|
758
|
+
if (name === "error") hasErrorExport = true;
|
|
759
|
+
}
|
|
760
|
+
if (decl.isVariableDeclaration()) {
|
|
761
|
+
for (const d of decl.node.declarations) {
|
|
762
|
+
if (t.isIdentifier(d.id)) {
|
|
763
|
+
if (d.id.name === "loading") hasLoadingExport = true;
|
|
764
|
+
if (d.id.name === "error") hasErrorExport = true;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
},
|
|
769
|
+
},
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/** Names of the file's functions whose own body produces JSX. */
|
|
774
|
+
function collectJsxFunctions(program: NodePath<t.Program>): Set<string> {
|
|
775
|
+
const names = new Set<string>();
|
|
776
|
+
const builds = (fn: NodePath) => {
|
|
777
|
+
let found = false;
|
|
778
|
+
fn.traverse({
|
|
779
|
+
JSXElement(p) {
|
|
780
|
+
if (p.getFunctionParent() === fn) found = true;
|
|
781
|
+
p.skip();
|
|
782
|
+
},
|
|
783
|
+
JSXFragment(p) {
|
|
784
|
+
if (p.getFunctionParent() === fn) found = true;
|
|
785
|
+
p.skip();
|
|
786
|
+
},
|
|
787
|
+
});
|
|
788
|
+
return found;
|
|
789
|
+
};
|
|
790
|
+
program.traverse({
|
|
791
|
+
FunctionDeclaration(p) {
|
|
792
|
+
if (p.node.id && builds(p)) names.add(p.node.id.name);
|
|
793
|
+
},
|
|
794
|
+
VariableDeclarator(p) {
|
|
795
|
+
const init = p.get("init");
|
|
796
|
+
if (!t.isIdentifier(p.node.id) || !(init.isArrowFunctionExpression() || init.isFunctionExpression())) return;
|
|
797
|
+
if (builds(init as NodePath)) names.add(p.node.id.name);
|
|
798
|
+
},
|
|
799
|
+
});
|
|
800
|
+
return names;
|
|
801
|
+
}
|