1
2
3
4
5
6
7
8
9
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
83
84
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
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
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
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
|
usingnamespace @import("global.zig");
const std = @import("std");
const lex = @import("js_lexer.zig");
const logger = @import("logger.zig");
const options = @import("options.zig");
const js_parser = @import("js_parser.zig");
const json_parser = @import("json_parser.zig");
const js_printer = @import("js_printer.zig");
const js_ast = @import("js_ast.zig");
const linker = @import("linker.zig");
usingnamespace @import("ast/base.zig");
usingnamespace @import("defines.zig");
const panicky = @import("panic_handler.zig");
const Fs = @import("fs.zig");
const schema = @import("api/schema.zig");
const Api = schema.Api;
const _resolver = @import("./resolver/resolver.zig");
const sync = @import("sync.zig");
const ImportRecord = @import("./import_record.zig").ImportRecord;
const allocators = @import("./allocators.zig");
const MimeType = @import("./http/mime_type.zig");
const resolve_path = @import("./resolver/resolve_path.zig");
const runtime = @import("./runtime.zig");
const Timer = @import("./timer.zig");
const hash_map = @import("hash_map.zig");
const PackageJSON = @import("./resolver/package_json.zig").PackageJSON;
const MacroRemap = @import("./resolver/package_json.zig").MacroMap;
const DebugLogs = _resolver.DebugLogs;
const NodeModuleBundle = @import("./node_module_bundle.zig").NodeModuleBundle;
const Router = @import("./router.zig");
const isPackagePath = _resolver.isPackagePath;
const Css = @import("css_scanner.zig");
const DotEnv = @import("./env_loader.zig");
const Lock = @import("./lock.zig").Lock;
const NewBunQueue = @import("./bun_queue.zig").NewBunQueue;
const NodeFallbackModules = @import("./node_fallbacks.zig");
const CacheEntry = @import("./cache.zig").FsCacheEntry;
const Analytics = @import("./analytics/analytics_thread.zig");
const Linker = linker.Linker;
const Resolver = _resolver.Resolver;
// How it works end-to-end
// 1. Resolve a file path from input using the resolver
// 2. Look at the extension of that file path, and determine a loader
// 3. If the loader is .js, .jsx, .ts, .tsx, or .json, run it through our JavaScript Parser
// IF serving via HTTP and it's parsed without errors:
// 4. If parsed without errors, generate a strong ETag & write the output to a buffer that sends to the in the Printer.
// 4. Else, write any errors to error page (which doesn't exist yet)
// IF writing to disk AND it's parsed without errors:
// 4. Write the output to a temporary file.
// Why? Two reasons.
// 1. At this point, we don't know what the best output path is.
// Most of the time, you want the shortest common path, which you can't know until you've
// built & resolved all paths.
// Consider this directory tree:
// - /Users/jarred/Code/app/src/index.tsx
// - /Users/jarred/Code/app/src/Button.tsx
// - /Users/jarred/Code/app/assets/logo.png
// - /Users/jarred/Code/app/src/Button.css
// - /Users/jarred/Code/app/node_modules/react/index.js
// - /Users/jarred/Code/app/node_modules/react/cjs/react.development.js
// Remember that we cannot know which paths need to be resolved without parsing the JavaScript.
// If we stopped here: /Users/jarred/Code/app/src/Button.tsx
// We would choose /Users/jarred/Code/app/src/ as the directory
// Then, that would result in a directory structure like this:
// - /Users/jarred/Code/app/src/Users/jarred/Code/app/node_modules/react/cjs/react.development.js
// Which is absolutely insane
//
// 2. We will need to write to disk at some point!
// - If we delay writing to disk, we need to print & allocate a potentially quite large
// buffer (react-dom.development.js is 550 KB)
// ^ This is how it used to work!
// - If we delay printing, we need to keep the AST around. Which breaks all our
// memory-saving recycling logic since that could be many many ASTs.
// 5. Once all files are written, determine the shortest common path
// 6. Move all the temporary files to their intended destinations
// IF writing to disk AND it's a file-like loader
// 4. Hash the contents
// - rewrite_paths.put(absolute_path, hash(file(absolute_path)))
// 5. Resolve any imports of this file to that hash(file(absolute_path))
// 6. Append to the files array with the new filename
// 7. When parsing & resolving is over, just copy the file.
// - on macOS, ensure it does an APFS shallow clone so that doesn't use disk space (only possible if file doesn't already exist)
// fclonefile
// IF serving via HTTP AND it's a file-like loader:
// 4. Use os.sendfile so copying/reading the file happens in the kernel instead of in Bun.
// This unfortunately means content hashing for HTTP server is unsupported, but metadata etags work
// For each imported file, GOTO 1.
pub const ParseResult = struct {
source: logger.Source,
loader: options.Loader,
ast: js_ast.Ast,
input_fd: ?StoredFileDescriptorType = null,
empty: bool = false,
};
const cache_files = false;
pub const Bundler = struct {
const ThisBundler = @This();
options: options.BundleOptions,
log: *logger.Log,
allocator: *std.mem.Allocator,
result: options.TransformResult = undefined,
resolver: Resolver,
fs: *Fs.FileSystem,
// thread_pool: *ThreadPool,
output_files: std.ArrayList(options.OutputFile),
resolve_results: *ResolveResults,
resolve_queue: ResolveQueue,
elapsed: u64 = 0,
needs_runtime: bool = false,
router: ?Router = null,
linker: Linker,
timer: std.time.Timer = undefined,
env: *DotEnv.Loader,
// must be pointer array because we can't we don't want the source to point to invalid memory if the array size is reallocated
virtual_modules: std.ArrayList(*ClientEntryPoint),
macro_context: ?js_ast.Macro.MacroContext = null,
pub const isCacheEnabled = cache_files;
pub fn clone(this: *ThisBundler, allocator: *std.mem.Allocator, to: *ThisBundler) !void {
to.* = this.*;
to.setAllocator(allocator);
to.log = try allocator.create(logger.Log);
to.log.* = logger.Log.init(allocator);
to.setLog(to.log);
to.macro_context = null;
}
pub fn setLog(this: *ThisBundler, log: *logger.Log) void {
this.log = log;
this.linker.log = log;
this.resolver.log = log;
}
pub fn setAllocator(this: *ThisBundler, allocator: *std.mem.Allocator) void {
this.allocator = allocator;
this.linker.allocator = allocator;
this.resolver.allocator = allocator;
}
pub inline fn resolveEntryPoint(bundler: *ThisBundler, entry_point: string) !_resolver.Result {
return bundler.resolver.resolve(bundler.fs.top_level_dir, entry_point, .entry_point) catch |err| {
const has_dot_slash_form = !strings.hasPrefix(entry_point, "./") and brk: {
_ = bundler.resolver.resolve(bundler.fs.top_level_dir, try strings.append(bundler.allocator, "./", entry_point), .entry_point) catch break :brk false;
break :brk true;
};
if (has_dot_slash_form) {
bundler.log.addErrorFmt(null, logger.Loc.Empty, bundler.allocator, "{s} resolving \"{s}\". Did you mean: \"./{s}\"", .{
@errorName(err),
entry_point,
entry_point,
}) catch unreachable;
} else {
bundler.log.addErrorFmt(null, logger.Loc.Empty, bundler.allocator, "{s} resolving \"{s}\" (entry point)", .{ @errorName(err), entry_point }) catch unreachable;
}
return err;
};
}
// to_bundle:
// thread_pool: *ThreadPool,
pub fn init(
allocator: *std.mem.Allocator,
log: *logger.Log,
opts: Api.TransformOptions,
existing_bundle: ?*NodeModuleBundle,
env_loader_: ?*DotEnv.Loader,
) !ThisBundler {
js_ast.Expr.Data.Store.create(allocator);
js_ast.Stmt.Data.Store.create(allocator);
var fs = try Fs.FileSystem.init1(
allocator,
opts.absolute_working_dir,
);
const bundle_options = try options.BundleOptions.fromApi(
allocator,
fs,
log,
opts,
existing_bundle,
);
var env_loader = env_loader_ orelse brk: {
var map = try allocator.create(DotEnv.Map);
map.* = DotEnv.Map.init(allocator);
var loader = try allocator.create(DotEnv.Loader);
loader.* = DotEnv.Loader.init(map, allocator);
break :brk loader;
};
DotEnv.instance = env_loader;
// var pool = try allocator.create(ThreadPool);
// try pool.init(ThreadPool.InitConfig{
// .allocator = allocator,
// });
var resolve_results = try allocator.create(ResolveResults);
resolve_results.* = ResolveResults.init(allocator);
return ThisBundler{
.options = bundle_options,
.fs = fs,
.allocator = allocator,
.timer = std.time.Timer.start() catch @panic("Timer fail"),
.resolver = Resolver.init1(allocator, log, fs, bundle_options),
.log = log,
// .thread_pool = pool,
.linker = undefined,
.result = options.TransformResult{ .outbase = bundle_options.output_dir },
.resolve_results = resolve_results,
.resolve_queue = ResolveQueue.init(allocator),
.output_files = std.ArrayList(options.OutputFile).init(allocator),
.virtual_modules = std.ArrayList(*ClientEntryPoint).init(allocator),
.env = env_loader,
};
}
pub fn configureLinker(bundler: *ThisBundler) void {
bundler.linker = Linker.init(
bundler.allocator,
bundler.log,
&bundler.resolve_queue,
&bundler.options,
&bundler.resolver,
bundler.resolve_results,
bundler.fs,
);
// If we don't explicitly pass JSX, try to get it from the root tsconfig
if (bundler.options.transform_options.jsx == null) {
// Most of the time, this will already be cached
if (bundler.resolver.readDirInfo(bundler.fs.top_level_dir) catch null) |root_dir| {
if (root_dir.tsconfig_json) |tsconfig| {
bundler.options.jsx = tsconfig.jsx;
}
}
}
}
pub fn runEnvLoader(this: *ThisBundler) !void {
switch (this.options.env.behavior) {
.prefix, .load_all => {
// Step 1. Load the project root.
var dir: *Fs.FileSystem.DirEntry = ((this.resolver.readDirInfo(this.fs.top_level_dir) catch return) orelse return).getEntries() orelse return;
// Process always has highest priority.
this.env.loadProcess();
if (this.options.production) {
try this.env.load(&this.fs.fs, dir, false);
} else {
try this.env.load(&this.fs.fs, dir, true);
}
},
.disable => {
this.env.loadProcess();
},
else => {},
}
if (this.env.map.get("DISABLE_BUN_ANALYTICS")) |should_disable| {
if (strings.eqlComptime(should_disable, "1")) {
Analytics.disabled = true;
}
}
if (this.env.map.get("CI") orelse
this.env.map.get("TDDIUM") orelse
this.env.map.get("JENKINS_URL") orelse
this.env.map.get("bamboo.buildKey")) |IS_CI|
{
Analytics.is_ci = true;
}
Analytics.disabled = Analytics.disabled or this.env.map.get("HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET") != null;
}
// This must be run after a framework is configured, if a framework is enabled
pub fn configureDefines(this: *ThisBundler) !void {
if (this.options.defines_loaded) {
return;
}
if (this.options.platform == .bun_macro) {
this.options.env.behavior = .prefix;
this.options.env.prefix = "BUN_";
}
try this.runEnvLoader();
js_ast.Expr.Data.Store.create(this.allocator);
js_ast.Stmt.Data.Store.create(this.allocator);
defer js_ast.Expr.Data.Store.reset();
defer js_ast.Stmt.Data.Store.reset();
if (this.options.framework) |framework| {
if (this.options.platform.isClient()) {
try this.options.loadDefines(this.allocator, this.env, &framework.client.env);
} else {
try this.options.loadDefines(this.allocator, this.env, &framework.server.env);
}
} else {
try this.options.loadDefines(this.allocator, this.env, &this.options.env);
}
}
pub fn configureFramework(
this: *ThisBundler,
comptime load_defines: bool,
) !void {
if (this.options.framework) |*framework| {
if (framework.needsResolveFromPackage()) {
var route_config = this.options.routes;
var pair = PackageJSON.FrameworkRouterPair{ .framework = framework, .router = &route_config };
if (framework.development) {
try this.resolver.resolveFramework(framework.package, &pair, .development, load_defines);
} else {
try this.resolver.resolveFramework(framework.package, &pair, .production, load_defines);
}
if (this.options.areDefinesUnset()) {
if (this.options.platform.isClient()) {
this.options.env = framework.client.env;
} else {
this.options.env = framework.server.env;
}
}
if (pair.loaded_routes) {
this.options.routes = route_config;
}
framework.resolved = true;
this.options.framework = framework.*;
} else if (!framework.resolved) {
Global.panic("directly passing framework path is not implemented yet!", .{});
}
}
}
pub fn configureFrameworkWithResolveResult(this: *ThisBundler, comptime client: bool) !?_resolver.Result {
if (this.options.framework != null) {
try this.configureFramework(true);
if (comptime client) {
if (this.options.framework.?.client.isEnabled()) {
return try this.resolver.resolve(this.fs.top_level_dir, this.options.framework.?.client.path, .stmt);
}
if (this.options.framework.?.fallback.isEnabled()) {
return try this.resolver.resolve(this.fs.top_level_dir, this.options.framework.?.fallback.path, .stmt);
}
} else {
if (this.options.framework.?.server.isEnabled()) {
return try this.resolver.resolve(this.fs.top_level_dir, this.options.framework.?.server, .stmt);
}
}
}
return null;
}
pub fn configureRouter(this: *ThisBundler, comptime load_defines: bool) !void {
try this.configureFramework(load_defines);
defer {
if (load_defines) {
this.configureDefines() catch {};
}
}
// if you pass just a directory, activate the router configured for the pages directory
// for now:
// - "." is not supported
// - multiple pages directories is not supported
if (!this.options.routes.routes_enabled and this.options.entry_points.len == 1 and !this.options.serve) {
// When inferring:
// - pages directory with a file extension is not supported. e.g. "pages.app/" won't work.
// This is a premature optimization to avoid this magical auto-detection we do here from meaningfully increasing startup time if you're just passing a file
// readDirInfo is a recursive lookup, top-down instead of bottom-up. It opens each folder handle and potentially reads the package.jsons
// So it is not fast! Unless it's already cached.
var paths = [_]string{std.mem.trimLeft(u8, this.options.entry_points[0], "./")};
if (std.mem.indexOfScalar(u8, paths[0], '.') == null) {
var pages_dir_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
var entry = this.fs.absBuf(&paths, &pages_dir_buf);
if (std.fs.path.extension(entry).len == 0) {
allocators.constStrToU8(entry).ptr[entry.len] = '/';
// Only throw if they actually passed in a route config and the directory failed to load
var dir_info_ = this.resolver.readDirInfo(entry) catch return;
var dir_info = dir_info_ orelse return;
this.options.routes.dir = dir_info.abs_path;
this.options.routes.extensions = std.mem.span(&options.RouteConfig.DefaultExtensions);
this.options.routes.routes_enabled = true;
this.router = try Router.init(this.fs, this.allocator, this.options.routes);
try this.router.?.loadRoutes(
this.log,
dir_info,
Resolver,
&this.resolver,
);
this.router.?.routes.client_framework_enabled = this.options.isFrontendFrameworkEnabled();
return;
}
}
} else if (this.options.routes.routes_enabled) {
var dir_info_ = try this.resolver.readDirInfo(this.options.routes.dir);
var dir_info = dir_info_ orelse return error.MissingRoutesDir;
this.options.routes.dir = dir_info.abs_path;
this.router = try Router.init(this.fs, this.allocator, this.options.routes);
try this.router.?.loadRoutes(this.log, dir_info, Resolver, &this.resolver);
this.router.?.routes.client_framework_enabled = this.options.isFrontendFrameworkEnabled();
return;
}
// If we get this far, it means they're trying to run the bundler without a preconfigured router
if (this.options.entry_points.len > 0) {
this.options.routes.routes_enabled = false;
}
if (this.router) |*router| {
router.routes.client_framework_enabled = this.options.isFrontendFrameworkEnabled();
}
}
pub fn resetStore(bundler: *ThisBundler) void {
js_ast.Expr.Data.Store.reset();
js_ast.Stmt.Data.Store.reset();
}
pub noinline fn dumpEnvironmentVariables(bundler: *ThisBundler) void {
@setCold(true);
const opts = std.json.StringifyOptions{
.whitespace = std.json.StringifyOptions.Whitespace{
.separator = true,
},
};
Output.flush();
var writer = Output.writer();
std.json.stringify(bundler.env.map.*, opts, Output.writer()) catch unreachable;
Output.flush();
}
pub const GenerateNodeModuleBundle = struct {
const BunQueue = NewBunQueue(_resolver.Result);
pub const ThreadPool = struct {
// Hardcode 512 as max number of threads for now.
workers: [512]Worker = undefined,
workers_used: u32 = 0,
cpu_count: u32 = 0,
started_workers: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0),
stopped_workers: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0),
completed_count: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0),
pub fn start(this: *ThreadPool, generator: *GenerateNodeModuleBundle) !void {
generator.bundler.env.loadProcess();
this.cpu_count = @truncate(u32, @divFloor((try std.Thread.getCpuCount()) + 1, 2));
if (generator.bundler.env.map.get("GOMAXPROCS")) |max_procs| {
if (std.fmt.parseInt(u32, max_procs, 10)) |cpu_count| {
this.cpu_count = std.math.min(this.cpu_count, cpu_count);
} else |err| {}
}
if (this.cpu_count <= 1) return;
while (this.workers_used < this.cpu_count) : (this.workers_used += 1) {
try this.workers[this.workers_used].init(generator);
}
}
pub fn wait(this: *ThreadPool, generator: *GenerateNodeModuleBundle) !void {
if (this.cpu_count <= 1) {
var worker = generator.allocator.create(Worker) catch unreachable;
worker.* = Worker{
.generator = generator,
.allocator = generator.allocator,
.data = generator.allocator.create(Worker.WorkerData) catch unreachable,
.thread_id = undefined,
.thread = undefined,
};
worker.data.shared_buffer = try MutableString.init(generator.allocator, 0);
worker.data.scan_pass_result = js_parser.ScanPassResult.init(generator.allocator);
worker.data.log = generator.log;
worker.data.estimated_input_lines_of_code = 0;
worker.data.macro_context = js_ast.Macro.MacroContext.init(generator.bundler);
defer {
worker.data.deinit(generator.allocator);
}
while (generator.queue.next()) |item| {
try generator.processFile(worker, generator.bundler, item);
}
generator.estimated_input_lines_of_code = worker.data.estimated_input_lines_of_code;
return;
}
while (generator.queue.count.load(.SeqCst) != generator.pool.completed_count.load(.SeqCst)) {
var j: usize = 0;
while (j < 100) : (j += 1) {}
std.atomic.spinLoopHint();
}
for (this.workers[0..this.workers_used]) |*worker| {
@atomicStore(bool, &worker.quit, true, .Release);
}
while (this.stopped_workers.load(.Acquire) != this.workers_used) {
var j: usize = 0;
while (j < 100) : (j += 1) {}
std.atomic.spinLoopHint();
}
for (this.workers[0..this.workers_used]) |*worker| {
worker.thread.join();
}
}
pub const Task = struct {
result: _resolver.Result,
generator: *GenerateNodeModuleBundle,
};
pub const Worker = struct {
thread_id: std.Thread.Id,
thread: std.Thread,
allocator: *std.mem.Allocator,
generator: *GenerateNodeModuleBundle,
data: *WorkerData = undefined,
quit: bool = false,
has_notify_started: bool = false,
pub const WorkerData = struct {
shared_buffer: MutableString = undefined,
scan_pass_result: js_parser.ScanPassResult = undefined,
log: *logger.Log,
estimated_input_lines_of_code: usize = 0,
macro_context: js_ast.Macro.MacroContext,
pub fn deinit(this: *WorkerData, allocator: *std.mem.Allocator) void {
this.shared_buffer.deinit();
this.scan_pass_result.named_imports.deinit();
this.scan_pass_result.import_records.deinit();
allocator.destroy(this);
}
};
pub fn init(worker: *Worker, generator: *GenerateNodeModuleBundle) !void {
worker.generator = generator;
worker.allocator = generator.allocator;
worker.thread = try std.Thread.spawn(.{}, Worker.run, .{worker});
}
pub fn notifyStarted(this: *Worker) void {
if (!this.has_notify_started) {
this.has_notify_started = true;
_ = this.generator.pool.started_workers.fetchAdd(1, .Release);
std.Thread.Futex.wake(&this.generator.pool.started_workers, std.math.maxInt(u32));
}
}
pub fn run(this: *Worker) void {
Output.Source.configureThread();
this.thread_id = std.Thread.getCurrentId();
if (isDebug) {
Output.prettyln("Thread started.\n", .{});
}
defer {
if (isDebug) {
Output.prettyln("Thread stopped.\n", .{});
}
Output.flush();
}
this.loop() catch |err| {
Output.prettyErrorln("<r><red>Error: {s}<r>", .{@errorName(err)});
};
}
pub fn loop(this: *Worker) anyerror!void {
defer {
_ = this.generator.pool.stopped_workers.fetchAdd(1, .Release);
this.notifyStarted();
std.Thread.Futex.wake(&this.generator.pool.stopped_workers, 1);
// std.Thread.Futex.wake(&this.generator.queue.len, std.math.maxInt(u32));
}
js_ast.Expr.Data.Store.create(this.generator.allocator);
js_ast.Stmt.Data.Store.create(this.generator.allocator);
this.data = this.generator.allocator.create(WorkerData) catch unreachable;
this.data.* = WorkerData{
.log = this.generator.allocator.create(logger.Log) catch unreachable,
.estimated_input_lines_of_code = 0,
.macro_context = js_ast.Macro.MacroContext.init(this.generator.bundler),
};
this.data.log.* = logger.Log.init(this.generator.allocator);
this.data.shared_buffer = try MutableString.init(this.generator.allocator, 0);
this.data.scan_pass_result = js_parser.ScanPassResult.init(this.generator.allocator);
var bundler = this.generator.bundler.*;
var bundler_ptr = &bundler;
const CacheSet = @import("./cache.zig");
// no funny business mr. cache
bundler_ptr.resolver.caches = CacheSet.Set.init(this.allocator);
bundler_ptr.linker.resolver = &bundler_ptr.resolver;
bundler_ptr.log = this.data.log;
bundler_ptr.linker.log = this.data.log;
bundler_ptr.linker.resolver.log = this.data.log;
defer {
{
this.generator.log_lock.lock();
this.data.log.appendTo(this.generator.log) catch {};
this.generator.estimated_input_lines_of_code += this.data.estimated_input_lines_of_code;
this.generator.log_lock.unlock();
}
this.data.deinit(this.generator.allocator);
}
this.notifyStarted();
while (!@atomicLoad(bool, &this.quit, .Acquire)) {
while (this.generator.queue.next()) |item| {
defer {
_ = this.generator.pool.completed_count.fetchAdd(1, .Release);
}
try this.generator.processFile(this, bundler_ptr, item);
}
}
}
};
};
write_lock: Lock,
log_lock: Lock = Lock.init(),
module_list: std.ArrayList(Api.JavascriptBundledModule),
package_list: std.ArrayList(Api.JavascriptBundledPackage),
header_string_buffer: MutableString,
// Just need to know if we've already enqueued this one
package_list_map: std.AutoHashMap(u64, u32),
queue: *BunQueue,
bundler: *ThisBundler,
allocator: *std.mem.Allocator,
tmpfile: std.fs.File,
log: *logger.Log,
pool: *ThreadPool,
tmpfile_byte_offset: u32 = 0,
code_end_byte_offset: u32 = 0,
has_jsx: bool = false,
estimated_input_lines_of_code: usize = 0,
work_waiter: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0),
list_lock: Lock = Lock.init(),
dynamic_import_file_size_store: U32Map,
dynamic_import_file_size_store_lock: Lock,
always_bundled_package_hashes: []u32 = &[_]u32{},
always_bundled_package_jsons: []*const PackageJSON = &.{},
const U32Map = std.AutoHashMap(u32, u32);
pub const current_version: u32 = 1;
const dist_index_js_string_pointer = Api.StringPointer{ .length = "dist/index.js".len };
const index_js_string_pointer = Api.StringPointer{ .length = "index.js".len, .offset = "dist/".len };
pub fn enqueueItem(this: *GenerateNodeModuleBundle, resolve: _resolver.Result) !void {
var result = resolve;
var path = result.path() orelse return;
const loader = this.bundler.options.loaders.get(path.name.ext) orelse .file;
if (!loader.isJavaScriptLikeOrJSON()) return;
path.* = try path.dupeAlloc(this.allocator);
if (BundledModuleData.get(this, &result)) |mod| {
try this.queue.upsert(mod.module_id, result);
} else {
try this.queue.upsert(result.hash(this.bundler.fs.top_level_dir, loader), result);
}
}
// The Bun Bundle Format
// All the node_modules your app uses in a single compact file with metadata
// A binary JavaScript bundle format prioritizing generation time and deserialization time
pub const magic_bytes = "#!/usr/bin/env bun\n\n";
// This makes it possible to do ./path-to-bundle on posix systems so you can see the raw JS contents
// https://en.wikipedia.org/wiki/Magic_number_(programming)#In_files
// Immediately after the magic bytes, the next character is a uint32 followed by a newline
// 0x00000000\n
// That uint32 denotes the byte offset in the file where the code for the bundle ends
// - If the value is 0, that means the file did not finish writing or there are no modules
// - This imposes a maximum bundle size of around 4,294,967,295 bytes. If your JS is more than 4 GB, it won't work.
// The raw JavaScript is encoded as a UTF-8 string starting from the current position + 1 until the above byte offset.
// This uint32 is useful for HTTP servers to separate:
// - Which part of the bundle is the JS code?
// - Which part is the metadata?
// Without needing to do a full pass through the file, or necessarily care about the metadata.
// The metadata is at the bottom of the file instead of the top because the metadata is written after all JS code in the bundle is written.
// The rationale there is:
// 1. We cannot prepend to a file without rewriting the entire file
// 2. The metadata is variable-length and that format will change often.
// 3. We won't have all the metadata until after all JS is finished writing
// If you have 32 MB of JavaScript dependencies, you really want to avoid reading the code in memory.
// - This lets you seek to the specific position in the file.
// - HTTP servers should use sendfile() instead of copying the file to userspace memory.
// So instead, we append metadata to the file after printing each node_module
// When there are no more modules to process, we generate the metadata
// To find the metadata, you look at the byte offset: initial_header[magic_bytes.len..initial_header.len - 1]
// Then, you add that number to initial_header.len
const initial_header = brk: {
var buf = std.mem.zeroes([magic_bytes.len + 5]u8);
std.mem.copy(u8, &buf, magic_bytes);
var remainder = buf[magic_bytes.len..];
// Write an invalid byte offset to be updated after we finish generating the code
std.mem.writeIntNative(u32, remainder[0 .. remainder.len - 1], 0);
buf[buf.len - 1] = '\n';
break :brk buf;
};
const code_start_byte_offset: u32 = initial_header.len;
// The specifics of the metadata is not documented here. You can find it in src/api/schema.peechy.
pub fn appendHeaderString(generator: *GenerateNodeModuleBundle, str: string) !Api.StringPointer {
// This is so common we might as well just reuse it
// Plus this is one machine word so it's a quick comparison
if (strings.eqlComptime(str, "index.js")) {
return index_js_string_pointer;
} else if (strings.eqlComptime(str, "dist/index.js")) {
return dist_index_js_string_pointer;
}
var offset = generator.header_string_buffer.list.items.len;
try generator.header_string_buffer.append(str);
return Api.StringPointer{
.offset = @truncate(u32, offset),
.length = @truncate(u32, str.len),
};
}
pub fn generate(
bundler: *ThisBundler,
allocator: *std.mem.Allocator,
framework_config: ?Api.LoadedFramework,
route_config: ?Api.LoadedRouteConfig,
destination: [*:0]const u8,
estimated_input_lines_of_code: *usize,
) !?Api.JavascriptBundleContainer {
var tmpdir: std.fs.Dir = try bundler.fs.fs.openTmpDir();
var tmpname_buf: [64]u8 = undefined;
bundler.resetStore();
try bundler.configureDefines();
const tmpname = try bundler.fs.tmpname(
".bun",
std.mem.span(&tmpname_buf),
std.hash.Wyhash.hash(@intCast(usize, std.time.milliTimestamp()) % std.math.maxInt(u32), std.mem.span(destination)),
);
var tmpfile = Fs.FileSystem.RealFS.Tmpfile{};
try tmpfile.create(&bundler.fs.fs, tmpname);
errdefer tmpfile.closeAndDelete(tmpname);
var generator = try allocator.create(GenerateNodeModuleBundle);
var queue = try BunQueue.init(allocator);
defer allocator.destroy(generator);
generator.* = GenerateNodeModuleBundle{
.module_list = std.ArrayList(Api.JavascriptBundledModule).init(allocator),
.package_list = std.ArrayList(Api.JavascriptBundledPackage).init(allocator),
.header_string_buffer = try MutableString.init(allocator, "dist/index.js".len),
.allocator = allocator,
.queue = queue,
.estimated_input_lines_of_code = 0,
// .resolve_queue = queue,
.bundler = bundler,
.tmpfile = tmpfile.file(),
.dynamic_import_file_size_store = U32Map.init(allocator),
.dynamic_import_file_size_store_lock = Lock.init(),
.log = bundler.log,
.package_list_map = std.AutoHashMap(u64, u32).init(allocator),
.pool = undefined,
.write_lock = Lock.init(),
};
// dist/index.js appears more common than /index.js
// but this means we can store both "dist/index.js" and "index.js" in one.
try generator.header_string_buffer.append("dist/index.js");
try generator.package_list_map.ensureTotalCapacity(128);
var pool = try allocator.create(ThreadPool);
pool.* = ThreadPool{};
generator.pool = pool;
var this = generator;
// Always inline the runtime into the bundle
try generator.appendBytes(&initial_header);
// If we try to be smart and rely on .written, it turns out incorrect
const code_start_pos = try this.tmpfile.getPos();
if (isDebug) {
try generator.appendBytes(runtime.Runtime.sourceContent());
try generator.appendBytes("\n\n");
} else {
try generator.appendBytes(comptime runtime.Runtime.sourceContent() ++ "\n\n");
}
if (bundler.log.level == .verbose) {
bundler.resolver.debug_logs = try DebugLogs.init(allocator);
}
Analytics.Features.bun_bun = true;
always_bundled: {
const root_package_json_resolved: _resolver.Result = bundler.resolver.resolve(bundler.fs.top_level_dir, "./package.json", .stmt) catch |err| {
generator.log.addWarning(null, logger.Loc.Empty, "Please run `bun bun` from a directory containing a package.json.") catch unreachable;
break :always_bundled;
};
const root_package_json = root_package_json_resolved.package_json orelse brk: {
const read_dir = (bundler.resolver.readDirInfo(bundler.fs.top_level_dir) catch unreachable).?;
Analytics.Features.tsconfig = Analytics.Features.tsconfig or read_dir.tsconfig_json != null;
break :brk read_dir.package_json.?;
};
Analytics.setProjectID(std.fs.path.dirname(root_package_json.source.path.text) orelse "/", root_package_json.name);
Analytics.Features.macros = Analytics.Features.macros or root_package_json.macros.count() > 0;
if (root_package_json.always_bundle.len > 0) {
Analytics.Features.always_bundle = true;
var always_bundled_package_jsons = bundler.allocator.alloc(*PackageJSON, root_package_json.always_bundle.len) catch unreachable;
var always_bundled_package_hashes = bundler.allocator.alloc(u32, root_package_json.always_bundle.len) catch unreachable;
var i: u16 = 0;
inner: for (root_package_json.always_bundle) |name| {
std.mem.copy(u8, &tmp_buildfile_buf, name);
std.mem.copy(u8, tmp_buildfile_buf[name.len..], "/package.json");
const package_json_import = tmp_buildfile_buf[0 .. name.len + "/package.json".len];
const result = bundler.resolver.resolve(bundler.fs.top_level_dir, package_json_import, .stmt) catch |err| {
generator.log.addErrorFmt(null, logger.Loc.Empty, bundler.allocator, "{s} resolving always bundled module \"{s}\"", .{ @errorName(err), name }) catch unreachable;
continue :inner;
};
var package_json: *PackageJSON = result.package_json orelse brk: {
const read_dir = (bundler.resolver.readDirInfo(package_json_import) catch unreachable).?;
if (read_dir.package_json == null) {
generator.log.addWarningFmt(null, logger.Loc.Empty, bundler.allocator, "{s} missing package.json. It will not be bundled", .{name}) catch unreachable;
continue :inner;
}
break :brk read_dir.package_json.?;
};
package_json.source.key_path = result.path_pair.primary;
// if (!strings.contains(result.path_pair.primary.text, package_json.name)) {
// generator.log.addErrorFmt(
// null,
// logger.Loc.Empty,
// bundler.allocator,
// "Bundling \"{s}\" is not supported because the package isn.\n To fix this, move the package's code to a directory containing the name.\n Location: \"{s}\"",
// .{
// name,
// name,
// result.path_pair.primary.text,
// },
// ) catch unreachable;
// continue :inner;
// }
always_bundled_package_jsons[i] = package_json;
always_bundled_package_hashes[i] = package_json.hash;
i += 1;
}
generator.always_bundled_package_hashes = always_bundled_package_hashes[0..i];
generator.always_bundled_package_jsons = always_bundled_package_jsons[0..i];
}
}
if (generator.log.errors > 0) return error.BundleFailed;
this.bundler.macro_context = js_ast.Macro.MacroContext.init(bundler);
const include_refresh_runtime =
!this.bundler.options.production and
this.bundler.options.jsx.supports_fast_refresh and
bundler.options.platform.isWebLike();
Analytics.Features.fast_refresh = this.bundler.options.jsx.supports_fast_refresh;
const resolve_queue_estimate = bundler.options.entry_points.len +
@intCast(usize, @boolToInt(framework_config != null)) +
@intCast(usize, @boolToInt(include_refresh_runtime)) +
@intCast(usize, @boolToInt(bundler.options.jsx.parse));
if (bundler.router) |router| {
defer this.bundler.resetStore();
Analytics.Features.filesystem_router = true;
const entry_points = try router.getEntryPoints();
for (entry_points) |entry_point| {
const resolved = bundler.resolveEntryPoint(entry_point) catch continue;
try this.enqueueItem(resolved);
}
this.bundler.resetStore();
} else {}
for (bundler.options.entry_points) |entry_point| {
if (bundler.options.platform.isBun()) continue;
defer this.bundler.resetStore();
const entry_point_path = bundler.normalizeEntryPointPath(entry_point);
const resolved = bundler.resolveEntryPoint(entry_point_path) catch continue;
try this.enqueueItem(resolved);
}
if (framework_config) |conf| {
defer this.bundler.resetStore();
try this.bundler.configureFramework(true);
if (bundler.options.framework) |framework| {
Analytics.Features.framework = true;
if (framework.override_modules.keys.len > 0) {
bundler.options.framework.?.override_modules_hashes = allocator.alloc(u64, framework.override_modules.keys.len) catch unreachable;
for (framework.override_modules.keys) |key, i| {
bundler.options.framework.?.override_modules_hashes[i] = std.hash.Wyhash.hash(0, key);
}
}
if (bundler.options.platform.isBun()) {
if (framework.server.isEnabled()) {
Analytics.Features.bunjs = true;
const resolved = try bundler.resolver.resolve(
bundler.fs.top_level_dir,
framework.server.path,
.entry_point,
);
try this.enqueueItem(resolved);
}
} else {
if (framework.client.isEnabled()) {
const resolved = try bundler.resolver.resolve(
bundler.fs.top_level_dir,
framework.client.path,
.entry_point,
);
try this.enqueueItem(resolved);
}
if (framework.fallback.isEnabled()) {
const resolved = try bundler.resolver.resolve(
bundler.fs.top_level_dir,
framework.fallback.path,
.entry_point,
);
try this.enqueueItem(resolved);
}
}
}
} else {}
// Normally, this is automatic
// However, since we only do the parsing pass, it may not get imported automatically.
if (bundler.options.jsx.parse) {
defer this.bundler.resetStore();
if (this.bundler.resolver.resolve(
this.bundler.fs.top_level_dir,
this.bundler.options.jsx.import_source,
.require,
)) |new_jsx_runtime| {
try this.enqueueItem(new_jsx_runtime);
} else |err| {}
}
var refresh_runtime_module_id: u32 = 0;
if (include_refresh_runtime) {
defer this.bundler.resetStore();
if (this.bundler.resolver.resolve(
this.bundler.fs.top_level_dir,
this.bundler.options.jsx.refresh_runtime,
.require,
)) |refresh_runtime| {
try this.enqueueItem(refresh_runtime);
if (BundledModuleData.get(this, &refresh_runtime)) |mod| {
refresh_runtime_module_id = mod.module_id;
}
} else |err| {}
}
this.bundler.resetStore();
if (bundler.options.platform != .bun) Analytics.enqueue(Analytics.EventName.bundle_start);
this.pool.start(this) catch |err| {
Analytics.enqueue(Analytics.EventName.bundle_fail);
return err;
};
this.pool.wait(this) catch |err| {
Analytics.enqueue(Analytics.EventName.bundle_fail);
return err;
};
if (bundler.options.platform != .bun) Analytics.enqueue(Analytics.EventName.bundle_success);
estimated_input_lines_of_code.* = generator.estimated_input_lines_of_code;
// if (comptime !isRelease) {
// this.queue.checkDuplicatesSlow();
// }
if (this.log.errors > 0) {
tmpfile.closeAndDelete(std.mem.span(tmpname));
// We stop here because if there are errors we don't know if the bundle is valid
// This manifests as a crash when sorting through the module list because we may have added files to the bundle which were never actually finished being added.
return null;
}
// Delay by one tick so that the rest of the file loads first
if (include_refresh_runtime and refresh_runtime_module_id > 0) {
var refresh_runtime_injector_buf: [1024]u8 = undefined;
var fixed_buffer = std.io.fixedBufferStream(&refresh_runtime_injector_buf);
var fixed_buffer_writer = fixed_buffer.writer();
fixed_buffer_writer.print(
\\if ('window' in globalThis) {{
\\ (async function() {{
\\ BUN_RUNTIME.__injectFastRefresh(${x}());
\\ }})();
\\}}
,
.{refresh_runtime_module_id},
) catch unreachable;
try this.tmpfile.writeAll(fixed_buffer.buffer[0..fixed_buffer.pos]);
}
// Ensure we never overflow
this.code_end_byte_offset = @truncate(
u32,
// Doing this math ourself seems to not necessarily produce correct results
(try this.tmpfile.getPos()),
);
var javascript_bundle_container = std.mem.zeroes(Api.JavascriptBundleContainer);
std.sort.sort(
Api.JavascriptBundledModule,
this.module_list.items,
this,
GenerateNodeModuleBundle.sortJavascriptModuleByPath,
);
if (comptime isDebug) {
const SeenHash = std.AutoHashMap(u64, void);
var map = SeenHash.init(this.allocator);
var ids = SeenHash.init(this.allocator);
try map.ensureTotalCapacity(@truncate(u32, this.module_list.items.len));
try ids.ensureTotalCapacity(@truncate(u32, this.module_list.items.len));
for (this.module_list.items) |a| {
const a_pkg: Api.JavascriptBundledPackage = this.package_list.items[a.package_id];
const a_name = this.metadataStringPointer(a_pkg.name);
const a_version = this.metadataStringPointer(a_pkg.version);
const a_path = this.metadataStringPointer(a.path);
std.debug.assert(a_name.len > 0);
std.debug.assert(a_version.len > 0);
std.debug.assert(a_path.len > 0);
var hash_print = std.mem.zeroes([4096]u8);
const hash = std.hash.Wyhash.hash(0, std.fmt.bufPrint(&hash_print, "{s}@{s}/{s}", .{ a_name, a_version, a_path }) catch unreachable);
var result1 = map.getOrPutAssumeCapacity(hash);
std.debug.assert(!result1.found_existing);
var result2 = ids.getOrPutAssumeCapacity(a.id);
std.debug.assert(!result2.found_existing);
}
}
var hasher = std.hash.Wyhash.init(0);
// We want to sort the packages as well as the files
// The modules sort the packages already
// So can just copy it in the below loop.
var sorted_package_list = try allocator.alloc(Api.JavascriptBundledPackage, this.package_list.items.len);
// At this point, the module_list is sorted.
if (this.module_list.items.len > 0) {
var package_id_i: u32 = 0;
var i: usize = 0;
// Assumption: node_modules are immutable
// Assumption: module files are immutable
// (They're not. But, for our purposes that's okay)
// The etag is:
// - The hash of each module's path in sorted order
// - The hash of each module's code size in sorted order
// - hash(hash(package_name, package_version))
// If this doesn't prove strong enough, we will do a proper content hash
// But I want to avoid that overhead unless proven necessary.
// There's a good chance we don't even strictly need an etag here.
var bytes: [4]u8 = undefined;
while (i < this.module_list.items.len) {
var current_package_id = this.module_list.items[i].package_id;
this.module_list.items[i].package_id = package_id_i;
var offset = @truncate(u32, i);
i += 1;
while (i < this.module_list.items.len and this.module_list.items[i].package_id == current_package_id) : (i += 1) {
this.module_list.items[i].package_id = package_id_i;
// Hash the file path
hasher.update(this.metadataStringPointer(this.module_list.items[i].path));
// Then the length of the code
std.mem.writeIntNative(u32, &bytes, this.module_list.items[i].code.length);
hasher.update(&bytes);
}
this.package_list.items[current_package_id].modules_offset = offset;
this.package_list.items[current_package_id].modules_length = @truncate(u32, i) - offset;
// Hash the hash of the package name
// it's hash(hash(package_name, package_version))
std.mem.writeIntNative(u32, &bytes, this.package_list.items[current_package_id].hash);
hasher.update(&bytes);
sorted_package_list[package_id_i] = this.package_list.items[current_package_id];
package_id_i += 1;
}
}
var javascript_bundle = std.mem.zeroes(Api.JavascriptBundle);
javascript_bundle.modules = this.module_list.items;
javascript_bundle.packages = sorted_package_list;
javascript_bundle.manifest_string = this.header_string_buffer.list.items;
const etag_u64 = hasher.final();
// We store the etag as a ascii hex encoded u64
// This is so we can send the bytes directly in the HTTP server instead of formatting it as hex each time.
javascript_bundle.etag = try std.fmt.allocPrint(allocator, "{x}", .{etag_u64});
javascript_bundle.generated_at = @truncate(u32, @intCast(u64, std.time.milliTimestamp()));
const basename = std.fs.path.basename(std.mem.span(destination));
const extname = std.fs.path.extension(basename);
javascript_bundle.import_from_name = if (bundler.options.platform.isBun())
"/node_modules.server.bun"
else
try std.fmt.allocPrint(
this.allocator,
"/{s}.{x}.bun",
.{
basename[0 .. basename.len - extname.len],
etag_u64,
},
);
javascript_bundle_container.bundle_format_version = current_version;
javascript_bundle_container.bundle = javascript_bundle;
javascript_bundle_container.code_length = this.code_end_byte_offset;
javascript_bundle_container.framework = framework_config;
javascript_bundle_container.routes = route_config;
var start_pos = try this.tmpfile.getPos();
var tmpwriter = std.io.bufferedWriter(this.tmpfile.writer());
const SchemaWriter = schema.Writer(@TypeOf(tmpwriter.writer()));
var schema_file_writer = SchemaWriter.init(tmpwriter.writer());
try javascript_bundle_container.encode(&schema_file_writer);
try tmpwriter.flush();
// sanity check
if (isDebug) {
try this.tmpfile.seekTo(start_pos);
var contents = try allocator.alloc(u8, (try this.tmpfile.getEndPos()) - start_pos);
var read_bytes = try this.tmpfile.read(contents);
var buf = contents[0..read_bytes];
var reader = schema.Reader.init(buf, allocator);
var decoder = try Api.JavascriptBundleContainer.decode(
&reader,
);
std.debug.assert(decoder.code_length.? == javascript_bundle_container.code_length.?);
}
var code_length_bytes: [4]u8 = undefined;
std.mem.writeIntNative(u32, &code_length_bytes, this.code_end_byte_offset);
_ = try std.os.pwrite(this.tmpfile.handle, &code_length_bytes, magic_bytes.len);
// Without his mutex, we get a crash at this location:
// try std.os.renameat(tmpdir.fd, tmpname, top_dir.fd, destination);
// ^
const top_dir = try std.fs.openDirAbsolute(Fs.FileSystem.instance.top_level_dir, .{});
_ = C.fchmod(
this.tmpfile.handle,
// chmod 777
0000010 | 0000100 | 0000001 | 0001000 | 0000040 | 0000004 | 0000002 | 0000400 | 0000200 | 0000020,
);
try tmpfile.promote(tmpname, top_dir.fd, destination);
// Print any errors at the end
// try this.log.print(Output.errorWriter());
return javascript_bundle_container;
}
pub fn metadataStringPointer(this: *GenerateNodeModuleBundle, ptr: Api.StringPointer) string {
return this.header_string_buffer.list.items[ptr.offset .. ptr.offset + ptr.length];
}
// Since we trim the prefixes, we must also compare the package name and version
pub fn sortJavascriptModuleByPath(ctx: *GenerateNodeModuleBundle, a: Api.JavascriptBundledModule, b: Api.JavascriptBundledModule) bool {
return switch (std.mem.order(
u8,
ctx.metadataStringPointer(
ctx.package_list.items[a.package_id].name,
),
ctx.metadataStringPointer(
ctx.package_list.items[b.package_id].name,
),
)) {
.eq => switch (std.mem.order(
u8,
ctx.metadataStringPointer(
ctx.package_list.items[a.package_id].version,
),
ctx.metadataStringPointer(
ctx.package_list.items[b.package_id].version,
),
)) {
.eq => std.mem.order(
u8,
ctx.metadataStringPointer(a.path),
ctx.metadataStringPointer(b.path),
) == .lt,
.lt => true,
else => false,
},
.lt => true,
else => false,
};
}
// pub fn sortJavascriptPackageByName(ctx: *GenerateNodeModuleBundle, a: Api.JavascriptBundledPackage, b: Api.JavascriptBundledPackage) bool {
// return std.mem.order(u8, ctx.metadataStringPointer(a.name), ctx.metadataStringPointer(b.name)) == .lt;
// }
pub fn appendBytes(generator: *GenerateNodeModuleBundle, bytes: anytype) !void {
try generator.tmpfile.writeAll(bytes);
generator.tmpfile_byte_offset += @truncate(u32, bytes.len);
}
const BundledModuleData = struct {
import_path: string,
package_path: string,
package: *const PackageJSON,
module_id: u32,
pub fn getForceBundle(this: *GenerateNodeModuleBundle, resolve_result: *const _resolver.Result) ?BundledModuleData {
return _get(this, resolve_result, true, false);
}
pub fn getForceBundleForMain(this: *GenerateNodeModuleBundle, resolve_result: *const _resolver.Result) ?BundledModuleData {
return _get(this, resolve_result, true, true);
}
threadlocal var normalized_package_path: [512]u8 = undefined;
threadlocal var normalized_package_path2: [512]u8 = undefined;
inline fn _get(this: *GenerateNodeModuleBundle, resolve_result: *const _resolver.Result, comptime force: bool, comptime is_main: bool) ?BundledModuleData {
const path = resolve_result.pathConst() orelse return null;
if (strings.eqlComptime(path.namespace, "node")) {
const _import_path = path.text["/bun-vfs/node_modules/".len..][resolve_result.package_json.?.name.len + 1 ..];
return BundledModuleData{
.import_path = _import_path,
.package_path = path.text["/bun-vfs/node_modules/".len..],
.package = resolve_result.package_json.?,
.module_id = resolve_result.package_json.?.hashModule(_import_path),
};
}
var import_path = path.text;
var package_path = path.text;
var file_path = path.text;
if (resolve_result.package_json) |pkg_| {
var pkg: *const PackageJSON = pkg_;
if (std.mem.indexOfScalar(u32, this.always_bundled_package_hashes, pkg.hash)) |pkg_i| {
pkg = this.always_bundled_package_jsons[pkg_i];
const key_path_source_dir = pkg.source.key_path.sourceDir();
const default_source_dir = pkg.source.path.sourceDir();
if (strings.startsWith(path.text, key_path_source_dir)) {
import_path = path.text[key_path_source_dir.len..];
} else if (strings.startsWith(path.text, default_source_dir)) {
import_path = path.text[default_source_dir.len..];
} else if (strings.startsWith(path.pretty, pkg.name)) {
import_path = path.pretty[pkg.name.len + 1 ..];
}
var buf_to_use: []u8 = if (is_main) &normalized_package_path2 else &normalized_package_path;
std.mem.copy(u8, buf_to_use, pkg.name);
buf_to_use[pkg.name.len] = '/';
std.mem.copy(u8, buf_to_use[pkg.name.len + 1 ..], import_path);
package_path = buf_to_use[0 .. pkg.name.len + import_path.len + 1];
return BundledModuleData{
.import_path = import_path,
.package_path = package_path,
.package = pkg,
.module_id = pkg.hashModule(package_path),
};
}
}
const root: _resolver.RootPathPair = this.bundler.resolver.rootNodeModulePackageJSON(
resolve_result,
) orelse return null;
var base_path = root.base_path;
const package_json = root.package_json;
// Easymode: the file path doesn't need to be remapped.
if (strings.startsWith(file_path, base_path)) {
import_path = std.mem.trimLeft(u8, path.text[base_path.len..], "/");
package_path = std.mem.trim(u8, path.text[base_path.len - package_json.name.len - 1 ..], "/");
std.debug.assert(import_path.len > 0);
return BundledModuleData{
.import_path = import_path,
.package_path = package_path,
.package = package_json,
.module_id = package_json.hashModule(package_path),
};
}
if (std.mem.lastIndexOf(u8, file_path, package_json.name)) |i| {
package_path = file_path[i..];
import_path = package_path[package_json.name.len + 1 ..];
std.debug.assert(import_path.len > 0);
return BundledModuleData{
.import_path = import_path,
.package_path = package_path,
.package = package_json,
.module_id = package_json.hashModule(package_path),
};
}
if (comptime force) {
if (std.mem.indexOfScalar(u32, this.always_bundled_package_hashes, root.package_json.hash)) |pkg_json_i| {
const pkg_json = this.always_bundled_package_jsons[pkg_json_i];
base_path = pkg_json.source.key_path.sourceDir();
if (strings.startsWith(file_path, base_path)) {
import_path = std.mem.trimLeft(u8, path.text[base_path.len..], "/");
package_path = std.mem.trim(u8, path.text[base_path.len - package_json.name.len - 1 ..], "/");
std.debug.assert(import_path.len > 0);
return BundledModuleData{
.import_path = import_path,
.package_path = package_path,
.package = package_json,
.module_id = package_json.hashModule(package_path),
};
}
if (std.mem.lastIndexOf(u8, file_path, package_json.name)) |i| {
package_path = file_path[i..];
import_path = package_path[package_json.name.len + 1 ..];
std.debug.assert(import_path.len > 0);
return BundledModuleData{
.import_path = import_path,
.package_path = package_path,
.package = package_json,
.module_id = package_json.hashModule(package_path),
};
}
}
unreachable;
}
return null;
}
pub fn get(this: *GenerateNodeModuleBundle, resolve_result: *const _resolver.Result) ?BundledModuleData {
return _get(this, resolve_result, false, false);
}
};
fn writeEmptyModule(this: *GenerateNodeModuleBundle, package_relative_path: string, module_id: u32) !u32 {
this.write_lock.lock();
defer this.write_lock.unlock();
var code_offset = @truncate(u32, try this.tmpfile.getPos());
var writer = this.tmpfile.writer();
var buffered = std.io.bufferedWriter(writer);
var bufwriter = buffered.writer();
try bufwriter.writeAll("// ");
try bufwriter.writeAll(package_relative_path);
try bufwriter.writeAll(" (disabled/empty)\nexport var $");
std.fmt.formatInt(module_id, 16, .lower, .{}, bufwriter) catch unreachable;
try bufwriter.writeAll(" = () => { var obj = {}; Object.defineProperty(obj, 'default', { value: obj, enumerable: false, configurable: true }, obj); return obj; }; \n");
try buffered.flush();
this.tmpfile_byte_offset = @truncate(u32, try this.tmpfile.getPos());
return code_offset;
}
fn processImportRecord(this: *GenerateNodeModuleBundle, import_record: ImportRecord) !void {}
var json_ast_symbols = [_]js_ast.Symbol{
js_ast.Symbol{ .original_name = "$$m" },
js_ast.Symbol{ .original_name = "exports" },
js_ast.Symbol{ .original_name = "module" },
js_ast.Symbol{ .original_name = "CONGRATS_YOU_FOUND_A_BUG" },
js_ast.Symbol{ .original_name = "$$bun_runtime_json_parse" },
};
const json_parse_string = "parse";
var json_ast_symbols_list = std.mem.span(&json_ast_symbols);
threadlocal var override_file_path_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
pub fn appendToModuleList(
this: *GenerateNodeModuleBundle,
package: *const PackageJSON,
module_id: u32,
code_offset: u32,
package_relative_path: string,
) !void {
this.list_lock.lock();
defer this.list_lock.unlock();
const code_length = @atomicLoad(u32, &this.tmpfile_byte_offset, .SeqCst) - code_offset;
if (comptime isDebug) {
std.debug.assert(code_length > 0);
std.debug.assert(package.hash != 0);
std.debug.assert(package.version.len > 0);
std.debug.assert(package.name.len > 0);
std.debug.assert(module_id > 0);
}
var package_get_or_put_entry = try this.package_list_map.getOrPut(package.hash);
if (!package_get_or_put_entry.found_existing) {
package_get_or_put_entry.value_ptr.* = @truncate(u32, this.package_list.items.len);
try this.package_list.append(
Api.JavascriptBundledPackage{
.name = try this.appendHeaderString(package.name),
.version = try this.appendHeaderString(package.version),
.hash = package.hash,
},
);
this.has_jsx = this.has_jsx or strings.eql(package.name, this.bundler.options.jsx.package_name);
}
var path_extname_length = @truncate(u8, std.fs.path.extension(package_relative_path).len);
try this.module_list.append(
Api.JavascriptBundledModule{
.path = try this.appendHeaderString(
package_relative_path,
),
.path_extname_length = path_extname_length,
.package_id = package_get_or_put_entry.value_ptr.*,
.id = module_id,
.code = Api.StringPointer{
.length = @truncate(u32, code_length),
.offset = @truncate(u32, code_offset),
},
},
);
}
threadlocal var json_e_string: js_ast.E.String = undefined;
threadlocal var json_e_call: js_ast.E.Call = undefined;
threadlocal var json_e_identifier: js_ast.E.Identifier = undefined;
threadlocal var json_call_args: [1]js_ast.Expr = undefined;
pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, bundler: *Bundler, _resolve: _resolver.Result) !void {
const resolve = _resolve;
if (resolve.is_external) return;
var shared_buffer = &worker.data.shared_buffer;
var scan_pass_result = &worker.data.scan_pass_result;
const is_from_node_modules = resolve.isLikelyNodeModule() or brk: {
if (resolve.package_json) |pkg| {
break :brk std.mem.indexOfScalar(u32, this.always_bundled_package_hashes, pkg.hash) != null;
}
break :brk false;
};
var file_path = (resolve.pathConst() orelse unreachable).*;
const source_dir = file_path.sourceDir();
const loader = this.bundler.options.loader(file_path.name.ext);
defer scan_pass_result.reset();
defer shared_buffer.reset();
defer this.bundler.resetStore();
var log = worker.data.log;
// If we're in a node_module, build that almost normally
if (is_from_node_modules) {
var written: usize = undefined;
var code_offset: u32 = 0;
const module_data = BundledModuleData.getForceBundleForMain(this, &resolve) orelse {
const fake_path = logger.Source.initPathString(file_path.text, "");
log.addResolveError(
&fake_path,
logger.Range.None,
this.allocator,
"Bug while resolving: \"{s}\"",
.{file_path.text},
resolve.import_kind,
) catch {};
return error.ResolveError;
};
const module_id = module_data.module_id;
const package = module_data.package;
const package_relative_path = module_data.import_path;
file_path.pretty = module_data.package_path;
const entry: CacheEntry = brk: {
if (this.bundler.options.framework) |framework| {
if (framework.override_modules_hashes.len > 0) {
const package_relative_path_hash = std.hash.Wyhash.hash(0, module_data.package_path);
if (std.mem.indexOfScalar(
u64,
framework.override_modules_hashes,
package_relative_path_hash,
)) |index| {
const relative_path = [_]string{
framework.resolved_dir,
framework.override_modules.values[index],
};
var override_path = this.bundler.fs.absBuf(
&relative_path,
&override_file_path_buf,
);
override_file_path_buf[override_path.len] = 0;
var override_pathZ = override_file_path_buf[0..override_path.len :0];
break :brk try bundler.resolver.caches.fs.readFileShared(
bundler.fs,
override_pathZ,
0,
null,
shared_buffer,
);
}
}
}
if (!strings.eqlComptime(file_path.namespace, "node"))
break :brk try bundler.resolver.caches.fs.readFileShared(
bundler.fs,
file_path.textZ(),
resolve.dirname_fd,
if (resolve.file_fd != 0) resolve.file_fd else null,
shared_buffer,
);
break :brk CacheEntry{
.contents = NodeFallbackModules.contentsFromPath(file_path.text) orelse "",
};
};
var approximate_newline_count: usize = 0;
defer worker.data.estimated_input_lines_of_code += approximate_newline_count;
// Handle empty files
// We can't just ignore them. Sometimes code will try to import it. Often because of TypeScript types.
// So we just say it's an empty object. Empty object mimicks what "browser": false does as well.
// TODO: optimize this so that all the exports for these are done in one line instead of writing repeatedly
if (entry.contents.len == 0 or (entry.contents.len < 33 and strings.trim(entry.contents, " \n\r").len == 0)) {
code_offset = try this.writeEmptyModule(module_data.package_path, module_id);
} else {
var ast: js_ast.Ast = undefined;
const source = logger.Source.initRecycledFile(
Fs.File{
.path = file_path,
.contents = entry.contents,
},
bundler.allocator,
) catch return null;
switch (loader) {
.jsx,
.tsx,
.js,
.ts,
=> {
var jsx = _resolve.jsx;
jsx.parse = loader.isJSX();
var opts = js_parser.Parser.Options.init(jsx, loader);
opts.transform_require_to_import = false;
opts.enable_bundling = true;
opts.warn_about_unbundled_modules = false;
opts.macro_context = &worker.data.macro_context;
opts.macro_context.remap = package.macros;
ast = (bundler.resolver.caches.js.parse(
bundler.allocator,
opts,
bundler.options.define,
log,
&source,
) catch null) orelse return;
approximate_newline_count = ast.approximate_newline_count;
if (ast.import_records.len > 0) {
for (ast.import_records) |*import_record, record_id| {
// Don't resolve the runtime
if (import_record.is_internal or import_record.is_unused) {
continue;
}
if (bundler.resolver.resolve(source_dir, import_record.path.text, import_record.kind)) |*_resolved_import| {
if (_resolved_import.is_external) {
continue;
}
var path = _resolved_import.path() orelse {
import_record.path.is_disabled = true;
import_record.is_bundled = true;
continue;
};
const loader_ = bundler.options.loader(path.name.ext);
if (!loader_.isJavaScriptLikeOrJSON()) {
import_record.path.is_disabled = true;
import_record.is_bundled = true;
continue;
}
// if (_resolved_import.package_json == null) |pkg_json| {
// _resolved_import.package_json = if (pkg_json.hash == resolve.package_json.?.hash)
// resolve.package_json
// else
// _resolved_import.package_json;
// }
const resolved_import: *const _resolver.Result = _resolved_import;
const _module_data = BundledModuleData.getForceBundle(this, resolved_import) orelse unreachable;
import_record.module_id = _module_data.module_id;
std.debug.assert(import_record.module_id != 0);
import_record.is_bundled = true;
path.* = try path.dupeAlloc(this.allocator);
import_record.path = path.*;
try this.queue.upsert(
_module_data.module_id,
_resolved_import.*,
);
} else |err| {
if (comptime isDebug) {
if (!import_record.handles_import_errors) {
Output.prettyErrorln("\n<r><red>{s}<r> on resolving \"{s}\" from \"{s}\"", .{
@errorName(err),
import_record.path.text,
file_path.text,
});
}
}
// Disable failing packages from being printed.
// This may cause broken code to write.
// However, doing this means we tell them all the resolve errors
// Rather than just the first one.
import_record.path.is_disabled = true;
switch (err) {
error.ModuleNotFound => {
if (!import_record.handles_import_errors) {
if (isPackagePath(import_record.path.text)) {
if (this.bundler.options.platform.isWebLike() and options.ExternalModules.isNodeBuiltin(import_record.path.text)) {
try log.addResolveErrorWithTextDupe(
&source,
import_record.range,
this.allocator,
"Could not resolve Node.js builtin: \"{s}\".",
.{import_record.path.text},
import_record.kind,
);
} else {
try log.addResolveErrorWithTextDupe(
&source,
import_record.range,
this.allocator,
"Could not resolve: \"{s}\". Maybe you need to \"bun install\"?",
.{import_record.path.text},
import_record.kind,
);
}
} else {
try log.addResolveErrorWithTextDupe(
&source,
import_record.range,
this.allocator,
"Could not resolve: \"{s}\"",
.{
import_record.path.text,
},
import_record.kind,
);
}
}
},
// assume other errors are already in the log
else => {},
}
}
}
}
},
.json => {
// parse the JSON _only_ to catch errors at build time.
const json_parse_result = json_parser.ParseJSONForBundling(&source, worker.data.log, worker.allocator) catch return;
if (json_parse_result.tag != .empty) {
const expr = brk: {
// If it's an ascii string, we just print it out with a big old JSON.parse()
if (json_parse_result.tag == .ascii) {
json_e_string = js_ast.E.String{ .utf8 = source.contents, .prefer_template = true };
var json_string_expr = js_ast.Expr{ .data = .{ .e_string = &json_e_string }, .loc = logger.Loc{ .start = 0 } };
json_call_args[0] = json_string_expr;
json_e_identifier = js_ast.E.Identifier{ .ref = Ref{ .source_index = 0, .inner_index = @intCast(Ref.Int, json_ast_symbols_list.len - 1) } };
json_e_call = js_ast.E.Call{
.target = js_ast.Expr{ .data = .{ .e_identifier = json_e_identifier }, .loc = logger.Loc{ .start = 0 } },
.args = std.mem.span(&json_call_args),
};
break :brk js_ast.Expr{ .data = .{ .e_call = &json_e_call }, .loc = logger.Loc{ .start = 0 } };
// If we're going to have to convert it to a UTF16, just make it an object actually
} else {
break :brk json_parse_result.expr;
}
};
var stmt = js_ast.Stmt.alloc(worker.allocator, js_ast.S.ExportDefault, js_ast.S.ExportDefault{
.value = js_ast.StmtOrExpr{ .expr = expr },
.default_name = js_ast.LocRef{ .loc = logger.Loc{}, .ref = Ref{} },
}, logger.Loc{ .start = 0 });
var stmts = worker.allocator.alloc(js_ast.Stmt, 1) catch unreachable;
stmts[0] = stmt;
var parts = worker.allocator.alloc(js_ast.Part, 1) catch unreachable;
parts[0] = js_ast.Part{ .stmts = stmts };
ast = js_ast.Ast.initTest(parts);
ast.runtime_imports = runtime.Runtime.Imports{};
ast.runtime_imports.@"$$m" = .{ .ref = Ref{ .source_index = 0, .inner_index = 0 }, .primary = Ref.None, .backup = Ref.None };
ast.runtime_imports.__export = .{ .ref = Ref{ .source_index = 0, .inner_index = 1 }, .primary = Ref.None, .backup = Ref.None };
ast.symbols = json_ast_symbols_list;
ast.module_ref = Ref{ .source_index = 0, .inner_index = 2 };
ast.exports_ref = ast.runtime_imports.__export.?.ref;
ast.bundle_export_ref = Ref{ .source_index = 0, .inner_index = 3 };
} else {
var parts = &[_]js_ast.Part{};
ast = js_ast.Ast.initTest(parts);
}
},
else => {
return;
},
}
switch (ast.parts.len) {
// It can be empty after parsing too
// A file like this is an example:
// "//# sourceMappingURL=validator.js.map"
0 => {
code_offset = try this.writeEmptyModule(module_data.package_path, module_id);
},
else => {
const register_ref = ast.runtime_imports.@"$$m".?.ref;
const E = js_ast.E;
const Expr = js_ast.Expr;
const Stmt = js_ast.Stmt;
var prepend_part: js_ast.Part = undefined;
var needs_prepend_part = false;
if (ast.parts.len > 1) {
for (ast.parts) |part| {
if (part.tag != .none and part.stmts.len > 0) {
prepend_part = part;
needs_prepend_part = true;
break;
}
}
}
var package_path = js_ast.E.String{ .utf8 = module_data.package_path };
var target_identifier = E.Identifier{ .ref = register_ref };
var cjs_args: [2]js_ast.G.Arg = undefined;
var module_binding = js_ast.B.Identifier{ .ref = ast.module_ref.? };
var exports_binding = js_ast.B.Identifier{ .ref = ast.exports_ref.? };
var part = &ast.parts[ast.parts.len - 1];
var new_stmts: [1]Stmt = undefined;
var register_args: [1]Expr = undefined;
var closure = E.Arrow{
.args = &cjs_args,
.body = .{
.loc = logger.Loc.Empty,
.stmts = part.stmts,
},
};
cjs_args[0] = js_ast.G.Arg{
.binding = js_ast.Binding{
.loc = logger.Loc.Empty,
.data = .{ .b_identifier = &module_binding },
},
};
cjs_args[1] = js_ast.G.Arg{
.binding = js_ast.Binding{
.loc = logger.Loc.Empty,
.data = .{ .b_identifier = &exports_binding },
},
};
var properties: [1]js_ast.G.Property = undefined;
var e_object = E.Object{
.properties = &properties,
};
const module_path_str = js_ast.Expr{ .data = .{ .e_string = &package_path }, .loc = logger.Loc.Empty };
properties[0] = js_ast.G.Property{
.key = module_path_str,
.value = Expr{ .loc = logger.Loc.Empty, .data = .{ .e_arrow = &closure } },
};
// if (!ast.uses_module_ref) {
// var symbol = &ast.symbols[ast.module_ref.?.inner_index];
// symbol.original_name = "_$$";
// }
// $$m(12345, "react", "index.js", function(module, exports) {
// })
var accessor = js_ast.E.Index{ .index = module_path_str, .target = js_ast.Expr{
.data = .{ .e_object = &e_object },
.loc = logger.Loc.Empty,
} };
register_args[0] = Expr{ .loc = logger.Loc.Empty, .data = .{ .e_index = &accessor } };
var call_register = E.Call{
.target = Expr{
.data = .{ .e_identifier = target_identifier },
.loc = logger.Loc{ .start = 0 },
},
.args = ®ister_args,
};
var register_expr = Expr{ .loc = call_register.target.loc, .data = .{ .e_call = &call_register } };
var decls: [1]js_ast.G.Decl = undefined;
var bundle_export_binding = js_ast.B.Identifier{ .ref = ast.runtime_imports.@"$$m".?.ref };
var binding = js_ast.Binding{
.loc = register_expr.loc,
.data = .{ .b_identifier = &bundle_export_binding },
};
decls[0] = js_ast.G.Decl{
.value = register_expr,
.binding = binding,
};
var export_var = js_ast.S.Local{
.decls = &decls,
.is_export = true,
};
new_stmts[0] = Stmt{ .loc = register_expr.loc, .data = .{ .s_local = &export_var } };
part.stmts = &new_stmts;
var writer = js_printer.NewFileWriter(this.tmpfile);
var symbols: [][]js_ast.Symbol = &([_][]js_ast.Symbol{ast.symbols});
// It should only have one part.
ast.parts = ast.parts[ast.parts.len - 1 ..];
const write_result =
// The difference between these two is `ascii_only`
// We try our best to print UTF-8.
// However, JavaScriptCore does not accept UTF-8.
// It accepts either latin1 characters as unsigned char
// or UTF-16 chars as uint16
// We don't want to add a UTF decoding pass to the .bun files
// because it's potentially 15 MB of code.
// If we store it as UTF-16 directly, then that 15 MB of code becomes 30 MB of code!
// lots of code!
//
if (!bundler.options.platform.isBun())
try js_printer.printCommonJSThreaded(
@TypeOf(writer),
writer,
ast,
js_ast.Symbol.Map.initList(symbols),
&source,
false,
js_printer.Options{
.to_module_ref = Ref.RuntimeRef,
.bundle_export_ref = ast.runtime_imports.@"$$m".?.ref,
.source_path = file_path,
.externals = ast.externals,
.indent = 0,
.require_ref = ast.require_ref,
.module_hash = module_id,
.runtime_imports = ast.runtime_imports,
.prepend_part_value = &prepend_part,
.prepend_part_key = if (needs_prepend_part) closure.body.stmts.ptr else null,
},
Linker,
&bundler.linker,
&this.write_lock,
std.fs.File,
this.tmpfile,
std.fs.File.getPos,
&this.tmpfile_byte_offset,
)
else
try js_printer.printCommonJSThreaded(
@TypeOf(writer),
writer,
ast,
js_ast.Symbol.Map.initList(symbols),
&source,
true,
js_printer.Options{
.to_module_ref = Ref.RuntimeRef,
.bundle_export_ref = ast.runtime_imports.@"$$m".?.ref,
.source_path = file_path,
.externals = ast.externals,
.indent = 0,
.require_ref = ast.require_ref,
.module_hash = module_id,
.runtime_imports = ast.runtime_imports,
.prepend_part_value = &prepend_part,
.prepend_part_key = if (needs_prepend_part) closure.body.stmts.ptr else null,
},
Linker,
&bundler.linker,
&this.write_lock,
std.fs.File,
this.tmpfile,
std.fs.File.getPos,
&this.tmpfile_byte_offset,
);
code_offset = write_result.off;
},
}
}
if (comptime isDebug) {
Output.prettyln("{s}@{s}/{s} - {d}:{d} \n", .{ package.name, package.version, package_relative_path, package.hash, module_id });
Output.flush();
std.debug.assert(package_relative_path.len > 0);
}
try this.appendToModuleList(
package,
module_id,
code_offset,
package_relative_path,
);
} else {
// If it's app code, scan but do not fully parse.
switch (loader) {
.jsx,
.tsx,
.js,
.ts,
=> {
const entry = bundler.resolver.caches.fs.readFileShared(
bundler.fs,
file_path.textZ(),
resolve.dirname_fd,
if (resolve.file_fd != 0) resolve.file_fd else null,
shared_buffer,
) catch return;
if (entry.contents.len == 0 or (entry.contents.len < 33 and strings.trim(entry.contents, " \n\r").len == 0)) return;
const source = logger.Source.initRecycledFile(Fs.File{ .path = file_path, .contents = entry.contents }, bundler.allocator) catch return null;
var jsx = bundler.options.jsx;
jsx.parse = loader.isJSX();
var opts = js_parser.Parser.Options.init(jsx, loader);
opts.macro_context = &worker.data.macro_context;
opts.macro_context.remap = resolve.getMacroRemappings();
try bundler.resolver.caches.js.scan(
bundler.allocator,
scan_pass_result,
opts,
bundler.options.define,
log,
&source,
);
worker.data.estimated_input_lines_of_code += scan_pass_result.approximate_newline_count;
{
for (scan_pass_result.import_records.items) |*import_record, i| {
if (import_record.is_internal or import_record.is_unused) {
continue;
}
if (bundler.resolver.resolve(source_dir, import_record.path.text, import_record.kind)) |*_resolved_import| {
if (_resolved_import.is_external) {
continue;
}
var path = _resolved_import.path() orelse continue;
const loader_ = this.bundler.options.loader(path.name.ext);
if (!loader_.isJavaScriptLikeOrJSON()) continue;
path.* = try path.dupeAlloc(this.allocator);
if (BundledModuleData.get(this, _resolved_import)) |mod| {
if (comptime !FeatureFlags.bundle_dynamic_import) {
if (import_record.kind == .dynamic)
continue;
} else {
// When app code dynamically imports a large file
// Don't bundle it. Leave it as a separate file.
// The main value from bundling in development is to minimize tiny, waterfall http requests
// If you're importing > 100 KB file dynamically, developer is probably explicitly trying to do that.
// There's a tradeoff between "I want to minimize page load time"
if (import_record.kind == .dynamic) {
this.dynamic_import_file_size_store_lock.lock();
defer this.dynamic_import_file_size_store_lock.unlock();
var dynamic_import_file_size = this.dynamic_import_file_size_store.getOrPut(mod.module_id) catch unreachable;
if (!dynamic_import_file_size.found_existing) {
var fd = _resolved_import.file_fd;
var can_close = false;
if (fd == 0) {
dynamic_import_file_size.value_ptr.* = 0;
fd = (std.fs.openFileAbsolute(path.textZ(), .{}) catch |err| {
this.log.addRangeWarningFmt(
&source,
import_record.range,
worker.allocator,
"{s} opening file: \"{s}\"",
.{ @errorName(err), path.text },
) catch unreachable;
continue;
}).handle;
can_close = true;
Fs.FileSystem.setMaxFd(fd);
}
defer {
if (can_close and bundler.fs.fs.needToCloseFiles()) {
var _file = std.fs.File{ .handle = fd };
_file.close();
_resolved_import.file_fd = 0;
} else if (FeatureFlags.store_file_descriptors) {
_resolved_import.file_fd = fd;
}
}
var file = std.fs.File{ .handle = fd };
var stat = file.stat() catch |err| {
this.log.addRangeWarningFmt(
&source,
import_record.range,
worker.allocator,
"{s} stat'ing file: \"{s}\"",
.{ @errorName(err), path.text },
) catch unreachable;
dynamic_import_file_size.value_ptr.* = 0;
continue;
};
dynamic_import_file_size.value_ptr.* = @truncate(u32, stat.size);
}
if (dynamic_import_file_size.value_ptr.* > 1024 * 100)
continue;
}
}
std.debug.assert(mod.module_id != 0);
try this.queue.upsert(
mod.module_id,
_resolved_import.*,
);
} else {
try this.queue.upsert(
_resolved_import.hash(
this.bundler.fs.top_level_dir,
loader_,
),
_resolved_import.*,
);
}
} else |err| {
switch (err) {
error.ModuleNotFound => {
if (!import_record.handles_import_errors) {
if (isPackagePath(import_record.path.text)) {
if (this.bundler.options.platform.isWebLike() and options.ExternalModules.isNodeBuiltin(import_record.path.text)) {
try log.addResolveErrorWithTextDupe(
&source,
import_record.range,
this.allocator,
"Could not resolve Node.js builtin: \"{s}\".",
.{import_record.path.text},
import_record.kind,
);
} else {
try log.addResolveErrorWithTextDupe(
&source,
import_record.range,
this.allocator,
"Could not resolve: \"{s}\". Maybe you need to \"bun install\"?",
.{import_record.path.text},
import_record.kind,
);
}
} else {
try log.addResolveErrorWithTextDupe(
&source,
import_record.range,
this.allocator,
"Could not resolve: \"{s}\"",
.{
import_record.path.text,
},
import_record.kind,
);
}
}
},
// assume other errors are already in the log
else => {},
}
}
}
}
},
else => {},
}
}
}
};
pub const BuildResolveResultPair = struct {
written: usize,
input_fd: ?StoredFileDescriptorType,
empty: bool = false,
};
pub fn buildWithResolveResult(
bundler: *ThisBundler,
resolve_result: _resolver.Result,
allocator: *std.mem.Allocator,
loader: options.Loader,
comptime Writer: type,
writer: Writer,
comptime import_path_format: options.BundleOptions.ImportPathFormat,
file_descriptor: ?StoredFileDescriptorType,
filepath_hash: u32,
comptime WatcherType: type,
watcher: *WatcherType,
client_entry_point: ?*ClientEntryPoint,
) !BuildResolveResultPair {
if (resolve_result.is_external) {
return BuildResolveResultPair{
.written = 0,
.input_fd = null,
};
}
errdefer bundler.resetStore();
var file_path = (resolve_result.pathConst() orelse {
return BuildResolveResultPair{
.written = 0,
.input_fd = null,
};
}).*;
if (strings.indexOf(file_path.text, bundler.fs.top_level_dir)) |i| {
file_path.pretty = file_path.text[i + bundler.fs.top_level_dir.len ..];
} else if (!file_path.is_symlink) {
file_path.pretty = allocator.dupe(u8, bundler.fs.relativeTo(file_path.text)) catch unreachable;
}
var old_bundler_allocator = bundler.allocator;
bundler.allocator = allocator;
defer bundler.allocator = old_bundler_allocator;
var old_linker_allocator = bundler.linker.allocator;
defer bundler.linker.allocator = old_linker_allocator;
bundler.linker.allocator = allocator;
switch (loader) {
.css => {
const CSSBundlerHMR = Css.NewBundler(
Writer,
@TypeOf(&bundler.linker),
@TypeOf(&bundler.resolver.caches.fs),
WatcherType,
@TypeOf(bundler.fs),
true,
);
const CSSBundler = Css.NewBundler(
Writer,
@TypeOf(&bundler.linker),
@TypeOf(&bundler.resolver.caches.fs),
WatcherType,
@TypeOf(bundler.fs),
false,
);
return BuildResolveResultPair{
.written = brk: {
if (bundler.options.hot_module_reloading) {
break :brk (try CSSBundlerHMR.bundle(
file_path.text,
bundler.fs,
writer,
watcher,
&bundler.resolver.caches.fs,
filepath_hash,
file_descriptor,
allocator,
bundler.log,
&bundler.linker,
)).written;
} else {
break :brk (try CSSBundler.bundle(
file_path.text,
bundler.fs,
writer,
watcher,
&bundler.resolver.caches.fs,
filepath_hash,
file_descriptor,
allocator,
bundler.log,
&bundler.linker,
)).written;
}
},
.input_fd = file_descriptor,
};
},
else => {
var result = bundler.parse(
ParseOptions{
.allocator = allocator,
.path = file_path,
.loader = loader,
.dirname_fd = resolve_result.dirname_fd,
.file_descriptor = file_descriptor,
.file_hash = filepath_hash,
.macro_remappings = resolve_result.getMacroRemappings(),
.jsx = resolve_result.jsx,
},
client_entry_point,
) orelse {
bundler.resetStore();
return BuildResolveResultPair{
.written = 0,
.input_fd = null,
};
};
if (result.empty) {
return BuildResolveResultPair{ .written = 0, .input_fd = result.input_fd, .empty = true };
}
try bundler.linker.link(file_path, &result, import_path_format, false);
if (bundler.options.platform.isBun()) {
return BuildResolveResultPair{
.written = switch (result.ast.exports_kind) {
.esm => try bundler.print(
result,
Writer,
writer,
.esm_ascii,
),
.cjs => try bundler.print(
result,
Writer,
writer,
.cjs_ascii,
),
else => unreachable,
},
.input_fd = result.input_fd,
};
}
return BuildResolveResultPair{
.written = switch (result.ast.exports_kind) {
.none, .esm => try bundler.print(
result,
Writer,
writer,
.esm,
),
.cjs => try bundler.print(
result,
Writer,
writer,
.cjs,
),
else => unreachable,
},
.input_fd = result.input_fd,
};
},
}
}
pub fn buildWithResolveResultEager(
bundler: *ThisBundler,
resolve_result: _resolver.Result,
comptime import_path_format: options.BundleOptions.ImportPathFormat,
comptime Outstream: type,
outstream: Outstream,
client_entry_point_: ?*ClientEntryPoint,
) !?options.OutputFile {
if (resolve_result.is_external) {
return null;
}
var file_path = (resolve_result.pathConst() orelse return null).*;
// Step 1. Parse & scan
const loader = bundler.options.loader(file_path.name.ext);
if (client_entry_point_) |client_entry_point| {
file_path = client_entry_point.source.path;
}
file_path.pretty = Linker.relative_paths_list.append(string, bundler.fs.relativeTo(file_path.text)) catch unreachable;
var output_file = options.OutputFile{
.input = file_path,
.loader = loader,
.value = undefined,
};
var file: std.fs.File = undefined;
if (Outstream == std.fs.Dir) {
const output_dir = outstream;
if (std.fs.path.dirname(file_path.pretty)) |dirname| {
try output_dir.makePath(dirname);
}
file = try output_dir.createFile(file_path.pretty, .{});
} else {
file = outstream;
}
switch (loader) {
.jsx, .tsx, .js, .ts, .json => {
var result = bundler.parse(
ParseOptions{
.allocator = bundler.allocator,
.path = file_path,
.loader = loader,
.dirname_fd = resolve_result.dirname_fd,
.file_descriptor = null,
.file_hash = null,
.macro_remappings = resolve_result.getMacroRemappings(),
.jsx = resolve_result.jsx,
},
client_entry_point_,
) orelse {
return null;
};
try bundler.linker.link(
file_path,
&result,
import_path_format,
false,
);
output_file.size = try bundler.print(
result,
js_printer.FileWriter,
js_printer.NewFileWriter(file),
.esm,
);
var file_op = options.OutputFile.FileOperation.fromFile(file.handle, file_path.pretty);
file_op.fd = file.handle;
file_op.is_tmpdir = false;
if (Outstream == std.fs.Dir) {
file_op.dir = outstream.fd;
if (bundler.fs.fs.needToCloseFiles()) {
file.close();
file_op.fd = 0;
}
}
output_file.value = .{ .move = file_op };
},
.css => {
const CSSWriter = Css.NewWriter(
std.fs.File,
@TypeOf(&bundler.linker),
import_path_format,
void,
);
const entry = bundler.resolver.caches.fs.readFile(
bundler.fs,
file_path.text,
resolve_result.dirname_fd,
!cache_files,
null,
) catch return null;
const _file = Fs.File{ .path = file_path, .contents = entry.contents };
var source = try logger.Source.initFile(_file, bundler.allocator);
source.contents_is_recycled = !cache_files;
var css_writer = CSSWriter.init(
&source,
file,
&bundler.linker,
bundler.log,
);
var did_warn = false;
try css_writer.run(bundler.log, bundler.allocator, &did_warn);
output_file.size = css_writer.written;
var file_op = options.OutputFile.FileOperation.fromFile(file.handle, file_path.pretty);
file_op.fd = file.handle;
file_op.is_tmpdir = false;
if (Outstream == std.fs.Dir) {
file_op.dir = outstream.fd;
if (bundler.fs.fs.needToCloseFiles()) {
file.close();
file_op.fd = 0;
}
}
output_file.value = .{ .move = file_op };
},
.file => {
var hashed_name = try bundler.linker.getHashedFilename(file_path, null);
var pathname = try bundler.allocator.alloc(u8, hashed_name.len + file_path.name.ext.len);
std.mem.copy(u8, pathname, hashed_name);
std.mem.copy(u8, pathname[hashed_name.len..], file_path.name.ext);
const dir = if (bundler.options.output_dir_handle) |output_handle| output_handle.fd else 0;
output_file.value = .{
.copy = options.OutputFile.FileOperation{
.pathname = pathname,
.dir = dir,
.is_outdir = true,
},
};
},
// // TODO:
// else => {},
}
return output_file;
}
pub fn print(
bundler: *ThisBundler,
result: ParseResult,
comptime Writer: type,
writer: Writer,
comptime format: js_printer.Format,
) !usize {
const ast = result.ast;
var symbols: [][]js_ast.Symbol = &([_][]js_ast.Symbol{ast.symbols});
return switch (format) {
.cjs => try js_printer.printCommonJS(
Writer,
writer,
ast,
js_ast.Symbol.Map.initList(symbols),
&result.source,
false,
js_printer.Options{
.to_module_ref = Ref.RuntimeRef,
.externals = ast.externals,
.runtime_imports = ast.runtime_imports,
.require_ref = ast.require_ref,
.css_import_behavior = bundler.options.cssImportBehavior(),
},
Linker,
&bundler.linker,
),
.esm => try js_printer.printAst(
Writer,
writer,
ast,
js_ast.Symbol.Map.initList(symbols),
&result.source,
false,
js_printer.Options{
.to_module_ref = Ref.RuntimeRef,
.externals = ast.externals,
.runtime_imports = ast.runtime_imports,
.require_ref = ast.require_ref,
.css_import_behavior = bundler.options.cssImportBehavior(),
},
Linker,
&bundler.linker,
),
.esm_ascii => try js_printer.printAst(
Writer,
writer,
ast,
js_ast.Symbol.Map.initList(symbols),
&result.source,
true,
js_printer.Options{
.to_module_ref = Ref.RuntimeRef,
.externals = ast.externals,
.runtime_imports = ast.runtime_imports,
.require_ref = ast.require_ref,
.css_import_behavior = bundler.options.cssImportBehavior(),
},
Linker,
&bundler.linker,
),
.cjs_ascii => try js_printer.printCommonJS(
Writer,
writer,
ast,
js_ast.Symbol.Map.initList(symbols),
&result.source,
true,
js_printer.Options{
.to_module_ref = Ref.RuntimeRef,
.externals = ast.externals,
.runtime_imports = ast.runtime_imports,
.require_ref = ast.require_ref,
.css_import_behavior = bundler.options.cssImportBehavior(),
},
Linker,
&bundler.linker,
),
};
}
pub const ParseOptions = struct {
allocator: *std.mem.Allocator,
dirname_fd: StoredFileDescriptorType,
file_descriptor: ?StoredFileDescriptorType = null,
file_hash: ?u32 = null,
path: Fs.Path,
loader: options.Loader,
jsx: options.JSX.Pragma,
macro_remappings: MacroRemap,
virtual_source: ?*const logger.Source = null,
};
pub fn parse(
bundler: *ThisBundler,
this_parse: ParseOptions,
client_entry_point_: anytype,
) ?ParseResult {
var allocator = this_parse.allocator;
const dirname_fd = this_parse.dirname_fd;
const file_descriptor = this_parse.file_descriptor;
const file_hash = this_parse.file_hash;
const path = this_parse.path;
const loader = this_parse.loader;
if (FeatureFlags.tracing) {
bundler.timer.reset();
}
defer {
if (FeatureFlags.tracing) {
bundler.elapsed += bundler.timer.read();
}
}
var result: ParseResult = undefined;
var input_fd: ?StoredFileDescriptorType = null;
const source: logger.Source = brk: {
if (this_parse.virtual_source) |virtual_source| {
break :brk virtual_source.*;
}
if (client_entry_point_) |client_entry_point| {
if (@hasField(std.meta.Child(@TypeOf(client_entry_point)), "source")) {
break :brk client_entry_point.source;
}
}
if (strings.eqlComptime(path.namespace, "node")) {
if (NodeFallbackModules.contentsFromPath(path.text)) |code| {
break :brk logger.Source.initPathString(path.text, code);
}
break :brk logger.Source.initPathString(path.text, "");
}
const entry = bundler.resolver.caches.fs.readFile(
bundler.fs,
path.text,
dirname_fd,
true,
file_descriptor,
) catch |err| {
bundler.log.addErrorFmt(null, logger.Loc.Empty, bundler.allocator, "{s} reading \"{s}\"", .{ @errorName(err), path.text }) catch {};
return null;
};
input_fd = entry.fd;
break :brk logger.Source.initRecycledFile(Fs.File{ .path = path, .contents = entry.contents }, bundler.allocator) catch return null;
};
if (source.contents.len == 0 or (source.contents.len < 33 and std.mem.trim(u8, source.contents, "\n\r ").len == 0)) {
return ParseResult{ .source = source, .input_fd = input_fd, .loader = loader, .empty = true, .ast = js_ast.Ast.empty };
}
switch (loader) {
.js,
.jsx,
.ts,
.tsx,
=> {
var jsx = this_parse.jsx;
jsx.parse = loader.isJSX();
var opts = js_parser.Parser.Options.init(jsx, loader);
opts.enable_bundling = false;
opts.transform_require_to_import = true;
opts.can_import_from_bundle = bundler.options.node_modules_bundle != null;
// HMR is enabled when devserver is running
// unless you've explicitly disabled it
// or you're running in SSR
// or the file is a node_module
opts.features.hot_module_reloading = bundler.options.hot_module_reloading and
bundler.options.platform.isNotBun() and
(!opts.can_import_from_bundle or
(opts.can_import_from_bundle and !path.isNodeModule()));
opts.features.react_fast_refresh = opts.features.hot_module_reloading and
jsx.parse and
bundler.options.jsx.supports_fast_refresh;
opts.filepath_hash_for_hmr = file_hash orelse 0;
opts.warn_about_unbundled_modules = bundler.options.platform.isNotBun();
if (bundler.macro_context == null) {
bundler.macro_context = js_ast.Macro.MacroContext.init(bundler);
}
// we'll just always enable top-level await
// this is incorrect for Node.js files which are CommonJS modules
opts.features.top_level_await = true;
opts.macro_context = &bundler.macro_context.?;
opts.macro_context.remap = this_parse.macro_remappings;
opts.features.is_macro_runtime = bundler.options.platform == .bun_macro;
const value = (bundler.resolver.caches.js.parse(
allocator,
opts,
bundler.options.define,
bundler.log,
&source,
) catch null) orelse return null;
return ParseResult{
.ast = value,
.source = source,
.loader = loader,
.input_fd = input_fd,
};
},
.json => {
var expr = json_parser.ParseJSON(&source, bundler.log, allocator) catch return null;
var stmt = js_ast.Stmt.alloc(allocator, js_ast.S.ExportDefault, js_ast.S.ExportDefault{
.value = js_ast.StmtOrExpr{ .expr = expr },
.default_name = js_ast.LocRef{ .loc = logger.Loc{}, .ref = Ref{} },
}, logger.Loc{ .start = 0 });
var stmts = allocator.alloc(js_ast.Stmt, 1) catch unreachable;
stmts[0] = stmt;
var parts = allocator.alloc(js_ast.Part, 1) catch unreachable;
parts[0] = js_ast.Part{ .stmts = stmts };
return ParseResult{
.ast = js_ast.Ast.initTest(parts),
.source = source,
.loader = loader,
.input_fd = input_fd,
};
},
.css => {},
else => Global.panic("Unsupported loader {s} for path: {s}", .{ loader, source.path.text }),
}
return null;
}
// This is public so it can be used by the HTTP handler when matching against public dir.
pub threadlocal var tmp_buildfile_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
threadlocal var tmp_buildfile_buf2: [std.fs.MAX_PATH_BYTES]u8 = undefined;
threadlocal var tmp_buildfile_buf3: [std.fs.MAX_PATH_BYTES]u8 = undefined;
// We try to be mostly stateless when serving
// This means we need a slightly different resolver setup
pub fn buildFile(
bundler: *ThisBundler,
log: *logger.Log,
allocator: *std.mem.Allocator,
path_to_use_: string,
_extension: string,
comptime client_entry_point_enabled: bool,
comptime serve_as_package_path: bool,
) !ServeResult {
var extension = _extension;
var old_log = bundler.log;
var old_allocator = bundler.allocator;
bundler.setLog(log);
defer bundler.setLog(old_log);
var path_to_use = path_to_use_;
if (strings.eqlComptime(path_to_use, "__runtime.js")) {
return ServeResult{
.file = options.OutputFile.initBuf(runtime.Runtime.sourceContent(), "__runtime.js", .js),
.mime_type = MimeType.javascript,
};
}
defer {
js_ast.Expr.Data.Store.reset();
js_ast.Stmt.Data.Store.reset();
}
// If the extension is .js, omit it.
// if (absolute_path.len > ".js".len and strings.eqlComptime(absolute_path[absolute_path.len - ".js".len ..], ".js")) {
// absolute_path = absolute_path[0 .. absolute_path.len - ".js".len];
// }
// All non-absolute paths are ./paths
if (path_to_use[0] != '/' and path_to_use[0] != '.') {
tmp_buildfile_buf3[0..2].* = "./".*;
@memcpy(tmp_buildfile_buf3[2..], path_to_use.ptr, path_to_use.len);
path_to_use = tmp_buildfile_buf3[0 .. 2 + path_to_use.len];
}
const resolved = if (comptime !client_entry_point_enabled) (try bundler.resolver.resolve(bundler.fs.top_level_dir, path_to_use, .stmt)) else brk: {
const absolute_pathname = Fs.PathName.init(path_to_use);
const loader_for_ext = bundler.options.loader(absolute_pathname.ext);
// The expected pathname looks like:
// /pages/index.entry.tsx
// /pages/index.entry.js
// /pages/index.entry.ts
// /pages/index.entry.jsx
if (loader_for_ext.supportsClientEntryPoint()) {
const absolute_pathname_pathname = Fs.PathName.init(absolute_pathname.base);
if (strings.eqlComptime(absolute_pathname_pathname.ext, ".entry")) {
const trail_dir = absolute_pathname.dirWithTrailingSlash();
var len: usize = trail_dir.len;
std.mem.copy(u8, tmp_buildfile_buf2[0..len], trail_dir);
std.mem.copy(u8, tmp_buildfile_buf2[len..], absolute_pathname_pathname.base);
len += absolute_pathname_pathname.base.len;
std.mem.copy(u8, tmp_buildfile_buf2[len..], absolute_pathname.ext);
len += absolute_pathname.ext.len;
std.debug.assert(len > 0);
const decoded_entry_point_path = tmp_buildfile_buf2[0..len];
break :brk (try bundler.resolver.resolve(bundler.fs.top_level_dir, decoded_entry_point_path, .entry_point));
}
}
break :brk (try bundler.resolver.resolve(bundler.fs.top_level_dir, path_to_use, .stmt));
};
const path = (resolved.pathConst() orelse return error.ModuleNotFound);
const loader = bundler.options.loader(path.name.ext);
const mime_type_ext = bundler.options.out_extensions.get(path.name.ext) orelse path.name.ext;
switch (loader) {
.js, .jsx, .ts, .tsx, .css => {
return ServeResult{
.file = options.OutputFile.initPending(loader, resolved),
.mime_type = MimeType.byLoader(
loader,
mime_type_ext[1..],
),
};
},
.json => {
return ServeResult{
.file = options.OutputFile.initPending(loader, resolved),
.mime_type = MimeType.transpiled_json,
};
},
else => {
var abs_path = path.text;
const file = try std.fs.openFileAbsolute(abs_path, .{ .read = true });
var stat = try file.stat();
return ServeResult{
.file = options.OutputFile.initFile(file, abs_path, stat.size),
.mime_type = MimeType.byLoader(
loader,
mime_type_ext[1..],
),
};
},
}
}
pub fn normalizeEntryPointPath(bundler: *ThisBundler, _entry: string) string {
var paths = [_]string{_entry};
var entry = bundler.fs.abs(&paths);
std.fs.accessAbsolute(entry, .{}) catch |err| {
return _entry;
};
entry = bundler.fs.relativeTo(entry);
if (!strings.startsWith(entry, "./")) {
// Entry point paths without a leading "./" are interpreted as package
// paths. This happens because they go through general path resolution
// like all other import paths so that plugins can run on them. Requiring
// a leading "./" for a relative path simplifies writing plugins because
// entry points aren't a special case.
//
// However, requiring a leading "./" also breaks backward compatibility
// and makes working with the CLI more difficult. So attempt to insert
// "./" automatically when needed. We don't want to unconditionally insert
// a leading "./" because the path may not be a file system path. For
// example, it may be a URL. So only insert a leading "./" when the path
// is an exact match for an existing file.
var __entry = bundler.allocator.alloc(u8, "./".len + entry.len) catch unreachable;
__entry[0] = '.';
__entry[1] = '/';
std.mem.copy(u8, __entry[2..__entry.len], entry);
entry = __entry;
}
return entry;
}
fn enqueueEntryPoints(bundler: *ThisBundler, entry_points: []_resolver.Result, comptime normalize_entry_point: bool) usize {
var entry_point_i: usize = 0;
for (bundler.options.entry_points) |_entry| {
var entry: string = if (comptime normalize_entry_point) bundler.normalizeEntryPointPath(_entry) else _entry;
defer {
js_ast.Expr.Data.Store.reset();
js_ast.Stmt.Data.Store.reset();
}
const result = bundler.resolver.resolve(bundler.fs.top_level_dir, entry, .entry_point) catch |err| {
Output.prettyError("Error resolving \"{s}\": {s}\n", .{ entry, @errorName(err) });
continue;
};
if (result.pathConst() == null) {
Output.prettyError("\"{s}\" is disabled due to \"browser\" field in package.json.\n", .{
entry,
});
continue;
}
if (bundler.linker.enqueueResolveResult(&result) catch unreachable) {
entry_points[entry_point_i] = result;
entry_point_i += 1;
}
}
return entry_point_i;
}
pub fn bundle(
allocator: *std.mem.Allocator,
log: *logger.Log,
opts: Api.TransformOptions,
) !options.TransformResult {
var bundler = try ThisBundler.init(allocator, log, opts, null, null);
bundler.configureLinker();
try bundler.configureRouter(false);
try bundler.configureDefines();
var skip_normalize = false;
var load_from_routes = false;
if (bundler.options.routes.routes_enabled and bundler.options.entry_points.len == 0) {
if (bundler.router) |router| {
bundler.options.entry_points = try router.getEntryPoints();
skip_normalize = true;
load_from_routes = true;
}
}
if (bundler.options.write and bundler.options.output_dir.len > 0) {}
// 100.00 µs std.fifo.LinearFifo(resolver.Result,std.fifo.LinearFifoBufferType { .Dynamic = {}}).writeItemAssumeCapacity
if (bundler.options.resolve_mode != .lazy) {
try bundler.resolve_queue.ensureUnusedCapacity(3);
}
var entry_points = try allocator.alloc(_resolver.Result, bundler.options.entry_points.len);
if (skip_normalize) {
entry_points = entry_points[0..bundler.enqueueEntryPoints(entry_points, false)];
} else {
entry_points = entry_points[0..bundler.enqueueEntryPoints(entry_points, true)];
}
if (log.level == .verbose) {
bundler.resolver.debug_logs = try DebugLogs.init(allocator);
}
var did_start = false;
if (bundler.options.output_dir_handle == null) {
const outstream = std.io.getStdOut();
if (load_from_routes) {
if (bundler.options.framework) |*framework| {
if (framework.client.isEnabled()) {
did_start = true;
try switch (bundler.options.import_path_format) {
.relative => bundler.processResolveQueue(.relative, true, @TypeOf(outstream), outstream),
.relative_nodejs => bundler.processResolveQueue(.relative_nodejs, true, @TypeOf(outstream), outstream),
.absolute_url => bundler.processResolveQueue(.absolute_url, true, @TypeOf(outstream), outstream),
.absolute_path => bundler.processResolveQueue(.absolute_path, true, @TypeOf(outstream), outstream),
.package_path => bundler.processResolveQueue(.package_path, true, @TypeOf(outstream), outstream),
};
}
}
}
if (!did_start) {
try switch (bundler.options.import_path_format) {
.relative => bundler.processResolveQueue(.relative, false, @TypeOf(outstream), outstream),
.relative_nodejs => bundler.processResolveQueue(.relative_nodejs, false, @TypeOf(outstream), outstream),
.absolute_url => bundler.processResolveQueue(.absolute_url, false, @TypeOf(outstream), outstream),
.absolute_path => bundler.processResolveQueue(.absolute_path, false, @TypeOf(outstream), outstream),
.package_path => bundler.processResolveQueue(.package_path, false, @TypeOf(outstream), outstream),
};
}
} else {
const output_dir = bundler.options.output_dir_handle orelse {
Output.printError("Invalid or missing output directory.", .{});
Output.flush();
Global.crash();
};
if (load_from_routes) {
if (bundler.options.framework) |*framework| {
if (framework.client.isEnabled()) {
did_start = true;
try switch (bundler.options.import_path_format) {
.relative => bundler.processResolveQueue(.relative, true, std.fs.Dir, output_dir),
.relative_nodejs => bundler.processResolveQueue(.relative_nodejs, true, std.fs.Dir, output_dir),
.absolute_url => bundler.processResolveQueue(.absolute_url, true, std.fs.Dir, output_dir),
.absolute_path => bundler.processResolveQueue(.absolute_path, true, std.fs.Dir, output_dir),
.package_path => bundler.processResolveQueue(.package_path, true, std.fs.Dir, output_dir),
};
}
}
}
if (!did_start) {
try switch (bundler.options.import_path_format) {
.relative => bundler.processResolveQueue(.relative, false, std.fs.Dir, output_dir),
.relative_nodejs => bundler.processResolveQueue(.relative_nodejs, false, std.fs.Dir, output_dir),
.absolute_url => bundler.processResolveQueue(.absolute_url, false, std.fs.Dir, output_dir),
.absolute_path => bundler.processResolveQueue(.absolute_path, false, std.fs.Dir, output_dir),
.package_path => bundler.processResolveQueue(.package_path, false, std.fs.Dir, output_dir),
};
}
}
// if (log.level == .verbose) {
// for (log.msgs.items) |msg| {
// try msg.writeFormat(std.io.getStdOut().writer());
// }
// }
if (bundler.linker.any_needs_runtime) {
try bundler.output_files.append(
options.OutputFile.initBuf(runtime.Runtime.sourceContent(), bundler.linker.runtime_source_path, .js),
);
}
if (FeatureFlags.tracing) {
Output.prettyErrorln(
"<r><d>\n---Tracing---\nResolve time: {d}\nParsing time: {d}\n---Tracing--\n\n<r>",
.{
bundler.resolver.elapsed,
bundler.elapsed,
},
);
}
var final_result = try options.TransformResult.init(try allocator.dupe(u8, bundler.result.outbase), bundler.output_files.toOwnedSlice(), log, allocator);
final_result.root_dir = bundler.options.output_dir_handle;
return final_result;
}
// pub fn processResolveQueueWithThreadPool(bundler)
pub fn processResolveQueue(
bundler: *ThisBundler,
comptime import_path_format: options.BundleOptions.ImportPathFormat,
comptime wrap_entry_point: bool,
comptime Outstream: type,
outstream: Outstream,
) !void {
// var count: u8 = 0;
while (bundler.resolve_queue.readItem()) |item| {
js_ast.Expr.Data.Store.reset();
js_ast.Stmt.Data.Store.reset();
// defer count += 1;
if (comptime wrap_entry_point) {
var path = item.pathConst() orelse unreachable;
const loader = bundler.options.loader(path.name.ext);
if (item.import_kind == .entry_point and loader.supportsClientEntryPoint()) {
var client_entry_point = try bundler.allocator.create(ClientEntryPoint);
client_entry_point.* = ClientEntryPoint{};
try client_entry_point.generate(ThisBundler, bundler, path.name, bundler.options.framework.?.client.path);
try bundler.virtual_modules.append(client_entry_point);
const entry_point_output_file = bundler.buildWithResolveResultEager(
item,
import_path_format,
Outstream,
outstream,
client_entry_point,
) catch continue orelse continue;
bundler.output_files.append(entry_point_output_file) catch unreachable;
js_ast.Expr.Data.Store.reset();
js_ast.Stmt.Data.Store.reset();
// At this point, the entry point will be de-duped.
// So we just immediately build it.
var item_not_entrypointed = item;
item_not_entrypointed.import_kind = .stmt;
const original_output_file = bundler.buildWithResolveResultEager(
item_not_entrypointed,
import_path_format,
Outstream,
outstream,
null,
) catch continue orelse continue;
bundler.output_files.append(original_output_file) catch unreachable;
continue;
}
}
const output_file = bundler.buildWithResolveResultEager(
item,
import_path_format,
Outstream,
outstream,
null,
) catch continue orelse continue;
bundler.output_files.append(output_file) catch unreachable;
// if (count >= 3) return try bundler.processResolveQueueWithThreadPool(import_path_format, wrap_entry_point, Outstream, outstream);
}
}
};
pub const Transformer = struct {
opts: Api.TransformOptions,
log: *logger.Log,
allocator: *std.mem.Allocator,
platform: options.Platform = undefined,
out_extensions: std.StringHashMap(string) = undefined,
output_path: string,
cwd: string,
define: *Define,
pub fn transform(
allocator: *std.mem.Allocator,
log: *logger.Log,
opts: Api.TransformOptions,
) !options.TransformResult {
js_ast.Expr.Data.Store.create(allocator);
js_ast.Stmt.Data.Store.create(allocator);
const platform = options.Platform.from(opts.platform);
var define = try options.definesFromTransformOptions(
allocator,
log,
opts.define,
false,
platform,
null,
null,
);
const cwd = if (opts.absolute_working_dir) |workdir| try std.fs.realpathAlloc(allocator, workdir) else try std.process.getCwdAlloc(allocator);
const output_dir_parts = [_]string{ try std.process.getCwdAlloc(allocator), opts.output_dir orelse "out" };
const output_dir = try std.fs.path.join(allocator, &output_dir_parts);
var output_files = try std.ArrayList(options.OutputFile).initCapacity(allocator, opts.entry_points.len);
const out_extensions = platform.outExtensions(allocator);
var loader_map = try options.loadersFromTransformOptions(allocator, opts.loaders);
var use_default_loaders = loader_map.count() == 0;
var jsx = if (opts.jsx) |_jsx| try options.JSX.Pragma.fromApi(_jsx, allocator) else options.JSX.Pragma{};
var output_i: usize = 0;
var chosen_alloc: *std.mem.Allocator = allocator;
var arena: std.heap.ArenaAllocator = undefined;
const use_arenas = opts.entry_points.len > 8;
var ulimit: usize = Fs.FileSystem.RealFS.adjustUlimit() catch unreachable;
var care_about_closing_files = !(FeatureFlags.store_file_descriptors and opts.entry_points.len * 2 < ulimit);
var transformer = Transformer{
.log = log,
.allocator = allocator,
.opts = opts,
.cwd = cwd,
.platform = platform,
.out_extensions = out_extensions,
.define = define,
.output_path = output_dir,
};
const write_to_output_dir = opts.entry_points.len > 1 or opts.output_dir != null;
var output_dir_handle: ?std.fs.Dir = null;
if (write_to_output_dir) {
output_dir_handle = try options.openOutputDir(output_dir);
}
if (write_to_output_dir) {
for (opts.entry_points) |entry_point, i| {
try transformer.processEntryPoint(
entry_point,
i,
&output_files,
output_dir_handle,
.disk,
care_about_closing_files,
use_default_loaders,
loader_map,
&jsx,
);
}
} else {
for (opts.entry_points) |entry_point, i| {
try transformer.processEntryPoint(
entry_point,
i,
&output_files,
output_dir_handle,
.stdout,
care_about_closing_files,
use_default_loaders,
loader_map,
&jsx,
);
}
}
return try options.TransformResult.init(output_dir, output_files.toOwnedSlice(), log, allocator);
}
pub fn processEntryPoint(
transformer: *Transformer,
entry_point: string,
i: usize,
output_files: *std.ArrayList(options.OutputFile),
_output_dir: ?std.fs.Dir,
comptime write_destination_type: options.WriteDestination,
care_about_closing_files: bool,
use_default_loaders: bool,
loader_map: std.StringHashMap(options.Loader),
jsx: *options.JSX.Pragma,
) !void {
var allocator = transformer.allocator;
var log = transformer.log;
var _log = logger.Log.init(allocator);
var __log = &_log;
const absolutePath = resolve_path.joinAbs(transformer.cwd, .auto, entry_point);
const file = try std.fs.openFileAbsolute(absolutePath, std.fs.File.OpenFlags{ .read = true });
defer {
if (care_about_closing_files) {
file.close();
}
}
const stat = try file.stat();
const code = try file.readToEndAlloc(allocator, stat.size);
defer {
if (_log.msgs.items.len == 0) {
allocator.free(code);
}
_log.appendTo(log) catch {};
}
const _file = Fs.File{ .path = Fs.Path.init(entry_point), .contents = code };
var source = try logger.Source.initFile(_file, allocator);
var loader: options.Loader = undefined;
if (use_default_loaders) {
loader = options.defaultLoaders.get(std.fs.path.extension(absolutePath)) orelse return;
} else {
loader = options.Loader.forFileName(
entry_point,
loader_map,
) orelse return;
}
var _source = &source;
var output_file = options.OutputFile{
.input = _file.path,
.loader = loader,
.value = undefined,
};
var file_to_write: std.fs.File = undefined;
var output_path: Fs.Path = undefined;
switch (write_destination_type) {
.stdout => {
file_to_write = std.io.getStdOut();
output_path = Fs.Path.init("stdout");
},
.disk => {
const output_dir = _output_dir orelse unreachable;
output_path = Fs.Path.init(try allocator.dupe(u8, resolve_path.relative(transformer.cwd, entry_point)));
file_to_write = try output_dir.createFile(entry_point, .{});
},
}
switch (loader) {
.jsx, .js, .ts, .tsx => {
jsx.parse = loader.isJSX();
var file_op = options.OutputFile.FileOperation.fromFile(file_to_write.handle, output_path.pretty);
var parser_opts = js_parser.Parser.Options.init(jsx.*, loader);
file_op.is_tmpdir = false;
output_file.value = .{ .move = file_op };
if (_output_dir) |output_dir| {
file_op.dir = output_dir.fd;
}
file_op.fd = file.handle;
var parser = try js_parser.Parser.init(parser_opts, log, _source, transformer.define, allocator);
parser_opts.can_import_from_bundle = false;
const result = try parser.parse();
const ast = result.ast;
var symbols: [][]js_ast.Symbol = &([_][]js_ast.Symbol{ast.symbols});
output_file.size = try js_printer.printAst(
js_printer.FileWriter,
js_printer.NewFileWriter(file_to_write),
ast,
js_ast.Symbol.Map.initList(symbols),
_source,
false,
js_printer.Options{
.to_module_ref = Ref.RuntimeRef,
.externals = ast.externals,
.transform_imports = false,
.runtime_imports = ast.runtime_imports,
},
?*c_void,
null,
);
},
else => {
unreachable;
},
}
js_ast.Expr.Data.Store.reset();
js_ast.Stmt.Data.Store.reset();
try output_files.append(output_file);
}
pub fn _transform(
allocator: *std.mem.Allocator,
log: *logger.Log,
opts: js_parser.Parser.Options,
loader: options.Loader,
define: *const Define,
source: *const logger.Source,
comptime Writer: type,
writer: Writer,
) !usize {
var ast: js_ast.Ast = undefined;
switch (loader) {
.json => {
var expr = try json_parser.ParseJSON(source, log, allocator);
var stmt = js_ast.Stmt.alloc(allocator, js_ast.S.ExportDefault{
.value = js_ast.StmtOrExpr{ .expr = expr },
.default_name = js_ast.LocRef{ .loc = logger.Loc{}, .ref = Ref{} },
}, logger.Loc{ .start = 0 });
var stmts = try allocator.alloc(js_ast.Stmt, 1);
stmts[0] = stmt;
var parts = try allocator.alloc(js_ast.Part, 1);
parts[0] = js_ast.Part{ .stmts = stmts };
ast = js_ast.Ast.initTest(parts);
},
.jsx, .tsx, .ts, .js => {
var parser = try js_parser.Parser.init(opts, log, source, define, allocator);
var res = try parser.parse();
ast = res.ast;
if (FeatureFlags.print_ast) {
try ast.toJSON(allocator, std.io.getStdErr().writer());
}
},
else => {
Global.panic("Unsupported loader: {s} for path: {s}", .{ loader, source.path.text });
},
}
var symbols: [][]js_ast.Symbol = &([_][]js_ast.Symbol{ast.symbols});
return try js_printer.printAst(
Writer,
writer,
ast,
js_ast.Symbol.Map.initList(symbols),
source,
false,
js_printer.Options{
.to_module_ref = ast.module_ref orelse js_ast.Ref{ .inner_index = 0 },
.transform_imports = false,
.runtime_imports = ast.runtime_imports,
},
null,
);
}
};
pub const ServeResult = struct {
file: options.OutputFile,
mime_type: MimeType,
};
pub const FallbackEntryPoint = struct {
code_buffer: [8096]u8 = undefined,
path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined,
source: logger.Source = undefined,
built_code: string = "",
pub fn generate(
entry: *FallbackEntryPoint,
input_path: string,
comptime BundlerType: type,
bundler: *BundlerType,
) !void {
// This is *extremely* naive.
// The basic idea here is this:
// --
// import * as EntryPoint from 'entry-point';
// import boot from 'framework';
// boot(EntryPoint);
// --
// We go through the steps of printing the code -- only to then parse/transpile it because
// we want it to go through the linker and the rest of the transpilation process
const dir_to_use: string = bundler.fs.top_level_dir;
const disable_css_imports = bundler.options.framework.?.client_css_in_js != .auto_onimportcss;
var code: string = undefined;
if (disable_css_imports) {
const fmt =
\\globalThis.Bun_disableCSSImports = true;
\\import boot from '{s}';
\\boot(globalThis.__BUN_DATA__);
;
const args = .{
input_path,
};
const count = std.fmt.count(fmt, args);
if (count < entry.code_buffer.len) {
code = try std.fmt.bufPrint(&entry.code_buffer, fmt, args);
} else {
code = try std.fmt.allocPrint(bundler.allocator, fmt, args);
}
} else {
const fmt =
\\import boot from '{s}';
\\boot(globalThis.__BUN_DATA__);
;
const args = .{
input_path,
};
const count = std.fmt.count(fmt, args);
if (count < entry.code_buffer.len) {
code = try std.fmt.bufPrint(&entry.code_buffer, fmt, args);
} else {
code = try std.fmt.allocPrint(bundler.allocator, fmt, args);
}
}
entry.source = logger.Source.initPathString(input_path, code);
entry.source.path.namespace = "fallback-entry";
}
};
pub const ClientEntryPoint = struct {
code_buffer: [8096]u8 = undefined,
path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined,
source: logger.Source = undefined,
pub fn isEntryPointPath(extname: string) bool {
return strings.startsWith("entry.", extname);
}
pub fn generateEntryPointPath(outbuffer: []u8, original_path: Fs.PathName) string {
var joined_base_and_dir_parts = [_]string{ original_path.dir, original_path.base };
var generated_path = Fs.FileSystem.instance.absBuf(&joined_base_and_dir_parts, outbuffer);
std.mem.copy(u8, outbuffer[generated_path.len..], ".entry");
generated_path = outbuffer[0 .. generated_path.len + ".entry".len];
std.mem.copy(u8, outbuffer[generated_path.len..], original_path.ext);
return outbuffer[0 .. generated_path.len + original_path.ext.len];
}
pub fn decodeEntryPointPath(outbuffer: []u8, original_path: Fs.PathName) string {
var joined_base_and_dir_parts = [_]string{ original_path.dir, original_path.base };
var generated_path = Fs.FileSystem.instance.absBuf(&joined_base_and_dir_parts, outbuffer);
var original_ext = original_path.ext;
if (strings.indexOf(original_path.ext, "entry")) |entry_i| {
original_ext = original_path.ext[entry_i + "entry".len ..];
}
std.mem.copy(u8, outbuffer[generated_path.len..], original_ext);
return outbuffer[0 .. generated_path.len + original_ext.len];
}
pub fn generate(entry: *ClientEntryPoint, comptime BundlerType: type, bundler: *BundlerType, original_path: Fs.PathName, client: string) !void {
// This is *extremely* naive.
// The basic idea here is this:
// --
// import * as EntryPoint from 'entry-point';
// import boot from 'framework';
// boot(EntryPoint);
// --
// We go through the steps of printing the code -- only to then parse/transpile it because
// we want it to go through the linker and the rest of the transpilation process
const dir_to_use: string = original_path.dirWithTrailingSlash();
const disable_css_imports = bundler.options.framework.?.client_css_in_js != .auto_onimportcss;
var code: string = undefined;
if (disable_css_imports) {
code = try std.fmt.bufPrint(
&entry.code_buffer,
\\globalThis.Bun_disableCSSImports = true;
\\import boot from '{s}';
\\import * as EntryPoint from '{s}{s}';
\\boot(EntryPoint);
,
.{
client,
dir_to_use,
original_path.filename,
},
);
} else {
code = try std.fmt.bufPrint(
&entry.code_buffer,
\\import boot from '{s}';
\\if ('setLoaded' in boot) boot.setLoaded(loaded);
\\import * as EntryPoint from '{s}{s}';
\\boot(EntryPoint);
,
.{
client,
dir_to_use,
original_path.filename,
},
);
}
entry.source = logger.Source.initPathString(generateEntryPointPath(&entry.path_buffer, original_path), code);
entry.source.path.namespace = "client-entry";
}
};
pub const ServerEntryPoint = struct {
code_buffer: [std.fs.MAX_PATH_BYTES * 2 + 500]u8 = undefined,
output_code_buffer: [std.fs.MAX_PATH_BYTES * 8 + 500]u8 = undefined,
source: logger.Source = undefined,
pub fn generate(
entry: *ServerEntryPoint,
comptime BundlerType: type,
bundler: *BundlerType,
original_path: Fs.PathName,
name: string,
) !void {
// This is *extremely* naive.
// The basic idea here is this:
// --
// import * as EntryPoint from 'entry-point';
// import boot from 'framework';
// boot(EntryPoint);
// --
// We go through the steps of printing the code -- only to then parse/transpile it because
// we want it to go through the linker and the rest of the transpilation process
const dir_to_use: string = original_path.dirWithTrailingSlash();
const code = try std.fmt.bufPrint(
&entry.code_buffer,
\\//Auto-generated file
\\import * as start from '{s}{s}';
\\export * from '{s}{s}';
\\if ('default' in start && "__internalIsCommonJSNamespace" in globalThis && __internalIsCommonJSNamespace(start)) {{
\\ start.default();
\\}}
,
.{
dir_to_use,
original_path.filename,
dir_to_use,
original_path.filename,
},
);
entry.source = logger.Source.initPathString(name, code);
entry.source.path.text = name;
entry.source.path.namespace = "server-entry";
}
};
pub const ResolveResults = std.AutoHashMap(
u64,
void,
);
pub const ResolveQueue = std.fifo.LinearFifo(
_resolver.Result,
std.fifo.LinearFifoBufferType.Dynamic,
);
// This is not very fast. The idea is: we want to generate a unique entry point
// per macro function export that registers the macro Registering the macro
// happens in VirtualMachine We "register" it which just marks the JSValue as
// protected. This is mostly a workaround for being unable to call ESM exported
// functions from C++. When that is resolved, we should remove this.
pub const MacroEntryPoint = struct {
code_buffer: [std.fs.MAX_PATH_BYTES * 2 + 500]u8 = undefined,
output_code_buffer: [std.fs.MAX_PATH_BYTES * 8 + 500]u8 = undefined,
source: logger.Source = undefined,
pub fn generateID(entry_path: string, function_name: string, buf: []u8, len: *u32) i32 {
var hasher = std.hash.Wyhash.init(0);
hasher.update(js_ast.Macro.namespaceWithColon);
hasher.update(entry_path);
hasher.update(function_name);
const truncated_u32 = @truncate(u32, hasher.final());
const specifier = std.fmt.bufPrint(buf, js_ast.Macro.namespaceWithColon ++ "//{x}.js", .{truncated_u32}) catch unreachable;
len.* = @truncate(u32, specifier.len);
return generateIDFromSpecifier(specifier);
}
pub fn generateIDFromSpecifier(specifier: string) i32 {
return @bitCast(i32, @truncate(u32, std.hash.Wyhash.hash(0, specifier)));
}
pub fn generate(
entry: *MacroEntryPoint,
bundler: *Bundler,
import_path: Fs.PathName,
function_name: string,
macro_id: i32,
macro_label_: string,
) !void {
const dir_to_use: string = import_path.dirWithTrailingSlash();
std.mem.copy(u8, entry.code_buffer[0..macro_label_.len], macro_label_);
const macro_label = entry.code_buffer[0..macro_label_.len];
const code = try std.fmt.bufPrint(
entry.code_buffer[macro_label.len..],
\\//Auto-generated file
\\var Macros;
\\try {{
\\ Macros = await import('{s}{s}');
\\}} catch (err) {{
\\ console.error("Error importing macro");
\\ throw err;
\\}}
\\if (!('{s}' in Macros)) {{
\\ throw new Error("Macro '{s}' not found in '{s}{s}'");
\\}}
\\
\\Bun.registerMacro({d}, Macros['{s}']);
,
.{
dir_to_use,
import_path.filename,
function_name,
function_name,
dir_to_use,
import_path.filename,
macro_id,
function_name,
},
);
entry.source = logger.Source.initPathString(macro_label, code);
entry.source.path.text = macro_label;
entry.source.path.namespace = js_ast.Macro.namespace;
}
};
|