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
|
const bun = @import("root").bun;
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const StoredFileDescriptorType = bun.StoredFileDescriptorType;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const Api = @import("../api/schema.zig").Api;
const std = @import("std");
const options = @import("../options.zig");
const cache = @import("../cache.zig");
const logger = @import("root").bun.logger;
const js_ast = bun.JSAst;
const fs = @import("../fs.zig");
const resolver = @import("./resolver.zig");
const js_lexer = bun.js_lexer;
const resolve_path = @import("./resolve_path.zig");
// Assume they're not going to have hundreds of main fields or browser map
// so use an array-backed hash table instead of bucketed
const MainFieldMap = bun.StringMap;
pub const BrowserMap = bun.StringMap;
pub const MacroImportReplacementMap = bun.StringArrayHashMap(string);
pub const MacroMap = bun.StringArrayHashMapUnmanaged(MacroImportReplacementMap);
const ScriptsMap = bun.StringArrayHashMap(string);
const Semver = @import("../install/semver.zig");
const Dependency = @import("../install/dependency.zig");
const String = @import("../install/semver.zig").String;
const Version = Semver.Version;
const Install = @import("../install/install.zig");
const FolderResolver = @import("../install/resolvers/folder_resolver.zig");
const Architecture = @import("../install/npm.zig").Architecture;
const OperatingSystem = @import("../install/npm.zig").OperatingSystem;
pub const SideEffectsMap = std.HashMapUnmanaged(bun.StringHashMapUnowned.Key, void, bun.StringHashMapUnowned.Adapter, 80);
pub const DependencyMap = struct {
map: HashMap = .{},
source_buf: []const u8 = "",
pub const HashMap = std.ArrayHashMapUnmanaged(
String,
Dependency,
String.ArrayHashContext,
false,
);
};
pub const PackageJSON = struct {
pub const LoadFramework = enum {
none,
development,
production,
};
pub fn generateHash(package_json: *PackageJSON) void {
var hashy: [1024]u8 = undefined;
@memset(&hashy, 0);
var used: usize = 0;
bun.copy(u8, &hashy, package_json.name);
used = package_json.name.len;
hashy[used] = '@';
used += 1;
bun.copy(u8, hashy[used..], package_json.version);
used += package_json.version.len;
package_json.hash = std.hash.Murmur3_32.hash(hashy[0..used]);
}
const node_modules_path = std.fs.path.sep_str ++ "node_modules" ++ std.fs.path.sep_str;
pub fn nameForImport(this: *const PackageJSON, allocator: std.mem.Allocator) !string {
if (strings.indexOf(this.source.path.text, node_modules_path)) |_| {
return this.name;
} else {
const parent = this.source.path.name.dirWithTrailingSlash();
if (strings.indexOf(parent, fs.FileSystem.instance.top_level_dir)) |i| {
const relative_dir = parent[i + fs.FileSystem.instance.top_level_dir.len ..];
var out_dir = try allocator.alloc(u8, relative_dir.len + 2);
bun.copy(u8, out_dir[2..], relative_dir);
out_dir[0..2].* = ("." ++ std.fs.path.sep_str).*;
return out_dir;
}
return this.name;
}
}
pub const FrameworkRouterPair = struct {
framework: *options.Framework,
router: *options.RouteConfig,
loaded_routes: bool = false,
};
name: string = "",
source: logger.Source,
main_fields: MainFieldMap,
module_type: options.ModuleType,
version: string = "",
hash: u32 = 0xDEADBEEF,
scripts: ?*ScriptsMap = null,
arch: Architecture = Architecture.all,
os: OperatingSystem = OperatingSystem.all,
package_manager_package_id: Install.PackageID = Install.invalid_package_id,
dependencies: DependencyMap = .{},
side_effects: union(enum) {
unspecified: void,
false: void,
map: SideEffectsMap,
} = .{ .unspecified = {} },
// Present if the "browser" field is present. This field is intended to be
// used by bundlers and lets you redirect the paths of certain 3rd-party
// modules that don't work in the browser to other modules that shim that
// functionality. That way you don't have to rewrite the code for those 3rd-
// party modules. For example, you might remap the native "util" node module
// to something like https://www.npmjs.com/package/util so it works in the
// browser.
//
// This field contains a mapping of absolute paths to absolute paths. Mapping
// to an empty path indicates that the module is disabled. As far as I can
// tell, the official spec is an abandoned GitHub repo hosted by a user account:
// https://github.com/defunctzombie/package-browser-field-spec. The npm docs
// say almost nothing: https://docs.npmjs.com/files/package.json.
//
// Note that the non-package "browser" map has to be checked twice to match
// Webpack's behavior: once before resolution and once after resolution. It
// leads to some unintuitive failure cases that we must emulate around missing
// file extensions:
//
// * Given the mapping "./no-ext": "./no-ext-browser.js" the query "./no-ext"
// should match but the query "./no-ext.js" should NOT match.
//
// * Given the mapping "./ext.js": "./ext-browser.js" the query "./ext.js"
// should match and the query "./ext" should ALSO match.
//
browser_map: BrowserMap,
exports: ?ExportsMap = null,
imports: ?ExportsMap = null,
pub inline fn isAppPackage(this: *const PackageJSON) bool {
return this.hash == 0xDEADBEEF;
}
fn loadDefineDefaults(
env: *options.Env,
json: *const js_ast.E.Object,
allocator: std.mem.Allocator,
) !void {
var valid_count: usize = 0;
for (json.properties.slice()) |prop| {
if (prop.value.?.data != .e_string) continue;
valid_count += 1;
}
env.defaults.shrinkRetainingCapacity(0);
env.defaults.ensureTotalCapacity(allocator, valid_count) catch {};
for (json.properties.slice()) |prop| {
if (prop.value.?.data != .e_string) continue;
env.defaults.appendAssumeCapacity(.{
.key = prop.key.?.data.e_string.string(allocator) catch unreachable,
.value = prop.value.?.data.e_string.string(allocator) catch unreachable,
});
}
}
fn loadOverrides(
framework: *options.Framework,
json: *const js_ast.E.Object,
allocator: std.mem.Allocator,
) void {
var valid_count: usize = 0;
for (json.properties.slice()) |prop| {
if (prop.value.?.data != .e_string) continue;
valid_count += 1;
}
var buffer = allocator.alloc([]const u8, valid_count * 2) catch unreachable;
var keys = buffer[0..valid_count];
var values = buffer[valid_count..];
var i: usize = 0;
for (json.properties.slice()) |prop| {
if (prop.value.?.data != .e_string) continue;
keys[i] = prop.key.?.data.e_string.string(allocator) catch unreachable;
values[i] = prop.value.?.data.e_string.string(allocator) catch unreachable;
i += 1;
}
framework.override_modules = Api.StringMap{ .keys = keys, .values = values };
}
fn loadDefineExpression(
env: *options.Env,
json: *const js_ast.E.Object,
allocator: std.mem.Allocator,
) anyerror!void {
for (json.properties.slice()) |prop| {
switch (prop.key.?.data) {
.e_string => |e_str| {
const str = e_str.string(allocator) catch "";
if (strings.eqlComptime(str, "defaults")) {
switch (prop.value.?.data) {
.e_object => |obj| {
try loadDefineDefaults(env, obj, allocator);
},
else => {
env.defaults.shrinkRetainingCapacity(0);
},
}
} else if (strings.eqlComptime(str, ".env")) {
switch (prop.value.?.data) {
.e_string => |value_str| {
env.setBehaviorFromPrefix(value_str.string(allocator) catch "");
},
else => {
env.behavior = .disable;
env.prefix = "";
},
}
}
},
else => continue,
}
}
}
fn loadFrameworkExpression(
framework: *options.Framework,
json: js_ast.Expr,
allocator: std.mem.Allocator,
comptime read_define: bool,
) bool {
if (json.asProperty("client")) |client| {
if (client.expr.asString(allocator)) |str| {
if (str.len > 0) {
framework.client.path = str;
framework.client.kind = .client;
}
}
}
if (json.asProperty("fallback")) |client| {
if (client.expr.asString(allocator)) |str| {
if (str.len > 0) {
framework.fallback.path = str;
framework.fallback.kind = .fallback;
}
}
}
if (json.asProperty("css")) |css_prop| {
if (css_prop.expr.asString(allocator)) |str| {
if (strings.eqlComptime(str, "onimportcss")) {
framework.client_css_in_js = .facade_onimportcss;
} else {
framework.client_css_in_js = .facade;
}
}
}
if (json.asProperty("override")) |override| {
if (override.expr.data == .e_object) {
loadOverrides(framework, override.expr.data.e_object, allocator);
}
}
if (comptime read_define) {
if (json.asProperty("define")) |defines| {
var skip_fallback = false;
if (defines.expr.asProperty("client")) |client| {
if (client.expr.data == .e_object) {
const object = client.expr.data.e_object;
framework.client.env = options.Env.init(
allocator,
);
loadDefineExpression(&framework.client.env, object, allocator) catch {};
framework.fallback.env = framework.client.env;
skip_fallback = true;
}
}
if (!skip_fallback) {
if (defines.expr.asProperty("fallback")) |client| {
if (client.expr.data == .e_object) {
const object = client.expr.data.e_object;
framework.fallback.env = options.Env.init(
allocator,
);
loadDefineExpression(&framework.fallback.env, object, allocator) catch {};
}
}
}
if (defines.expr.asProperty("server")) |server| {
if (server.expr.data == .e_object) {
const object = server.expr.data.e_object;
framework.server.env = options.Env.init(
allocator,
);
loadDefineExpression(&framework.server.env, object, allocator) catch {};
}
}
}
}
if (json.asProperty("server")) |server| {
if (server.expr.asString(allocator)) |str| {
if (str.len > 0) {
framework.server.path = str;
framework.server.kind = .server;
}
}
}
return framework.client.isEnabled() or framework.server.isEnabled() or framework.fallback.isEnabled();
}
pub fn loadFrameworkWithPreference(
package_json: *const PackageJSON,
pair: *FrameworkRouterPair,
json: js_ast.Expr,
allocator: std.mem.Allocator,
comptime read_defines: bool,
comptime load_framework: LoadFramework,
) void {
const framework_object = json.asProperty("framework") orelse return;
if (framework_object.expr.asProperty("displayName")) |name| {
if (name.expr.asString(allocator)) |str| {
if (str.len > 0) {
pair.framework.display_name = str;
}
}
}
if (json.get("version")) |version| {
if (version.asString(allocator)) |str| {
if (str.len > 0) {
pair.framework.version = str;
}
}
}
if (framework_object.expr.asProperty("static")) |static_prop| {
if (static_prop.expr.asString(allocator)) |str| {
if (str.len > 0) {
pair.router.static_dir = str;
pair.router.static_dir_enabled = true;
}
}
}
if (framework_object.expr.asProperty("assetPrefix")) |asset_prefix| {
if (asset_prefix.expr.asString(allocator)) |_str| {
const str = std.mem.trimRight(u8, _str, " ");
if (str.len > 0) {
pair.router.asset_prefix_path = str;
}
}
}
if (!pair.router.routes_enabled) {
if (framework_object.expr.asProperty("router")) |router| {
if (router.expr.asProperty("dir")) |route_dir| {
switch (route_dir.expr.data) {
.e_string => |estr| {
const str = estr.string(allocator) catch unreachable;
if (str.len > 0) {
pair.router.dir = str;
pair.router.possible_dirs = &[_]string{};
pair.loaded_routes = true;
}
},
.e_array => |array| {
var count: usize = 0;
const items = array.items.slice();
for (items) |item| {
count += @intFromBool(item.data == .e_string and item.data.e_string.data.len > 0);
}
switch (count) {
0 => {},
1 => {
const str = items[0].data.e_string.string(allocator) catch unreachable;
if (str.len > 0) {
pair.router.dir = str;
pair.router.possible_dirs = &[_]string{};
pair.loaded_routes = true;
}
},
else => {
const list = allocator.alloc(string, count) catch unreachable;
var list_i: usize = 0;
for (items) |item| {
if (item.data == .e_string and item.data.e_string.data.len > 0) {
list[list_i] = item.data.e_string.string(allocator) catch unreachable;
list_i += 1;
}
}
pair.router.dir = list[0];
pair.router.possible_dirs = list;
pair.loaded_routes = true;
},
}
},
else => {},
}
}
if (router.expr.asProperty("extensions")) |extensions_expr| {
if (extensions_expr.expr.asArray()) |array_const| {
var array = array_const;
var valid_count: usize = 0;
while (array.next()) |expr| {
if (expr.data != .e_string) continue;
const e_str: *const js_ast.E.String = expr.data.e_string;
if (e_str.data.len == 0 or e_str.data[0] != '.') continue;
valid_count += 1;
}
if (valid_count > 0) {
var extensions = allocator.alloc(string, valid_count) catch unreachable;
array.index = 0;
var i: usize = 0;
// We don't need to allocate the strings because we keep the package.json source string in memory
while (array.next()) |expr| {
if (expr.data != .e_string) continue;
const e_str: *const js_ast.E.String = expr.data.e_string;
if (e_str.data.len == 0 or e_str.data[0] != '.') continue;
extensions[i] = e_str.data;
i += 1;
}
}
}
}
}
}
switch (comptime load_framework) {
.development => {
if (framework_object.expr.asProperty("development")) |env| {
if (loadFrameworkExpression(pair.framework, env.expr, allocator, read_defines)) {
pair.framework.package = package_json.nameForImport(allocator) catch unreachable;
pair.framework.development = true;
if (env.expr.asProperty("static")) |static_prop| {
if (static_prop.expr.asString(allocator)) |str| {
if (str.len > 0) {
pair.router.static_dir = str;
pair.router.static_dir_enabled = true;
}
}
}
return;
}
}
},
.production => {
if (framework_object.expr.asProperty("production")) |env| {
if (loadFrameworkExpression(pair.framework, env.expr, allocator, read_defines)) {
pair.framework.package = package_json.nameForImport(allocator) catch unreachable;
pair.framework.development = false;
if (env.expr.asProperty("static")) |static_prop| {
if (static_prop.expr.asString(allocator)) |str| {
if (str.len > 0) {
pair.router.static_dir = str;
pair.router.static_dir_enabled = true;
}
}
}
return;
}
}
},
else => unreachable,
}
if (loadFrameworkExpression(pair.framework, framework_object.expr, allocator, read_defines)) {
pair.framework.package = package_json.nameForImport(allocator) catch unreachable;
pair.framework.development = false;
}
}
pub fn parseMacrosJSON(
allocator: std.mem.Allocator,
macros: js_ast.Expr,
log: *logger.Log,
json_source: *const logger.Source,
) MacroMap {
var macro_map = MacroMap{};
if (macros.data != .e_object) return macro_map;
const properties = macros.data.e_object.properties.slice();
for (properties) |property| {
const key = property.key.?.asString(allocator) orelse continue;
if (!resolver.isPackagePath(key)) {
log.addRangeWarningFmt(
json_source,
json_source.rangeOfString(property.key.?.loc),
allocator,
"\"{s}\" is not a package path. \"macros\" remaps package paths to macros. Skipping.",
.{key},
) catch unreachable;
continue;
}
const value = property.value.?;
if (value.data != .e_object) {
log.addWarningFmt(
json_source,
value.loc,
allocator,
"Invalid macro remapping in \"{s}\": expected object where the keys are import names and the value is a string path to replace",
.{key},
) catch unreachable;
continue;
}
const remap_properties = value.data.e_object.properties.slice();
if (remap_properties.len == 0) continue;
var map = MacroImportReplacementMap.init(allocator);
map.ensureUnusedCapacity(remap_properties.len) catch unreachable;
for (remap_properties) |remap| {
const import_name = remap.key.?.asString(allocator) orelse continue;
const remap_value = remap.value.?;
if (remap_value.data != .e_string or remap_value.data.e_string.data.len == 0) {
log.addWarningFmt(
json_source,
remap_value.loc,
allocator,
"Invalid macro remapping for import \"{s}\": expected string to remap to. e.g. \"graphql\": \"bun-macro-relay\" ",
.{import_name},
) catch unreachable;
continue;
}
const remap_value_str = remap_value.data.e_string.data;
map.putAssumeCapacityNoClobber(import_name, remap_value_str);
}
if (map.count() > 0) {
macro_map.put(allocator, key, map) catch unreachable;
}
}
return macro_map;
}
pub fn parse(
r: *resolver.Resolver,
input_path: string,
dirname_fd: StoredFileDescriptorType,
package_id: ?Install.PackageID,
comptime include_scripts_: @Type(.EnumLiteral),
comptime include_dependencies: @Type(.EnumLiteral),
comptime generate_hash_: @Type(.EnumLiteral),
) ?PackageJSON {
const generate_hash = generate_hash_ == .generate_hash;
const include_scripts = include_scripts_ == .include_scripts;
// TODO: remove this extra copy
const parts = [_]string{ input_path, "package.json" };
const package_json_path_ = r.fs.abs(&parts);
const package_json_path = r.fs.dirname_store.append(@TypeOf(package_json_path_), package_json_path_) catch unreachable;
// DirInfo cache is reused globally
// So we cannot free these
const allocator = bun.fs_allocator;
const entry = r.caches.fs.readFileWithAllocator(
allocator,
r.fs,
package_json_path,
dirname_fd,
false,
null,
) catch |err| {
if (err != error.IsDir) {
r.log.addErrorFmt(null, logger.Loc.Empty, allocator, "Cannot read file \"{s}\": {s}", .{ r.prettyPath(fs.Path.init(input_path)), @errorName(err) }) catch unreachable;
}
return null;
};
defer {
if (entry.fd != 0) {
_ = bun.sys.close(entry.fd);
}
}
if (r.debug_logs) |*debug| {
debug.addNoteFmt("The file \"{s}\" exists", .{package_json_path});
}
const key_path = fs.Path.init(package_json_path);
var json_source = logger.Source.initPathString(key_path.text, entry.contents);
json_source.path.pretty = r.prettyPath(json_source.path);
const json: js_ast.Expr = (r.caches.json.parseJSON(r.log, json_source, allocator) catch |err| {
if (Environment.isDebug) {
Output.printError("{s}: JSON parse error: {s}", .{ package_json_path, @errorName(err) });
}
return null;
} orelse return null);
if (json.data != .e_object) {
// Invalid package.json in node_modules is noisy.
// Let's just ignore it.
allocator.free(entry.contents);
return null;
}
var package_json = PackageJSON{
.name = "",
.version = "",
.hash = 0xDEADBEEF,
.source = json_source,
.module_type = .unknown,
.browser_map = BrowserMap.init(allocator, false),
.main_fields = MainFieldMap.init(allocator, false),
};
// Note: we tried rewriting this to be fewer loops over all the properties (asProperty loops over each)
// The end result was: it's not faster! Sometimes, it's slower.
// It's hard to say why.
// Feels like a codegen issue.
// or that looping over every property doesn't really matter because most package.jsons are < 20 properties
if (json.asProperty("version")) |version_json| {
if (version_json.expr.asString(allocator)) |version_str| {
if (version_str.len > 0) {
package_json.version = allocator.dupe(u8, version_str) catch unreachable;
}
}
}
if (json.asProperty("name")) |version_json| {
if (version_json.expr.asString(allocator)) |version_str| {
if (version_str.len > 0) {
package_json.name = allocator.dupe(u8, version_str) catch unreachable;
}
}
}
// If we're coming from `bun run`
// We do not need to parse all this stuff.
if (comptime !include_scripts) {
if (json.asProperty("type")) |type_json| {
if (type_json.expr.asString(allocator)) |type_str| {
switch (options.ModuleType.List.get(type_str) orelse options.ModuleType.unknown) {
.cjs => {
package_json.module_type = .cjs;
},
.esm => {
package_json.module_type = .esm;
},
.unknown => {
r.log.addRangeWarningFmt(
&json_source,
json_source.rangeOfString(type_json.loc),
allocator,
"\"{s}\" is not a valid value for \"type\" field (must be either \"commonjs\" or \"module\")",
.{type_str},
) catch unreachable;
},
}
} else {
r.log.addWarning(&json_source, type_json.loc, "The value for \"type\" must be a string") catch unreachable;
}
}
// Read the "main" fields
for (r.opts.main_fields) |main| {
if (json.asProperty(main)) |main_json| {
const expr: js_ast.Expr = main_json.expr;
if ((expr.asString(allocator))) |str| {
if (str.len > 0) {
package_json.main_fields.put(main, str) catch unreachable;
}
}
}
}
// Read the "browser" property, but only when targeting the browser
if (r.opts.target.supportsBrowserField()) {
// We both want the ability to have the option of CJS vs. ESM and the
// option of having node vs. browser. The way to do this is to use the
// object literal form of the "browser" field like this:
//
// "main": "dist/index.node.cjs.js",
// "module": "dist/index.node.esm.js",
// "browser": {
// "./dist/index.node.cjs.js": "./dist/index.browser.cjs.js",
// "./dist/index.node.esm.js": "./dist/index.browser.esm.js"
// },
//
if (json.asProperty("browser")) |browser_prop| {
switch (browser_prop.expr.data) {
.e_object => |obj| {
// The value is an object
// Remap all files in the browser field
for (obj.properties.slice()) |*prop| {
var _key_str = (prop.key orelse continue).asString(allocator) orelse continue;
const value: js_ast.Expr = prop.value orelse continue;
// Normalize the path so we can compare against it without getting
// confused by "./". There is no distinction between package paths and
// relative paths for these values because some tools (i.e. Browserify)
// don't make such a distinction.
//
// This leads to weird things like a mapping for "./foo" matching an
// import of "foo", but that's actually not a bug. Or arguably it's a
// bug in Browserify but we have to replicate this bug because packages
// do this in the wild.
const key = allocator.dupe(u8, r.fs.normalize(_key_str)) catch unreachable;
switch (value.data) {
.e_string => |str| {
// If this is a string, it's a replacement package
package_json.browser_map.put(key, str.string(allocator) catch unreachable) catch unreachable;
},
.e_boolean => |boolean| {
if (!boolean.value) {
package_json.browser_map.put(key, "") catch unreachable;
}
},
else => {
r.log.addWarning(&json_source, value.loc, "Each \"browser\" mapping must be a string or boolean") catch unreachable;
},
}
}
},
else => {},
}
}
}
if (json.asProperty("exports")) |exports_prop| {
if (ExportsMap.parse(bun.default_allocator, &json_source, r.log, exports_prop.expr, exports_prop.loc)) |exports_map| {
package_json.exports = exports_map;
}
}
if (json.asProperty("imports")) |imports_prop| {
if (ExportsMap.parse(bun.default_allocator, &json_source, r.log, imports_prop.expr, imports_prop.loc)) |imports_map| {
package_json.imports = imports_map;
}
}
if (json.get("sideEffects")) |side_effects_field| outer: {
if (side_effects_field.asBool()) |boolean| {
if (!boolean)
package_json.side_effects = .{ .false = {} };
} else if (side_effects_field.asArray()) |array_| {
var array = array_;
// TODO: switch to only storing hashes
var map = SideEffectsMap{};
map.ensureTotalCapacity(allocator, array.array.items.len) catch unreachable;
while (array.next()) |item| {
if (item.asString(allocator)) |name| {
// TODO: support RegExp using JavaScriptCore <> C++ bindings
if (strings.containsChar(name, '*')) {
// https://sourcegraph.com/search?q=context:global+file:package.json+sideEffects%22:+%5B&patternType=standard&sm=1&groupBy=repo
// a lot of these seem to be css files which we don't care about for now anyway
// so we can just skip them in here
if (strings.eqlComptime(std.fs.path.extension(name), ".css"))
continue;
r.log.addWarning(
&json_source,
item.loc,
"wildcard sideEffects are not supported yet, which means this package will be deoptimized",
) catch unreachable;
map.deinit(allocator);
package_json.side_effects = .{ .unspecified = {} };
break :outer;
}
var joined = [_]string{
json_source.path.name.dirWithTrailingSlash(),
name,
};
_ = map.getOrPutAssumeCapacity(
bun.StringHashMapUnowned.Key.init(r.fs.join(&joined)),
);
}
}
package_json.side_effects = .{ .map = map };
}
}
}
if (comptime include_dependencies == .main or include_dependencies == .local) {
update_dependencies: {
if (package_id) |pkg| {
package_json.package_manager_package_id = pkg;
break :update_dependencies;
}
// // if there is a name & version, check if the lockfile has the package
if (package_json.name.len > 0 and package_json.version.len > 0) {
if (r.package_manager) |pm| {
const tag = Dependency.Version.Tag.infer(package_json.version);
if (tag == .npm) {
const sliced = Semver.SlicedString.init(package_json.version, package_json.version);
if (Dependency.parseWithTag(
allocator,
String.init(package_json.name, package_json.name),
String.Builder.stringHash(package_json.name),
package_json.version,
.npm,
&sliced,
r.log,
)) |dependency_version| {
if (dependency_version.value.npm.version.isExact()) {
if (pm.lockfile.resolve(package_json.name, dependency_version)) |resolved| {
package_json.package_manager_package_id = resolved;
if (resolved > 0) {
break :update_dependencies;
}
}
}
}
}
}
}
if (json.get("cpu")) |os_field| {
var first = true;
if (os_field.asArray()) |array_const| {
var array = array_const;
while (array.next()) |item| {
if (item.asString(bun.default_allocator)) |str| {
if (first) {
package_json.arch = Architecture.none;
first = false;
}
package_json.arch = package_json.arch.apply(str);
}
}
}
}
if (json.get("os")) |os_field| {
var first = true;
var tmp = os_field.asArray();
if (tmp) |*array| {
while (array.next()) |item| {
if (item.asString(bun.default_allocator)) |str| {
if (first) {
package_json.os = OperatingSystem.none;
first = false;
}
package_json.os = package_json.os.apply(str);
}
}
}
}
const DependencyGroup = Install.Lockfile.Package.DependencyGroup;
const features = .{
.dependencies = true,
.dev_dependencies = include_dependencies == .main,
.optional_dependencies = true,
.peer_dependencies = false,
};
const dependency_groups = comptime brk: {
var out_groups: [
@as(usize, @intFromBool(features.dependencies)) +
@as(usize, @intFromBool(features.dev_dependencies)) +
@as(usize, @intFromBool(features.optional_dependencies)) +
@as(usize, @intFromBool(features.peer_dependencies))
]DependencyGroup = undefined;
var out_group_i: usize = 0;
if (features.dependencies) {
out_groups[out_group_i] = DependencyGroup.dependencies;
out_group_i += 1;
}
if (features.dev_dependencies) {
out_groups[out_group_i] = DependencyGroup.dev;
out_group_i += 1;
}
if (features.optional_dependencies) {
out_groups[out_group_i] = DependencyGroup.optional;
out_group_i += 1;
}
if (features.peer_dependencies) {
out_groups[out_group_i] = DependencyGroup.peer;
out_group_i += 1;
}
break :brk out_groups;
};
var total_dependency_count: usize = 0;
inline for (dependency_groups) |group| {
if (json.get(group.field)) |group_json| {
if (group_json.data == .e_object) {
total_dependency_count += group_json.data.e_object.properties.len;
}
}
}
if (total_dependency_count > 0) {
package_json.dependencies.map = DependencyMap.HashMap{};
package_json.dependencies.source_buf = json_source.contents;
const ctx = String.ArrayHashContext{
.a_buf = json_source.contents,
.b_buf = json_source.contents,
};
package_json.dependencies.map.ensureTotalCapacityContext(
allocator,
total_dependency_count,
ctx,
) catch unreachable;
inline for (dependency_groups) |group| {
if (json.get(group.field)) |group_json| {
if (group_json.data == .e_object) {
var group_obj = group_json.data.e_object;
for (group_obj.properties.slice()) |*prop| {
const name_prop = prop.key orelse continue;
const name_str = name_prop.asString(allocator) orelse continue;
const name_hash = String.Builder.stringHash(name_str);
const name = String.init(name_str, name_str);
const version_value = prop.value orelse continue;
const version_str = version_value.asString(allocator) orelse continue;
const sliced_str = Semver.SlicedString.init(version_str, version_str);
if (Dependency.parse(
allocator,
name,
name_hash,
version_str,
&sliced_str,
r.log,
)) |dependency_version| {
const dependency = Dependency{
.name = name,
.version = dependency_version,
.name_hash = name_hash,
.behavior = group.behavior,
};
package_json.dependencies.map.putAssumeCapacityContext(
dependency.name,
dependency,
ctx,
);
}
}
}
}
}
}
}
}
// used by `bun run`
if (include_scripts) {
read_scripts: {
if (json.asProperty("scripts")) |scripts_prop| {
if (scripts_prop.expr.data == .e_object) {
const scripts_obj = scripts_prop.expr.data.e_object;
var count: usize = 0;
for (scripts_obj.properties.slice()) |prop| {
const key = prop.key.?.asString(allocator) orelse continue;
const value = prop.value.?.asString(allocator) orelse continue;
count += @as(usize, @intFromBool(key.len > 0 and value.len > 0));
}
if (count == 0) break :read_scripts;
var scripts = ScriptsMap.init(allocator);
scripts.ensureUnusedCapacity(count) catch break :read_scripts;
for (scripts_obj.properties.slice()) |prop| {
const key = prop.key.?.asString(allocator) orelse continue;
const value = prop.value.?.asString(allocator) orelse continue;
if (!(key.len > 0 and value.len > 0)) continue;
scripts.putAssumeCapacity(key, value);
}
package_json.scripts = allocator.create(ScriptsMap) catch unreachable;
package_json.scripts.?.* = scripts;
}
}
}
}
if (generate_hash) {
if (package_json.name.len > 0 and package_json.version.len > 0) {
package_json.generateHash();
}
}
return package_json;
}
pub fn hashModule(this: *const PackageJSON, module: string) u32 {
var hasher = bun.Wyhash.init(0);
hasher.update(std.mem.asBytes(&this.hash));
hasher.update(module);
return @as(u32, @truncate(hasher.final()));
}
};
pub const ExportsMap = struct {
root: Entry,
exports_range: logger.Range = logger.Range.None,
property_key_loc: logger.Loc,
pub fn parse(allocator: std.mem.Allocator, source: *const logger.Source, log: *logger.Log, json: js_ast.Expr, property_key_loc: logger.Loc) ?ExportsMap {
var visitor = Visitor{ .allocator = allocator, .source = source, .log = log };
const root = visitor.visit(json);
if (root.data == .null) {
return null;
}
return ExportsMap{
.root = root,
.exports_range = source.rangeOfString(json.loc),
.property_key_loc = property_key_loc,
};
}
pub const Visitor = struct {
allocator: std.mem.Allocator,
source: *const logger.Source,
log: *logger.Log,
pub fn visit(this: Visitor, expr: js_ast.Expr) Entry {
var first_token: logger.Range = logger.Range.None;
switch (expr.data) {
.e_null => {
return Entry{ .first_token = js_lexer.rangeOfIdentifier(this.source, expr.loc), .data = .{ .null = {} } };
},
.e_string => |str| {
return Entry{
.data = .{
.string = str.slice(this.allocator),
},
.first_token = this.source.rangeOfString(expr.loc),
};
},
.e_array => |e_array| {
var array = this.allocator.alloc(Entry, e_array.items.len) catch unreachable;
for (e_array.items.slice(), array) |item, *dest| {
dest.* = this.visit(item);
}
return Entry{
.data = .{
.array = array,
},
.first_token = logger.Range{ .loc = expr.loc, .len = 1 },
};
},
.e_object => |e_obj| {
var map_data = Entry.Data.Map.List{};
map_data.ensureTotalCapacity(this.allocator, e_obj.*.properties.len) catch unreachable;
map_data.len = e_obj.*.properties.len;
var expansion_keys = this.allocator.alloc(Entry.Data.Map.MapEntry, e_obj.*.properties.len) catch unreachable;
var expansion_key_i: usize = 0;
var map_data_slices = map_data.slice();
var map_data_keys = map_data_slices.items(.key);
var map_data_ranges = map_data_slices.items(.key_range);
var map_data_entries = map_data_slices.items(.value);
var is_conditional_sugar = false;
first_token.loc = expr.loc;
first_token.len = 1;
for (e_obj.properties.slice(), 0..) |prop, i| {
const key: string = prop.key.?.data.e_string.slice(this.allocator);
const key_range: logger.Range = this.source.rangeOfString(prop.key.?.loc);
// If exports is an Object with both a key starting with "." and a key
// not starting with ".", throw an Invalid Package Configuration error.
var cur_is_conditional_sugar = !strings.startsWithChar(key, '.');
if (i == 0) {
is_conditional_sugar = cur_is_conditional_sugar;
} else if (is_conditional_sugar != cur_is_conditional_sugar) {
const prev_key_range = map_data_ranges[i - 1];
const prev_key = map_data_keys[i - 1];
this.log.addRangeWarningFmtWithNote(
this.source,
key_range,
this.allocator,
"This object cannot contain keys that both start with \".\" and don't start with \".\"",
.{},
"The previous key \"{s}\" is incompatible with the current key \"{s}\"",
.{ prev_key, key },
prev_key_range,
) catch unreachable;
map_data.deinit(this.allocator);
this.allocator.free(expansion_keys);
return Entry{
.data = .{ .invalid = {} },
.first_token = first_token,
};
}
map_data_keys[i] = key;
map_data_ranges[i] = key_range;
map_data_entries[i] = this.visit(prop.value.?);
if (strings.endsWithComptime(key, "/") or strings.containsChar(key, '*')) {
expansion_keys[expansion_key_i] = Entry.Data.Map.MapEntry{
.value = map_data_entries[i],
.key = key,
.key_range = key_range,
};
expansion_key_i += 1;
}
}
// this leaks a lil, but it's fine.
expansion_keys = expansion_keys[0..expansion_key_i];
// Let expansionKeys be the list of keys of matchObj either ending in "/"
// or containing only a single "*", sorted by the sorting function
// PATTERN_KEY_COMPARE which orders in descending order of specificity.
const GlobLengthSorter: type = strings.NewGlobLengthSorter(Entry.Data.Map.MapEntry, "key");
var sorter = GlobLengthSorter{};
std.sort.block(Entry.Data.Map.MapEntry, expansion_keys, sorter, GlobLengthSorter.lessThan);
return Entry{
.data = .{
.map = Entry.Data.Map{
.list = map_data,
.expansion_keys = expansion_keys,
},
},
.first_token = first_token,
};
},
.e_boolean => {
first_token = js_lexer.rangeOfIdentifier(this.source, expr.loc);
},
.e_number => {
// TODO: range of number
first_token.loc = expr.loc;
first_token.len = 1;
},
else => {
first_token.loc = expr.loc;
},
}
this.log.addRangeWarning(this.source, first_token, "This value must be a string, an object, an array, or null") catch unreachable;
return Entry{
.data = .{ .invalid = {} },
.first_token = first_token,
};
}
};
pub const Entry = struct {
first_token: logger.Range,
data: Data,
pub const Data = union(Tag) {
invalid: void,
null: void,
boolean: bool,
string: string,
array: []const Entry,
map: Map,
pub const Tag = enum {
invalid,
null,
boolean,
string,
array,
map,
};
pub const Map = struct {
// This is not a std.ArrayHashMap because we also store the key_range which is a little weird
pub const List = std.MultiArrayList(MapEntry);
expansion_keys: []MapEntry,
list: List,
pub const MapEntry = struct {
key: string,
key_range: logger.Range,
value: Entry,
};
};
};
pub fn keysStartWithDot(this: *const Entry) bool {
return this.data == .map and this.data.map.list.len > 0 and strings.startsWithChar(this.data.map.list.items(.key)[0], '.');
}
pub fn valueForKey(this: *const Entry, key_: string) ?Entry {
switch (this.data) {
.map => {
var slice = this.data.map.list.slice();
const keys = slice.items(.key);
for (keys, 0..) |key, i| {
if (strings.eql(key, key_)) {
return slice.items(.value)[i];
}
}
return null;
},
else => {
return null;
},
}
}
};
};
pub const ESModule = struct {
pub const ConditionsMap = bun.StringArrayHashMap(void);
debug_logs: ?*resolver.DebugLogs = null,
conditions: ConditionsMap,
allocator: std.mem.Allocator,
module_type: *options.ModuleType = undefined,
pub const Resolution = struct {
status: Status = Status.Undefined,
path: string = "",
debug: Debug = Debug{},
pub const Debug = struct {
// This is the range of the token to use for error messages
token: logger.Range = logger.Range.None,
// If the status is "UndefinedNoConditionsMatch", this is the set of
// conditions that didn't match. This information is used for error messages.
unmatched_conditions: []string = &[_]string{},
};
};
pub const Status = enum {
Undefined,
UndefinedNoConditionsMatch, // A more friendly error message for when no conditions are matched
Null,
Exact,
ExactEndsWithStar,
Inexact, // This means we may need to try CommonJS-style extension suffixes
/// Module specifier is an invalid URL, package name or package subpath specifier.
InvalidModuleSpecifier,
/// package.json configuration is invalid or contains an invalid configuration.
InvalidPackageConfiguration,
/// Package exports or imports define a target module for the package that is an invalid type or string target.
InvalidPackageTarget,
/// Package exports do not define or permit a target subpath in the package for the given module.
PackagePathNotExported,
/// The package or module requested does not exist.
ModuleNotFound,
/// The user just needs to add the missing extension
ModuleNotFoundMissingExtension,
/// The resolved path corresponds to a directory, which is not a supported target for module imports.
UnsupportedDirectoryImport,
/// The user just needs to add the missing "/index.js" suffix
UnsupportedDirectoryImportMissingIndex,
/// When a package path is explicitly set to null, that means it's not exported.
PackagePathDisabled,
// The internal #import specifier was not found
PackageImportNotDefined,
PackageResolve,
pub inline fn isUndefined(this: Status) bool {
return switch (this) {
.Undefined, .UndefinedNoConditionsMatch => true,
else => false,
};
}
};
pub const Package = struct {
name: string,
version: string = "",
subpath: string,
pub const External = struct {
name: Semver.String = .{},
version: Semver.String = .{},
subpath: Semver.String = .{},
};
pub fn count(this: Package, builder: *Semver.String.Builder) void {
builder.count(this.name);
builder.count(this.version);
builder.count(this.subpath);
}
pub fn clone(this: Package, builder: *Semver.String.Builder) External {
return .{
.name = builder.appendUTF8WithoutPool(Semver.String, this.name, 0),
.version = builder.appendUTF8WithoutPool(Semver.String, this.version, 0),
.subpath = builder.appendUTF8WithoutPool(Semver.String, this.subpath, 0),
};
}
pub fn toExternal(this: Package, buffer: []const u8) External {
return .{
.name = Semver.String.init(buffer, this.name),
.version = Semver.String.init(buffer, this.version),
.subpath = Semver.String.init(buffer, this.subpath),
};
}
pub fn withAutoVersion(this: Package) Package {
if (this.version.len == 0) {
return .{
.name = this.name,
.subpath = this.subpath,
.version = "latest",
};
}
return this;
}
pub fn parseName(specifier: string) ?string {
var slash = strings.indexOfCharNeg(specifier, '/');
if (!strings.startsWithChar(specifier, '@')) {
slash = if (slash == -1) @as(i32, @intCast(specifier.len)) else slash;
return specifier[0..@as(usize, @intCast(slash))];
} else {
if (slash == -1) return null;
const slash2 = strings.indexOfChar(specifier[@as(usize, @intCast(slash)) + 1 ..], '/') orelse
specifier[@as(u32, @intCast(slash + 1))..].len;
return specifier[0 .. @as(usize, @intCast(slash + 1)) + slash2];
}
}
pub fn parseVersion(specifier_after_name: string) ?string {
if (strings.indexOfChar(specifier_after_name, '/')) |slash| {
// "foo@/bar" is not a valid specifier\
// "foo@/" is not a valid specifier
// "foo/@/bar" is not a valid specifier
// "foo@1/bar" is a valid specifier
// "foo@^123.2.3+ba-ab/bar" is a valid specifier
// ^^^^^^^^^^^^^^
// this is the version
const remainder = specifier_after_name[0..slash];
if (remainder.len > 0 and remainder[0] == '@') {
return remainder[1..];
}
return remainder;
}
return null;
}
pub fn parse(specifier: string, subpath_buf: []u8) ?Package {
if (specifier.len == 0) return null;
var package = Package{ .name = parseName(specifier) orelse return null, .subpath = "" };
if (strings.startsWith(package.name, ".") or strings.indexAnyComptime(package.name, "\\%") != null)
return null;
const offset: usize = if (package.name.len == 0 or package.name[0] != '@') 0 else 1;
if (strings.indexOfChar(specifier[offset..], '@')) |at| {
package.version = parseVersion(specifier[offset..][at..]) orelse "";
if (package.version.len == 0) {
package.version = specifier[offset..][at..];
if (package.version.len > 0 and package.version[0] == '@') {
package.version = package.version[1..];
}
}
package.name = specifier[0 .. at + offset];
parseSubpath(&package.subpath, specifier[@min(package.name.len + package.version.len + 1, specifier.len)..], subpath_buf);
} else {
parseSubpath(&package.subpath, specifier[package.name.len..], subpath_buf);
}
return package;
}
pub fn parseSubpath(subpath: *[]const u8, specifier: string, subpath_buf: []u8) void {
subpath_buf[0] = '.';
bun.copy(u8, subpath_buf[1..], specifier);
subpath.* = subpath_buf[0 .. specifier.len + 1];
}
};
const ReverseKind = enum { exact, pattern, prefix };
pub const ReverseResolution = struct {
subpath: string = "",
token: logger.Range = logger.Range.None,
};
const invalid_percent_chars = [_]string{
"%2f",
"%2F",
"%5c",
"%5C",
};
threadlocal var resolved_path_buf_percent: [bun.MAX_PATH_BYTES]u8 = undefined;
pub fn resolve(r: *const ESModule, package_url: string, subpath: string, exports: ExportsMap.Entry) Resolution {
return finalize(
r.resolveExports(package_url, subpath, exports),
);
}
pub fn resolveImports(r: *const ESModule, specifier: string, imports: ExportsMap.Entry) Resolution {
if (imports.data != .map) {
return .{
.status = .InvalidPackageConfiguration,
.debug = .{
.token = logger.Range.None,
},
};
}
const result = r.resolveImportsExports(
specifier,
imports,
true,
"/",
);
switch (result.status) {
.Undefined, .Null => {
return .{ .status = .PackageImportNotDefined, .debug = .{ .token = result.debug.token } };
},
else => {
return finalize(result);
},
}
}
pub fn finalize(result_: Resolution) Resolution {
var result = result_;
if (result.status != .Exact and result.status != .ExactEndsWithStar and result.status != .Inexact) {
return result;
}
// If resolved contains any percent encodings of "/" or "\" ("%2f" and "%5C"
// respectively), then throw an Invalid Module Specifier error.
const PercentEncoding = @import("../url.zig").PercentEncoding;
var fbs = std.io.fixedBufferStream(&resolved_path_buf_percent);
var writer = fbs.writer();
const len = PercentEncoding.decode(@TypeOf(&writer), &writer, result.path) catch return Resolution{
.status = .InvalidModuleSpecifier,
.path = result.path,
.debug = result.debug,
};
const resolved_path = resolved_path_buf_percent[0..len];
var found: string = "";
if (strings.contains(resolved_path, invalid_percent_chars[0])) {
found = invalid_percent_chars[0];
} else if (strings.contains(resolved_path, invalid_percent_chars[1])) {
found = invalid_percent_chars[1];
} else if (strings.contains(resolved_path, invalid_percent_chars[2])) {
found = invalid_percent_chars[2];
} else if (strings.contains(resolved_path, invalid_percent_chars[3])) {
found = invalid_percent_chars[3];
}
if (found.len != 0) {
return Resolution{ .status = .InvalidModuleSpecifier, .path = result.path, .debug = result.debug };
}
// If resolved is a directory, throw an Unsupported Directory Import error.
if (strings.endsWithAnyComptime(resolved_path, "/\\")) {
return Resolution{ .status = .UnsupportedDirectoryImport, .path = result.path, .debug = result.debug };
}
result.path = resolved_path;
return result;
}
fn resolveExports(
r: *const ESModule,
package_url: string,
subpath: string,
exports: ExportsMap.Entry,
) Resolution {
if (exports.data == .invalid) {
if (r.debug_logs) |logs| {
logs.addNote("Invalid package configuration");
}
return Resolution{ .status = .InvalidPackageConfiguration, .debug = .{ .token = exports.first_token } };
}
if (strings.eqlComptime(subpath, ".")) {
var main_export = ExportsMap.Entry{ .data = .{ .null = {} }, .first_token = logger.Range.None };
if (switch (exports.data) {
.string,
.array,
=> true,
.map => !exports.keysStartWithDot(),
else => false,
}) {
main_export = exports;
} else if (exports.data == .map) {
if (exports.valueForKey(".")) |value| {
main_export = value;
}
}
if (main_export.data != .null) {
const result = r.resolveTarget(package_url, main_export, "", false, false);
if (result.status != .Null and result.status != .Undefined) {
return result;
}
}
} else if (exports.data == .map and exports.keysStartWithDot()) {
const result = r.resolveImportsExports(subpath, exports, false, package_url);
if (result.status != .Null and result.status != .Undefined) {
return result;
}
if (result.status == .Null) {
return Resolution{ .status = .PackagePathDisabled, .debug = .{ .token = exports.first_token } };
}
}
if (r.debug_logs) |logs| {
logs.addNoteFmt("The path \"{s}\" was not exported", .{subpath});
}
return Resolution{ .status = .PackagePathNotExported, .debug = .{ .token = exports.first_token } };
}
fn resolveImportsExports(
r: *const ESModule,
match_key: string,
match_obj: ExportsMap.Entry,
is_imports: bool,
package_url: string,
) Resolution {
if (r.debug_logs) |logs| {
logs.addNoteFmt("Checking object path map for \"{s}\"", .{match_key});
}
// If matchKey is a key of matchObj and does not end in "/" or contain "*", then
if (!strings.endsWithChar(match_key, '/') and !strings.containsChar(match_key, '*')) {
if (match_obj.valueForKey(match_key)) |target| {
if (r.debug_logs) |log| {
log.addNoteFmt("Found \"{s}\"", .{match_key});
}
return r.resolveTarget(package_url, target, "", is_imports, false);
}
}
if (match_obj.data == .map) {
const expansion_keys = match_obj.data.map.expansion_keys;
for (expansion_keys) |expansion| {
// If expansionKey contains "*", set patternBase to the substring of
// expansionKey up to but excluding the first "*" character
if (strings.indexOfChar(expansion.key, '*')) |star| {
const pattern_base = expansion.key[0..star];
// If patternBase is not null and matchKey starts with but is not equal
// to patternBase, then
if (strings.startsWith(match_key, pattern_base)) {
// Let patternTrailer be the substring of expansionKey from the index
// after the first "*" character.
const pattern_trailer = expansion.key[star + 1 ..];
// If patternTrailer has zero length, or if matchKey ends with
// patternTrailer and the length of matchKey is greater than or
// equal to the length of expansionKey, then
if (pattern_trailer.len == 0 or (strings.endsWith(match_key, pattern_trailer) and match_key.len >= expansion.key.len)) {
const target = expansion.value;
const subpath = match_key[pattern_base.len .. match_key.len - pattern_trailer.len];
if (r.debug_logs) |log| {
log.addNoteFmt("The key \"{s}\" matched with \"{s}\" left over", .{ expansion.key, subpath });
}
return r.resolveTarget(package_url, target, subpath, is_imports, true);
}
}
} else {
// Otherwise if patternBase is null and matchKey starts with
// expansionKey, then
if (strings.startsWith(match_key, expansion.key)) {
const target = expansion.value;
const subpath = match_key[expansion.key.len..];
if (r.debug_logs) |log| {
log.addNoteFmt("The key \"{s}\" matched with \"{s}\" left over", .{ expansion.key, subpath });
}
var result = r.resolveTarget(package_url, target, subpath, is_imports, false);
if (result.status == .Exact or result.status == .ExactEndsWithStar) {
// Return the object { resolved, exact: false }.
result.status = .Inexact;
}
return result;
}
}
if (r.debug_logs) |log| {
log.addNoteFmt("The key \"{s}\" did not match", .{expansion.key});
}
}
}
if (r.debug_logs) |log| {
log.addNoteFmt("No keys matched \"{s}\"", .{match_key});
}
return Resolution{
.status = .Null,
.debug = .{ .token = match_obj.first_token },
};
}
threadlocal var resolve_target_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
threadlocal var resolve_target_buf2: [bun.MAX_PATH_BYTES]u8 = undefined;
fn resolveTarget(
r: *const ESModule,
package_url: string,
target: ExportsMap.Entry,
subpath: string,
internal: bool,
comptime pattern: bool,
) Resolution {
switch (target.data) {
.string => |str| {
if (r.debug_logs) |log| {
log.addNoteFmt("Checking path \"{s}\" against target \"{s}\"", .{ subpath, str });
log.increaseIndent();
}
defer {
if (r.debug_logs) |log| {
log.decreaseIndent();
}
}
// If pattern is false, subpath has non-zero length and target
// does not end with "/", throw an Invalid Module Specifier error.
if (comptime !pattern) {
if (subpath.len > 0 and !strings.endsWithChar(str, '/')) {
if (r.debug_logs) |log| {
log.addNoteFmt("The target \"{s}\" is invalid because it doesn't end with a \"/\"", .{str});
}
return Resolution{ .path = str, .status = .InvalidModuleSpecifier, .debug = .{ .token = target.first_token } };
}
}
// If target does not start with "./", then...
if (!strings.startsWith(str, "./")) {
if (r.debug_logs) |log| {
log.addNoteFmt("The target \"{s}\" is invalid because it doesn't start with a \"./\"", .{str});
}
if (internal and !strings.hasPrefixComptime(str, "../") and !strings.hasPrefix(str, "/")) {
if (comptime pattern) {
// Return the URL resolution of resolvedTarget with every instance of "*" replaced with subpath.
const len = std.mem.replacementSize(u8, str, "*", subpath);
_ = std.mem.replace(u8, str, "*", subpath, &resolve_target_buf2);
const result = resolve_target_buf2[0..len];
if (r.debug_logs) |log| {
log.addNoteFmt("Subsituted \"{s}\" for \"*\" in \".{s}\" to get \".{s}\" ", .{ subpath, str, result });
}
return Resolution{ .path = result, .status = .PackageResolve, .debug = .{ .token = target.first_token } };
} else {
var parts2 = [_]string{ str, subpath };
const result = resolve_path.joinStringBuf(&resolve_target_buf2, parts2, .auto);
if (r.debug_logs) |log| {
log.addNoteFmt("Resolved \".{s}\" to \".{s}\"", .{ str, result });
}
return Resolution{ .path = result, .status = .PackageResolve, .debug = .{ .token = target.first_token } };
}
}
return Resolution{ .path = str, .status = .InvalidPackageTarget, .debug = .{ .token = target.first_token } };
}
// If target split on "/" or "\" contains any ".", ".." or "node_modules"
// segments after the first segment, throw an Invalid Package Target error.
if (findInvalidSegment(str)) |invalid| {
if (r.debug_logs) |log| {
log.addNoteFmt("The target \"{s}\" is invalid because it contains an invalid segment \"{s}\"", .{ str, invalid });
}
return Resolution{ .path = str, .status = .InvalidPackageTarget, .debug = .{ .token = target.first_token } };
}
// Let resolvedTarget be the URL resolution of the concatenation of packageURL and target.
var parts = [_]string{ package_url, str };
const resolved_target = resolve_path.joinStringBuf(&resolve_target_buf, parts, .auto);
// If target split on "/" or "\" contains any ".", ".." or "node_modules"
// segments after the first segment, throw an Invalid Package Target error.
if (findInvalidSegment(resolved_target)) |invalid| {
if (r.debug_logs) |log| {
log.addNoteFmt("The target \"{s}\" is invalid because it contains an invalid segment \"{s}\"", .{ str, invalid });
}
return Resolution{ .path = str, .status = .InvalidModuleSpecifier, .debug = .{ .token = target.first_token } };
}
if (comptime pattern) {
// Return the URL resolution of resolvedTarget with every instance of "*" replaced with subpath.
const len = std.mem.replacementSize(u8, resolved_target, "*", subpath);
_ = std.mem.replace(u8, resolved_target, "*", subpath, &resolve_target_buf2);
const result = resolve_target_buf2[0..len];
if (r.debug_logs) |log| {
log.addNoteFmt("Substituted \"{s}\" for \"*\" in \".{s}\" to get \".{s}\" ", .{ subpath, resolved_target, result });
}
const status: Status = if (strings.endsWithChar(result, '*') and strings.indexOfChar(result, '*').? == result.len - 1)
.ExactEndsWithStar
else
.Exact;
return Resolution{ .path = result, .status = status, .debug = .{ .token = target.first_token } };
} else {
var parts2 = [_]string{ package_url, str, subpath };
const result = resolve_path.joinStringBuf(&resolve_target_buf2, parts2, .auto);
if (r.debug_logs) |log| {
log.addNoteFmt("Substituted \"{s}\" for \"*\" in \".{s}\" to get \".{s}\" ", .{ subpath, resolved_target, result });
}
return Resolution{ .path = result, .status = .Exact, .debug = .{ .token = target.first_token } };
}
},
.map => |object| {
var did_find_map_entry = false;
var last_map_entry_i: usize = 0;
const slice = object.list.slice();
const keys = slice.items(.key);
for (keys, 0..) |key, i| {
if (r.conditions.contains(key)) {
if (r.debug_logs) |log| {
log.addNoteFmt("The key \"{s}\" matched", .{key});
}
var prev_module_type = r.module_type.*;
var result = r.resolveTarget(package_url, slice.items(.value)[i], subpath, internal, pattern);
if (result.status.isUndefined()) {
did_find_map_entry = true;
last_map_entry_i = i;
r.module_type.* = prev_module_type;
continue;
}
if (strings.eqlComptime(key, "import")) {
r.module_type.* = .esm;
}
if (strings.eqlComptime(key, "require")) {
r.module_type.* = .cjs;
}
return result;
}
if (r.debug_logs) |log| {
log.addNoteFmt("The key \"{s}\" did not match", .{key});
}
}
if (r.debug_logs) |log| {
log.addNoteFmt("No keys matched", .{});
}
var return_target = target;
// ALGORITHM DEVIATION: Provide a friendly error message if no conditions matched
if (keys.len > 0 and !target.keysStartWithDot()) {
var last_map_entry = ExportsMap.Entry.Data.Map.MapEntry{
.key = keys[last_map_entry_i],
.value = slice.items(.value)[last_map_entry_i],
// key_range is unused, so we don't need to pull up the array for it.
.key_range = logger.Range.None,
};
if (did_find_map_entry and
last_map_entry.value.data == .map and
last_map_entry.value.data.map.list.len > 0 and
!last_map_entry.value.keysStartWithDot())
{
// If a top-level condition did match but no sub-condition matched,
// complain about the sub-condition instead of the top-level condition.
// This leads to a less confusing error message. For example:
//
// "exports": {
// "node": {
// "require": "./dist/bwip-js-node.js"
// }
// },
//
// We want the warning to say this:
//
// note: None of the conditions provided ("require") match any of the
// currently active conditions ("default", "import", "node")
// 14 | "node": {
// | ^
//
// We don't want the warning to say this:
//
// note: None of the conditions provided ("browser", "electron", "node")
// match any of the currently active conditions ("default", "import", "node")
// 7 | "exports": {
// | ^
//
// More information: https://github.com/evanw/esbuild/issues/1484
return_target = last_map_entry.value;
}
return Resolution{
.path = "",
.status = .UndefinedNoConditionsMatch,
.debug = .{
.token = target.first_token,
.unmatched_conditions = return_target.data.map.list.items(.key),
},
};
}
return Resolution{
.path = "",
.status = .UndefinedNoConditionsMatch,
.debug = .{ .token = target.first_token },
};
},
.array => |array| {
if (array.len == 0) {
if (r.debug_logs) |log| {
log.addNoteFmt("The path \"{s}\" is an empty array", .{subpath});
}
return Resolution{ .path = "", .status = .Null, .debug = .{ .token = target.first_token } };
}
var last_exception = Status.Undefined;
var last_debug = Resolution.Debug{ .token = target.first_token };
for (array) |targetValue| {
// Let resolved be the result, continuing the loop on any Invalid Package Target error.
var prev_module_type = r.module_type.*;
const result = r.resolveTarget(package_url, targetValue, subpath, internal, pattern);
if (result.status == .InvalidPackageTarget or result.status == .Null) {
last_debug = result.debug;
last_exception = result.status;
}
if (result.status.isUndefined()) {
r.module_type.* = prev_module_type;
continue;
}
return result;
}
return Resolution{ .path = "", .status = last_exception, .debug = last_debug };
},
.null => {
if (r.debug_logs) |log| {
log.addNoteFmt("The path \"{s}\" is null", .{subpath});
}
return Resolution{ .path = "", .status = .Null, .debug = .{ .token = target.first_token } };
},
else => {},
}
if (r.debug_logs) |logs| {
logs.addNoteFmt("Invalid package target for path \"{s}\"", .{subpath});
}
return Resolution{ .status = .InvalidPackageTarget, .debug = .{ .token = target.first_token } };
}
fn resolveExportsReverse(
r: *const ESModule,
query: string,
root: ExportsMap.Entry,
) ?ReverseResolution {
if (root.data == .map and root.keysStartWithDot()) {
if (r.resolveImportsExportsReverse(query, root)) |res| {
return res;
}
}
return null;
}
fn resolveImportsExportsReverse(
r: *const ESModule,
query: string,
match_obj: ExportsMap.Entry,
) ?ReverseResolution {
if (match_obj.data != .map) return null;
const map = match_obj.data.map;
if (!strings.endsWithChar(query, "*")) {
var slices = map.list.slice();
var keys = slices.items(.key);
var values = slices.items(.value);
for (keys, 0..) |key, i| {
if (r.resolveTargetReverse(query, key, values[i], .exact)) |result| {
return result;
}
}
}
for (map.expansion_keys) |expansion| {
if (strings.endsWithChar(expansion.key, '*')) {
if (r.resolveTargetReverse(query, expansion.key, expansion.value, .pattern)) |result| {
return result;
}
}
if (r.resolveTargetReverse(query, expansion.key, expansion.value, .reverse)) |result| {
return result;
}
}
}
threadlocal var resolve_target_reverse_prefix_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
threadlocal var resolve_target_reverse_prefix_buf2: [bun.MAX_PATH_BYTES]u8 = undefined;
fn resolveTargetReverse(
r: *const ESModule,
query: string,
key: string,
target: ExportsMap.Entry,
comptime kind: ReverseKind,
) ?ReverseResolution {
switch (target.data) {
.string => |str| {
switch (comptime kind) {
.exact => {
if (strings.eql(query, str)) {
return ReverseResolution{ .subpath = str, .token = target.first_token };
}
},
.prefix => {
if (strings.startsWith(query, str)) {
return ReverseResolution{
.subpath = std.fmt.bufPrint(&resolve_target_reverse_prefix_buf, "{s}{s}", .{ key, query[str.len..] }) catch unreachable,
.token = target.first_token,
};
}
},
.pattern => {
const key_without_trailing_star = std.mem.trimRight(u8, key, "*");
const star = strings.indexOfChar(str, '*') orelse {
// Handle the case of no "*"
if (strings.eql(query, str)) {
return ReverseResolution{ .subpath = key_without_trailing_star, .token = target.first_token };
}
return null;
};
// Only support tracing through a single "*"
const prefix = str[0..star];
const suffix = str[star + 1 ..];
if (strings.startsWith(query, prefix) and !strings.containsChar(suffix, '*')) {
const after_prefix = query[prefix.len..];
if (strings.endsWith(after_prefix, suffix)) {
const star_data = after_prefix[0 .. after_prefix.len - suffix.len];
return ReverseResolution{
.subpath = std.fmt.bufPrint(
&resolve_target_reverse_prefix_buf2,
"{s}{s}",
.{
key_without_trailing_star,
star_data,
},
) catch unreachable,
.token = target.first_token,
};
}
}
},
}
},
.map => |map| {
const slice = map.list.slice();
const keys = slice.items(.key);
for (keys, 0..) |map_key, i| {
if (r.conditions.contains(map_key)) {
if (r.resolveTargetReverse(query, key, slice.items(.value)[i], kind)) |result| {
if (strings.eqlComptime(map_key, "import")) {
r.module_type.* = .esm;
} else if (strings.eqlComptime(map_key, "require")) {
r.module_type.* = .cjs;
}
return result;
}
}
}
},
.array => |array| {
for (array) |target_value| {
if (r.resolveTargetReverse(query, key, target_value, kind)) |result| {
return result;
}
}
},
else => {},
}
return null;
}
};
fn findInvalidSegment(path_: string) ?string {
var slash = strings.indexAnyComptime(path_, "/\\") orelse return "";
var path = path_[slash + 1 ..];
while (path.len > 0) {
var segment = path;
if (strings.indexAnyComptime(path, "/\\")) |new_slash| {
segment = path[0..new_slash];
path = path[new_slash + 1 ..];
} else {
path = "";
}
switch (segment.len) {
1 => {
if (strings.eqlComptimeIgnoreLen(segment, ".")) return segment;
},
2 => {
if (strings.eqlComptimeIgnoreLen(segment, "..")) return segment;
},
"node_modules".len => {
if (strings.eqlComptimeIgnoreLen(segment, "node_modules")) return segment;
},
else => {},
}
}
return null;
}
|