@shell-shock/core 0.14.6 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -945,6 +945,998 @@ function SpawnFunctionDeclaration() {
945
945
  })
946
946
  ];
947
947
  }
948
+ /**
949
+ * Generates the `resolveModule` function declaration, which is a utility for resolving file paths across different platforms.
950
+ */
951
+ function ResolveModuleFunctionDeclaration() {
952
+ const context = (0, _powerlines_plugin_alloy_core_contexts_context.usePowerlines)();
953
+ return [
954
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
955
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceDeclaration, {
956
+ name: "ResolveModuleOptions",
957
+ doc: "Options for the `resolve` handler function.",
958
+ get children() {
959
+ return [
960
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
961
+ name: "parentURL",
962
+ optional: true,
963
+ type: "string",
964
+ doc: "The parent URL to use for resolving paths."
965
+ }),
966
+ (0, _alloy_js_core_jsx_runtime.createIntrinsic)("hbr", {}),
967
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
968
+ name: "conditions",
969
+ optional: true,
970
+ type: "string[]",
971
+ doc: "The conditions to use for resolving paths."
972
+ }),
973
+ (0, _alloy_js_core_jsx_runtime.createIntrinsic)("hbr", {})
974
+ ];
975
+ }
976
+ }),
977
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
978
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceDeclaration, {
979
+ name: "PackageConfig",
980
+ doc: "Parsed package.json configuration for module resolution.",
981
+ get children() {
982
+ return [
983
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
984
+ name: "pjsonPath",
985
+ type: "string",
986
+ doc: "The path to the package.json file."
987
+ }),
988
+ (0, _alloy_js_core_jsx_runtime.createIntrinsic)("hbr", {}),
989
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
990
+ name: "exists",
991
+ type: "boolean",
992
+ doc: "Whether the package.json file exists."
993
+ }),
994
+ (0, _alloy_js_core_jsx_runtime.createIntrinsic)("hbr", {}),
995
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
996
+ name: "main",
997
+ optional: true,
998
+ type: "string",
999
+ doc: "The main entry point of the package."
1000
+ }),
1001
+ (0, _alloy_js_core_jsx_runtime.createIntrinsic)("hbr", {}),
1002
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
1003
+ name: "name",
1004
+ optional: true,
1005
+ type: "string",
1006
+ doc: "The name of the package."
1007
+ }),
1008
+ (0, _alloy_js_core_jsx_runtime.createIntrinsic)("hbr", {}),
1009
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
1010
+ name: "type",
1011
+ optional: true,
1012
+ type: `"commonjs" | "module" | "none"`,
1013
+ doc: "The module type of the package."
1014
+ }),
1015
+ (0, _alloy_js_core_jsx_runtime.createIntrinsic)("hbr", {}),
1016
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
1017
+ name: "exports",
1018
+ optional: true,
1019
+ type: "Record<string, unknown>",
1020
+ doc: "The exports map of the package."
1021
+ }),
1022
+ (0, _alloy_js_core_jsx_runtime.createIntrinsic)("hbr", {}),
1023
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.InterfaceMember, {
1024
+ name: "imports",
1025
+ optional: true,
1026
+ type: "Record<string, unknown>",
1027
+ doc: "The imports map of the package."
1028
+ })
1029
+ ];
1030
+ }
1031
+ }),
1032
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1033
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.VarDeclaration, {
1034
+ "const": true,
1035
+ name: "DEFAULT_CONDITIONS_SET",
1036
+ initializer: _alloy_js_core.code`new Set(["node", "import"]);`
1037
+ }),
1038
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1039
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.VarDeclaration, {
1040
+ "const": true,
1041
+ name: "DEFAULT_EXTENSIONS",
1042
+ initializer: _alloy_js_core.code`[".mjs", ".cjs", ".js", ".json", ".node"];`
1043
+ }),
1044
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1045
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.VarDeclaration, {
1046
+ "const": true,
1047
+ name: "invalidPackageNameRegEx",
1048
+ initializer: _alloy_js_core.code`/^\\.|\\/|%/;`
1049
+ }),
1050
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1051
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1052
+ name: "readPackageConfig",
1053
+ parameters: [{
1054
+ name: "jsonPath",
1055
+ type: "string"
1056
+ }],
1057
+ returnType: "PackageConfig",
1058
+ children: _alloy_js_core.code`const result: PackageConfig = {
1059
+ pjsonPath: jsonPath,
1060
+ exists: false,
1061
+ main: undefined,
1062
+ name: undefined,
1063
+ type: "none",
1064
+ exports: undefined,
1065
+ imports: undefined
1066
+ };
1067
+
1068
+ let content: string;
1069
+ try {
1070
+ content = readFileSync(jsonPath, "utf8");
1071
+ } catch (err: any) {
1072
+ if (err?.code === "ENOENT") {
1073
+ return result;
1074
+ }
1075
+ throw err;
1076
+ }
1077
+
1078
+ let parsed: Record<string, unknown>;
1079
+ try {
1080
+ parsed = JSON.parse(content);
1081
+ } catch {
1082
+ return result;
1083
+ }
1084
+
1085
+ result.exists = true;
1086
+
1087
+ if (typeof parsed.name === "string") {
1088
+ result.name = parsed.name;
1089
+ }
1090
+ if (typeof parsed.main === "string") {
1091
+ result.main = parsed.main;
1092
+ }
1093
+ if (parsed.exports !== undefined && parsed.exports !== null) {
1094
+ result.exports = parsed.exports as Record<string, unknown>;
1095
+ }
1096
+ if (parsed.imports !== undefined && parsed.imports !== null) {
1097
+ result.imports = parsed.imports as Record<string, unknown>;
1098
+ }
1099
+ if (parsed.type === "commonjs" || parsed.type === "module") {
1100
+ result.type = parsed.type;
1101
+ }
1102
+
1103
+ return result;`
1104
+ }),
1105
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1106
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1107
+ name: "getPackageScopeConfig",
1108
+ parameters: [{
1109
+ name: "resolved",
1110
+ type: "URL | string"
1111
+ }],
1112
+ returnType: "PackageConfig",
1113
+ children: _alloy_js_core.code`let packageJSONUrl = new URL("package.json", resolved);
1114
+
1115
+ while (true) {
1116
+ const packageJSONPath = packageJSONUrl.pathname;
1117
+ if (packageJSONPath.endsWith("node_modules/package.json")) {
1118
+ break;
1119
+ }
1120
+
1121
+ const packageConfig = readPackageConfig(fileURLToPath(packageJSONUrl));
1122
+ if (packageConfig.exists) {
1123
+ return packageConfig;
1124
+ }
1125
+
1126
+ const lastPackageJSONUrl = packageJSONUrl;
1127
+ packageJSONUrl = new URL("../package.json", packageJSONUrl);
1128
+
1129
+ if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) {
1130
+ break;
1131
+ }
1132
+ }
1133
+
1134
+ return {
1135
+ pjsonPath: fileURLToPath(packageJSONUrl),
1136
+ exists: false,
1137
+ type: "none"
1138
+ };`
1139
+ }),
1140
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1141
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1142
+ name: "isConditionalExportsMainSugar",
1143
+ parameters: [{
1144
+ name: "exports",
1145
+ type: "unknown"
1146
+ }],
1147
+ returnType: "boolean",
1148
+ children: _alloy_js_core.code`if (typeof exports === "string" || Array.isArray(exports)) {
1149
+ return true;
1150
+ }
1151
+ if (typeof exports !== "object" || exports === null) {
1152
+ return false;
1153
+ }
1154
+
1155
+ const keys = Object.getOwnPropertyNames(exports);
1156
+ let isConditionalSugar = false;
1157
+
1158
+ for (let i = 0; i < keys.length; i++) {
1159
+ const key = keys[i]!;
1160
+ const currentIsConditionalSugar = key === "" || key[0] !== ".";
1161
+ if (i === 0) {
1162
+ isConditionalSugar = currentIsConditionalSugar;
1163
+ } else if (isConditionalSugar !== currentIsConditionalSugar) {
1164
+ throw new Error(
1165
+ '"exports" cannot contain some keys starting with \\'.\\' and some not.'
1166
+ );
1167
+ }
1168
+ }
1169
+
1170
+ return isConditionalSugar;`
1171
+ }),
1172
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1173
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1174
+ name: "resolvePackageTarget",
1175
+ parameters: [
1176
+ {
1177
+ name: "packageJsonUrl",
1178
+ type: "URL"
1179
+ },
1180
+ {
1181
+ name: "target",
1182
+ type: "unknown"
1183
+ },
1184
+ {
1185
+ name: "subpath",
1186
+ type: "string"
1187
+ },
1188
+ {
1189
+ name: "packageSubpath",
1190
+ type: "string"
1191
+ },
1192
+ {
1193
+ name: "base",
1194
+ type: "URL"
1195
+ },
1196
+ {
1197
+ name: "pattern",
1198
+ type: "boolean"
1199
+ },
1200
+ {
1201
+ name: "internal",
1202
+ type: "boolean"
1203
+ },
1204
+ {
1205
+ name: "conditions",
1206
+ type: "Set<string>"
1207
+ }
1208
+ ],
1209
+ returnType: "URL | null | undefined",
1210
+ children: _alloy_js_core.code`if (typeof target === "string") {
1211
+ if (!target.startsWith("./")) {
1212
+ if (internal && !target.startsWith("../") && !target.startsWith("/")) {
1213
+ let isURL = false;
1214
+ try {
1215
+ new URL(target);
1216
+ isURL = true;
1217
+ } catch {}
1218
+
1219
+ if (!isURL) {
1220
+ const exportTarget = pattern
1221
+ ? target.replace(/\\*/g, () => subpath)
1222
+ : target + subpath;
1223
+ return packageResolve(exportTarget, packageJsonUrl, conditions);
1224
+ }
1225
+ }
1226
+ return null;
1227
+ }
1228
+
1229
+ const resolved = new URL(target, packageJsonUrl);
1230
+ const resolvedPath = resolved.pathname;
1231
+ const packagePath = new URL(".", packageJsonUrl).pathname;
1232
+
1233
+ if (!resolvedPath.startsWith(packagePath)) {
1234
+ return null;
1235
+ }
1236
+
1237
+ if (subpath === "") {
1238
+ return resolved;
1239
+ }
1240
+
1241
+ if (pattern) {
1242
+ return new URL(resolved.href.replace(/\\*/g, () => subpath));
1243
+ }
1244
+
1245
+ return new URL(subpath, resolved);
1246
+ }
1247
+
1248
+ if (Array.isArray(target)) {
1249
+ if (target.length === 0) {
1250
+ return null;
1251
+ }
1252
+
1253
+ let lastException: Error | null | undefined;
1254
+ for (const targetItem of target) {
1255
+ let resolveResult: URL | null | undefined;
1256
+ try {
1257
+ resolveResult = resolvePackageTarget(
1258
+ packageJsonUrl, targetItem, subpath, packageSubpath,
1259
+ base, pattern, internal, conditions
1260
+ );
1261
+ } catch (error: any) {
1262
+ lastException = error;
1263
+ if (error?.code === "ERR_INVALID_PACKAGE_TARGET") {
1264
+ continue;
1265
+ }
1266
+ throw error;
1267
+ }
1268
+
1269
+ if (resolveResult === undefined) {
1270
+ continue;
1271
+ }
1272
+ if (resolveResult === null) {
1273
+ lastException = null;
1274
+ continue;
1275
+ }
1276
+ return resolveResult;
1277
+ }
1278
+
1279
+ if (lastException === undefined || lastException === null) {
1280
+ return null;
1281
+ }
1282
+ throw lastException;
1283
+ }
1284
+
1285
+ if (typeof target === "object" && target !== null) {
1286
+ const keys = Object.getOwnPropertyNames(target);
1287
+ for (const key of keys) {
1288
+ if (key === "default" || conditions.has(key)) {
1289
+ const conditionalTarget = (target as Record<string, unknown>)[key];
1290
+ const resolveResult = resolvePackageTarget(
1291
+ packageJsonUrl, conditionalTarget, subpath, packageSubpath,
1292
+ base, pattern, internal, conditions
1293
+ );
1294
+ if (resolveResult === undefined) {
1295
+ continue;
1296
+ }
1297
+ return resolveResult;
1298
+ }
1299
+ }
1300
+ return null;
1301
+ }
1302
+
1303
+ if (target === null) {
1304
+ return null;
1305
+ }
1306
+
1307
+ return null;`
1308
+ }),
1309
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1310
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1311
+ name: "packageExportsResolve",
1312
+ parameters: [
1313
+ {
1314
+ name: "packageJsonUrl",
1315
+ type: "URL"
1316
+ },
1317
+ {
1318
+ name: "packageSubpath",
1319
+ type: "string"
1320
+ },
1321
+ {
1322
+ name: "packageConfig",
1323
+ type: "PackageConfig"
1324
+ },
1325
+ {
1326
+ name: "base",
1327
+ type: "URL"
1328
+ },
1329
+ {
1330
+ name: "conditions",
1331
+ type: "Set<string>"
1332
+ }
1333
+ ],
1334
+ returnType: "URL | null | undefined",
1335
+ children: _alloy_js_core.code`let exports = packageConfig.exports;
1336
+ if (isConditionalExportsMainSugar(exports)) {
1337
+ exports = { ".": exports } as Record<string, unknown>;
1338
+ }
1339
+
1340
+ if (
1341
+ exports !== undefined &&
1342
+ exports !== null &&
1343
+ typeof exports === "object" &&
1344
+ Object.prototype.hasOwnProperty.call(exports, packageSubpath) &&
1345
+ !packageSubpath.includes("*") &&
1346
+ !packageSubpath.endsWith("/")
1347
+ ) {
1348
+ const target = (exports as Record<string, unknown>)[packageSubpath];
1349
+ const resolveResult = resolvePackageTarget(
1350
+ packageJsonUrl, target, "", packageSubpath,
1351
+ base, false, false, conditions
1352
+ );
1353
+ if (resolveResult === null || resolveResult === undefined) {
1354
+ const err = new Error(
1355
+ \`Package subpath '\${packageSubpath}' is not defined by "exports" in \${fileURLToPath(packageJsonUrl)}\`
1356
+ );
1357
+ (err as any).code = "ERR_PACKAGE_PATH_NOT_EXPORTED";
1358
+ throw err;
1359
+ }
1360
+ return resolveResult;
1361
+ }
1362
+
1363
+ let bestMatch = "";
1364
+ let bestMatchSubpath = "";
1365
+ const keys = exports ? Object.getOwnPropertyNames(exports) : [];
1366
+
1367
+ for (const key of keys) {
1368
+ const patternIndex = key.indexOf("*");
1369
+ if (
1370
+ patternIndex !== -1 &&
1371
+ packageSubpath.startsWith(key.slice(0, patternIndex))
1372
+ ) {
1373
+ const patternTrailer = key.slice(patternIndex + 1);
1374
+ if (
1375
+ packageSubpath.length >= key.length &&
1376
+ packageSubpath.endsWith(patternTrailer) &&
1377
+ patternKeyCompare(bestMatch, key) === 1 &&
1378
+ key.lastIndexOf("*") === patternIndex
1379
+ ) {
1380
+ bestMatch = key;
1381
+ bestMatchSubpath = packageSubpath.slice(
1382
+ patternIndex,
1383
+ packageSubpath.length - patternTrailer.length
1384
+ );
1385
+ }
1386
+ }
1387
+ }
1388
+
1389
+ if (bestMatch) {
1390
+ const target = (exports as Record<string, unknown>)[bestMatch];
1391
+ const resolveResult = resolvePackageTarget(
1392
+ packageJsonUrl, target, bestMatchSubpath, bestMatch,
1393
+ base, true, false, conditions
1394
+ );
1395
+ if (resolveResult === null || resolveResult === undefined) {
1396
+ const err = new Error(
1397
+ \`Package subpath '\${packageSubpath}' is not defined by "exports" in \${fileURLToPath(packageJsonUrl)}\`
1398
+ );
1399
+ (err as any).code = "ERR_PACKAGE_PATH_NOT_EXPORTED";
1400
+ throw err;
1401
+ }
1402
+ return resolveResult;
1403
+ }
1404
+
1405
+ const err = new Error(
1406
+ \`Package subpath '\${packageSubpath}' is not defined by "exports" in \${fileURLToPath(packageJsonUrl)}\`
1407
+ );
1408
+ (err as any).code = "ERR_PACKAGE_PATH_NOT_EXPORTED";
1409
+ throw err;`
1410
+ }),
1411
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1412
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1413
+ name: "patternKeyCompare",
1414
+ parameters: [{
1415
+ name: "a",
1416
+ type: "string"
1417
+ }, {
1418
+ name: "b",
1419
+ type: "string"
1420
+ }],
1421
+ returnType: "number",
1422
+ children: _alloy_js_core.code`const aPatternIndex = a.indexOf("*");
1423
+ const bPatternIndex = b.indexOf("*");
1424
+ const baseLengthA = aPatternIndex === -1 ? a.length : aPatternIndex + 1;
1425
+ const baseLengthB = bPatternIndex === -1 ? b.length : bPatternIndex + 1;
1426
+ if (baseLengthA > baseLengthB) return -1;
1427
+ if (baseLengthB > baseLengthA) return 1;
1428
+ if (aPatternIndex === -1) return 1;
1429
+ if (bPatternIndex === -1) return -1;
1430
+ if (a.length > b.length) return -1;
1431
+ if (b.length > a.length) return 1;
1432
+ return 0;`
1433
+ }),
1434
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1435
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1436
+ name: "legacyMainResolve",
1437
+ parameters: [{
1438
+ name: "packageJsonUrl",
1439
+ type: "URL"
1440
+ }, {
1441
+ name: "packageConfig",
1442
+ type: "PackageConfig"
1443
+ }],
1444
+ returnType: "URL | undefined",
1445
+ children: _alloy_js_core.code`let guess: URL | undefined;
1446
+ const tries: string[] = [];
1447
+
1448
+ if (packageConfig.main !== undefined) {
1449
+ tries.push(
1450
+ \`./\${packageConfig.main}\`,
1451
+ \`./\${packageConfig.main}.js\`,
1452
+ \`./\${packageConfig.main}.json\`,
1453
+ \`./\${packageConfig.main}.node\`,
1454
+ \`./\${packageConfig.main}/index.js\`,
1455
+ \`./\${packageConfig.main}/index.json\`,
1456
+ \`./\${packageConfig.main}/index.node\`
1457
+ );
1458
+ }
1459
+
1460
+ tries.push("./index.js", "./index.json", "./index.node");
1461
+
1462
+ for (const entry of tries) {
1463
+ guess = new URL(entry, packageJsonUrl);
1464
+ try {
1465
+ const s = statSync(fileURLToPath(guess));
1466
+ if (s.isFile()) {
1467
+ return guess;
1468
+ }
1469
+ } catch {}
1470
+ guess = undefined;
1471
+ }
1472
+
1473
+ return undefined;`
1474
+ }),
1475
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1476
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1477
+ name: "parsePackageName",
1478
+ parameters: [{
1479
+ name: "specifier",
1480
+ type: "string"
1481
+ }, {
1482
+ name: "base",
1483
+ type: "URL"
1484
+ }],
1485
+ returnType: "{ packageName: string; packageSubpath: string; isScoped: boolean }",
1486
+ children: _alloy_js_core.code`let separatorIndex = specifier.indexOf("/");
1487
+ let validPackageName = true;
1488
+ let isScoped = false;
1489
+
1490
+ if (specifier[0] === "@") {
1491
+ isScoped = true;
1492
+ if (separatorIndex === -1 || specifier.length === 0) {
1493
+ validPackageName = false;
1494
+ } else {
1495
+ separatorIndex = specifier.indexOf("/", separatorIndex + 1);
1496
+ }
1497
+ }
1498
+
1499
+ const packageName = separatorIndex === -1
1500
+ ? specifier
1501
+ : specifier.slice(0, separatorIndex);
1502
+
1503
+ if (invalidPackageNameRegEx.exec(packageName) !== null) {
1504
+ validPackageName = false;
1505
+ }
1506
+
1507
+ if (!validPackageName) {
1508
+ const err = new Error(
1509
+ \`Invalid module "\${specifier}" is not a valid package name imported from \${fileURLToPath(base)}\`
1510
+ );
1511
+ (err as any).code = "ERR_INVALID_MODULE_SPECIFIER";
1512
+ throw err;
1513
+ }
1514
+
1515
+ const packageSubpath = "." + (separatorIndex === -1
1516
+ ? ""
1517
+ : specifier.slice(separatorIndex));
1518
+
1519
+ return { packageName, packageSubpath, isScoped };`
1520
+ }),
1521
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1522
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1523
+ name: "packageResolve",
1524
+ parameters: [
1525
+ {
1526
+ name: "specifier",
1527
+ type: "string"
1528
+ },
1529
+ {
1530
+ name: "base",
1531
+ type: "URL"
1532
+ },
1533
+ {
1534
+ name: "conditions",
1535
+ type: "Set<string>"
1536
+ }
1537
+ ],
1538
+ returnType: "URL",
1539
+ children: _alloy_js_core.code`if (builtinModules.includes(specifier)) {
1540
+ return new URL("node:" + specifier);
1541
+ }
1542
+
1543
+ const { packageName, packageSubpath, isScoped } = parsePackageName(specifier, base);
1544
+
1545
+ // Self-resolution: check if the specifier matches the current package name
1546
+ const selfConfig = getPackageScopeConfig(base);
1547
+ if (
1548
+ selfConfig.exists &&
1549
+ selfConfig.name === packageName &&
1550
+ selfConfig.exports !== undefined &&
1551
+ selfConfig.exports !== null
1552
+ ) {
1553
+ const packageJsonUrl = pathToFileURL(selfConfig.pjsonPath);
1554
+ const result = packageExportsResolve(
1555
+ packageJsonUrl, packageSubpath, selfConfig, base, conditions
1556
+ );
1557
+ if (result) {
1558
+ return result;
1559
+ }
1560
+ }
1561
+
1562
+ let packageJsonUrl = new URL(
1563
+ "./node_modules/" + packageName + "/package.json",
1564
+ base
1565
+ );
1566
+ let packageJsonPath = fileURLToPath(packageJsonUrl);
1567
+ let lastPath: string;
1568
+
1569
+ do {
1570
+ let stat: ReturnType<typeof statSync> | undefined;
1571
+ try {
1572
+ stat = statSync(packageJsonPath.slice(0, -13));
1573
+ } catch {}
1574
+
1575
+ if (!stat || !stat.isDirectory()) {
1576
+ lastPath = packageJsonPath;
1577
+ packageJsonUrl = new URL(
1578
+ (isScoped ? "../../../../node_modules/" : "../../../node_modules/") +
1579
+ packageName +
1580
+ "/package.json",
1581
+ packageJsonUrl
1582
+ );
1583
+ packageJsonPath = fileURLToPath(packageJsonUrl);
1584
+ continue;
1585
+ }
1586
+
1587
+ const packageConfig = readPackageConfig(packageJsonPath);
1588
+
1589
+ if (packageConfig.exports !== undefined && packageConfig.exports !== null) {
1590
+ const result = packageExportsResolve(
1591
+ packageJsonUrl, packageSubpath, packageConfig, base, conditions
1592
+ );
1593
+ if (result) {
1594
+ return result;
1595
+ }
1596
+ }
1597
+
1598
+ if (packageSubpath === ".") {
1599
+ const legacyResult = legacyMainResolve(packageJsonUrl, packageConfig);
1600
+ if (legacyResult) {
1601
+ return legacyResult;
1602
+ }
1603
+
1604
+ return new URL(".", packageJsonUrl);
1605
+ }
1606
+
1607
+ return new URL(packageSubpath, packageJsonUrl);
1608
+ } while (packageJsonPath.length !== lastPath!.length);
1609
+
1610
+ const err = new Error(
1611
+ \`Cannot find package '\${packageName}' imported from \${fileURLToPath(base)}\`
1612
+ );
1613
+ (err as any).code = "ERR_MODULE_NOT_FOUND";
1614
+ throw err;`
1615
+ }),
1616
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1617
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1618
+ name: "finalizeResolution",
1619
+ parameters: [{
1620
+ name: "resolved",
1621
+ type: "URL"
1622
+ }, {
1623
+ name: "base",
1624
+ type: "URL"
1625
+ }],
1626
+ returnType: "URL",
1627
+ children: _alloy_js_core.code`if (/%2f|%5c/i.test(resolved.pathname)) {
1628
+ const err = new Error(
1629
+ \`Invalid module "\${resolved.pathname}" must not include encoded "/" or "\\\\" characters imported from \${fileURLToPath(base)}\`
1630
+ );
1631
+ (err as any).code = "ERR_INVALID_MODULE_SPECIFIER";
1632
+ throw err;
1633
+ }
1634
+
1635
+ const filePath = fileURLToPath(resolved);
1636
+
1637
+ let stat: ReturnType<typeof statSync> | undefined;
1638
+ try {
1639
+ stat = statSync(filePath.endsWith("/") ? filePath.slice(0, -1) : filePath);
1640
+ } catch {}
1641
+
1642
+ if (stat && stat.isDirectory()) {
1643
+ const err = new Error(
1644
+ \`Directory import '\${filePath}' is not supported resolving ES modules imported from \${fileURLToPath(base)}\`
1645
+ );
1646
+ (err as any).code = "ERR_UNSUPPORTED_DIR_IMPORT";
1647
+ throw err;
1648
+ }
1649
+
1650
+ if (!stat || !stat.isFile()) {
1651
+ const err = new Error(
1652
+ \`Cannot find module '\${filePath}' imported from \${fileURLToPath(base)}\`
1653
+ );
1654
+ (err as any).code = "ERR_MODULE_NOT_FOUND";
1655
+ throw err;
1656
+ }
1657
+
1658
+ const real = realpathSync(filePath);
1659
+ const { search, hash } = resolved;
1660
+ const realUrl = pathToFileURL(real + (filePath.endsWith(sep) ? "/" : ""));
1661
+ realUrl.search = search;
1662
+ realUrl.hash = hash;
1663
+
1664
+ return realUrl;`
1665
+ }),
1666
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1667
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1668
+ name: "moduleResolve",
1669
+ parameters: [
1670
+ {
1671
+ name: "specifier",
1672
+ type: "string"
1673
+ },
1674
+ {
1675
+ name: "base",
1676
+ type: "URL"
1677
+ },
1678
+ {
1679
+ name: "conditions",
1680
+ type: "Set<string>"
1681
+ }
1682
+ ],
1683
+ returnType: "URL",
1684
+ children: _alloy_js_core.code`let resolved: URL | undefined;
1685
+
1686
+ if (
1687
+ specifier[0] === "/" ||
1688
+ (specifier[0] === "." &&
1689
+ (specifier.length === 1 ||
1690
+ specifier[1] === "/" ||
1691
+ (specifier[1] === "." && (specifier.length === 2 || specifier[2] === "/"))))
1692
+ ) {
1693
+ try {
1694
+ resolved = new URL(specifier, base);
1695
+ } catch (error_: any) {
1696
+ const err = new Error(
1697
+ \`Failed to resolve module specifier "\${specifier}" from "\${base}": Invalid relative URL or base scheme is not hierarchical.\`
1698
+ );
1699
+ err.cause = error_;
1700
+ throw err;
1701
+ }
1702
+ } else if (base.protocol === "file:" && specifier[0] === "#") {
1703
+ const packageConfig = getPackageScopeConfig(base);
1704
+ if (packageConfig.exists && packageConfig.imports) {
1705
+ const packageJsonUrl = pathToFileURL(packageConfig.pjsonPath);
1706
+ const imports = packageConfig.imports;
1707
+ if (Object.prototype.hasOwnProperty.call(imports, specifier) && !specifier.includes("*")) {
1708
+ const resolveResult = resolvePackageTarget(
1709
+ packageJsonUrl, imports[specifier], "", specifier,
1710
+ base, false, true, conditions
1711
+ );
1712
+ if (resolveResult !== null && resolveResult !== undefined) {
1713
+ resolved = resolveResult;
1714
+ }
1715
+ }
1716
+
1717
+ if (!resolved) {
1718
+ let bestMatch = "";
1719
+ let bestMatchSubpath = "";
1720
+ const keys = Object.getOwnPropertyNames(imports);
1721
+
1722
+ for (const key of keys) {
1723
+ const patternIndex = key.indexOf("*");
1724
+ if (patternIndex !== -1 && specifier.startsWith(key.slice(0, -1))) {
1725
+ const patternTrailer = key.slice(patternIndex + 1);
1726
+ if (
1727
+ specifier.length >= key.length &&
1728
+ specifier.endsWith(patternTrailer) &&
1729
+ patternKeyCompare(bestMatch, key) === 1 &&
1730
+ key.lastIndexOf("*") === patternIndex
1731
+ ) {
1732
+ bestMatch = key;
1733
+ bestMatchSubpath = specifier.slice(
1734
+ patternIndex,
1735
+ specifier.length - patternTrailer.length
1736
+ );
1737
+ }
1738
+ }
1739
+ }
1740
+
1741
+ if (bestMatch) {
1742
+ const target = imports[bestMatch];
1743
+ const resolveResult = resolvePackageTarget(
1744
+ packageJsonUrl, target, bestMatchSubpath, bestMatch,
1745
+ base, true, true, conditions
1746
+ );
1747
+ if (resolveResult !== null && resolveResult !== undefined) {
1748
+ resolved = resolveResult;
1749
+ }
1750
+ }
1751
+ }
1752
+ }
1753
+
1754
+ if (!resolved) {
1755
+ const err = new Error(
1756
+ \`Package import specifier "\${specifier}" is not defined in package imported from \${fileURLToPath(base)}\`
1757
+ );
1758
+ (err as any).code = "ERR_PACKAGE_IMPORT_NOT_DEFINED";
1759
+ throw err;
1760
+ }
1761
+ } else {
1762
+ try {
1763
+ resolved = new URL(specifier);
1764
+ } catch {
1765
+ resolved = packageResolve(specifier, base, conditions);
1766
+ }
1767
+ }
1768
+
1769
+ if (resolved.protocol !== "file:") {
1770
+ return resolved;
1771
+ }
1772
+
1773
+ return finalizeResolution(resolved, base);`
1774
+ }),
1775
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1776
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1777
+ name: "tryModuleResolve",
1778
+ parameters: [
1779
+ {
1780
+ name: "id",
1781
+ type: "string"
1782
+ },
1783
+ {
1784
+ name: "url",
1785
+ type: "URL"
1786
+ },
1787
+ {
1788
+ name: "conditions",
1789
+ type: "Set<string>"
1790
+ }
1791
+ ],
1792
+ returnType: "URL | undefined",
1793
+ children: _alloy_js_core.code`try {
1794
+ return moduleResolve(id, url, conditions);
1795
+ } catch (error: any) {
1796
+ if (
1797
+ !(error as { code: string })?.code &&
1798
+ ![
1799
+ "ERR_MODULE_NOT_FOUND",
1800
+ "ERR_UNSUPPORTED_DIR_IMPORT",
1801
+ "MODULE_NOT_FOUND",
1802
+ "ERR_PACKAGE_PATH_NOT_EXPORTED"
1803
+ ].includes(error?.code)
1804
+ ) {
1805
+ throw error;
1806
+ }
1807
+ }`
1808
+ }),
1809
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1810
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_typescript_components_tsdoc.TSDoc, {
1811
+ heading: "A function to resolve module specifiers to URLs.",
1812
+ get children() {
1813
+ return [
1814
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_typescript_components_tsdoc.TSDocParam, {
1815
+ name: "specifier",
1816
+ children: `The module specifier to resolve.`
1817
+ }),
1818
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_typescript_components_tsdoc.TSDocParam, {
1819
+ name: "options",
1820
+ children: `The options for resolving the module. Defaults to an empty object.`
1821
+ }),
1822
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_typescript_components_tsdoc.TSDocReturns, { children: `The result of the resolve operation.` })
1823
+ ];
1824
+ }
1825
+ }),
1826
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.FunctionDeclaration, {
1827
+ async: true,
1828
+ name: "resolveModule",
1829
+ parameters: [{
1830
+ name: "specifier",
1831
+ type: "string"
1832
+ }, {
1833
+ name: "options",
1834
+ type: "ResolveModuleOptions",
1835
+ default: "{} as ResolveModuleOptions"
1836
+ }],
1837
+ returnType: "Promise<string>",
1838
+ get children() {
1839
+ return _alloy_js_core.code`const parentURL = options.parentURL ? new URL(options.parentURL) : undefined;
1840
+ const conditionsSet = options.conditions?.length
1841
+ ? new Set(options.conditions)
1842
+ : DEFAULT_CONDITIONS_SET;
1843
+
1844
+ // Handle protocol-prefixed specifiers (node:, data:, http:, https:, ${context.config.framework}:)
1845
+ if (/^(?:node|data|https?|${context.config.framework}):/.test(specifier)) {
1846
+ return specifier;
1847
+ }
1848
+
1849
+ const bareSpecifier = specifier.replace(/^node:/, "");
1850
+ if (builtinModules.includes(bareSpecifier)) {
1851
+ return "node:" + bareSpecifier;
1852
+ }
1853
+
1854
+ if (specifier.startsWith("file://")) {
1855
+ return specifier;
1856
+ }
1857
+
1858
+ if (isAbsolute(specifier)) {
1859
+ try {
1860
+ const s = statSync(specifier);
1861
+ if (s.isFile()) {
1862
+ return pathToFileURL(realpathSync(specifier)).href;
1863
+ }
1864
+ } catch (err: any) {
1865
+ if (err?.code !== "ENOENT") {
1866
+ throw err;
1867
+ }
1868
+ }
1869
+ }
1870
+
1871
+ const roots = [parentURL ?? pathToFileURL(join(process.cwd(), "_"))];
1872
+
1873
+ const projectRootURL = pathToFileURL("${context.config.root}");
1874
+ if (!roots.some(root => root.href === projectRootURL.href)) {
1875
+ roots.push(projectRootURL);
1876
+ }
1877
+
1878
+ const workspaceRootURL = pathToFileURL("${context.workspaceConfig.workspaceRoot}");
1879
+ if (!roots.some(root => root.href === workspaceRootURL.href)) {
1880
+ roots.push(workspaceRootURL);
1881
+ }
1882
+
1883
+ let resolved: URL | undefined;
1884
+ for (const root of roots) {
1885
+ const _urls: URL[] = [];
1886
+ _urls.push(root);
1887
+ if (root.protocol === "file:") {
1888
+ _urls.push(
1889
+ new URL("./", root),
1890
+ new URL(join(root.pathname, "_index.js"), root),
1891
+ new URL("node_modules", root)
1892
+ );
1893
+ }
1894
+
1895
+ for (const url of _urls) {
1896
+ resolved = tryModuleResolve(specifier, url, conditionsSet);
1897
+ if (resolved) {
1898
+ break;
1899
+ }
1900
+
1901
+ for (const prefix of ["", "/index"]) {
1902
+ for (const extension of DEFAULT_EXTENSIONS) {
1903
+ resolved = tryModuleResolve(
1904
+ specifier + prefix + extension,
1905
+ url,
1906
+ conditionsSet
1907
+ );
1908
+ if (resolved) {
1909
+ break;
1910
+ }
1911
+ }
1912
+ if (resolved) {
1913
+ break;
1914
+ }
1915
+ }
1916
+ if (resolved) {
1917
+ break;
1918
+ }
1919
+ }
1920
+
1921
+ if (resolved) {
1922
+ break;
1923
+ }
1924
+ }
1925
+
1926
+ if (!resolved) {
1927
+ const err = new Error(
1928
+ \`Cannot find module \${specifier} imported from \${base.href}\`
1929
+ );
1930
+ (err as any).code = "ERR_MODULE_NOT_FOUND";
1931
+ throw err;
1932
+ }
1933
+
1934
+ return pathToFileURL(fileURLToPath(resolved)).href;
1935
+ `;
1936
+ }
1937
+ })
1938
+ ];
1939
+ }
948
1940
  function ContextUtilities() {
949
1941
  return _alloy_js_core.code`
950
1942
  /**
@@ -1046,15 +2038,22 @@ function UtilsBuiltin(props) {
1046
2038
  "normalize",
1047
2039
  "join",
1048
2040
  "posix",
1049
- "sep"
2041
+ "sep",
2042
+ "dirname",
2043
+ "isAbsolute"
1050
2044
  ],
1051
2045
  "node:fs": [
1052
2046
  "openSync",
1053
2047
  "closeSync",
1054
- "read"
2048
+ "read",
2049
+ "readFileSync",
2050
+ "statSync",
2051
+ "realpathSync"
1055
2052
  ],
1056
2053
  "node:fs/promises": ["stat"],
1057
2054
  "node:util": ["promisify"],
2055
+ "node:url": ["fileURLToPath", "pathToFileURL"],
2056
+ "node:module": ["builtinModules"],
1058
2057
  "node:child_process": [{
1059
2058
  name: "spawn",
1060
2059
  alias: "_spawn"
@@ -1113,6 +2112,8 @@ function UtilsBuiltin(props) {
1113
2112
  (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1114
2113
  (0, _alloy_js_core_jsx_runtime.createComponent)(SpawnFunctionDeclaration, {}),
1115
2114
  (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
2115
+ (0, _alloy_js_core_jsx_runtime.createComponent)(ResolveModuleFunctionDeclaration, {}),
2116
+ (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1116
2117
  (0, _alloy_js_core_jsx_runtime.createComponent)(FindSuggestionsDeclaration, {}),
1117
2118
  (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core_components_spacing.Spacing, {}),
1118
2119
  (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_core.Show, {
@@ -1133,6 +2134,7 @@ exports.ContextUtilities = ContextUtilities;
1133
2134
  exports.EnvSupportUtilities = EnvSupportUtilities;
1134
2135
  exports.FindSuggestionsDeclaration = FindSuggestionsDeclaration;
1135
2136
  exports.HyperlinkSupportUtilities = HyperlinkSupportUtilities;
2137
+ exports.ResolveModuleFunctionDeclaration = ResolveModuleFunctionDeclaration;
1136
2138
  exports.SpawnFunctionDeclaration = SpawnFunctionDeclaration;
1137
2139
  exports.UtilsBuiltin = UtilsBuiltin;
1138
2140
  //# sourceMappingURL=utils-builtin.cjs.map