@jsenv/node-esm-resolution 1.0.0 → 1.0.2

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.
@@ -9,21 +9,21 @@
9
9
  * maintain symlink as facade url when it's outside project directory
10
10
  * or use the real path when inside
11
11
  */
12
- import { pathToFileURL } from "node:url"
13
- import { existsSync, readFileSync, realpathSync } from "node:fs"
12
+ import { pathToFileURL } from "node:url";
13
+ import { existsSync, readFileSync, realpathSync } from "node:fs";
14
14
 
15
- import { isSpecifierForNodeBuiltin } from "./node_builtin_specifiers.js"
16
- import { defaultLookupPackageScope } from "./default_lookup_package_scope.js"
17
- import { defaultReadPackageJson } from "./default_read_package_json.js"
18
- import { getParentUrl, isValidUrl } from "./url_utils.js"
15
+ import { isSpecifierForNodeBuiltin } from "./node_builtin_specifiers.js";
16
+ import { defaultLookupPackageScope } from "./default_lookup_package_scope.js";
17
+ import { defaultReadPackageJson } from "./default_read_package_json.js";
18
+ import { getParentUrl, isValidUrl } from "./url_utils.js";
19
19
  import {
20
20
  createInvalidModuleSpecifierError,
21
21
  createModuleNotFoundError,
22
22
  createPackageImportNotDefinedError,
23
23
  createPackagePathNotExportedError,
24
24
  createInvalidPackageTargetError,
25
- } from "./errors.js"
26
- import { readCustomConditionsFromProcessArgs } from "./custom_conditions.js"
25
+ } from "./errors.js";
26
+ import { readCustomConditionsFromProcessArgs } from "./custom_conditions.js";
27
27
 
28
28
  export const applyNodeEsmResolution = ({
29
29
  specifier,
@@ -39,8 +39,8 @@ export const applyNodeEsmResolution = ({
39
39
  lookupPackageScope,
40
40
  readPackageJson,
41
41
  preservesSymlink,
42
- })
43
- const { url } = resolution
42
+ });
43
+ const { url } = resolution;
44
44
  if (url.startsWith("file:")) {
45
45
  if (url.includes("%2F") || url.includes("%5C")) {
46
46
  throw createInvalidModuleSpecifierError(
@@ -49,15 +49,15 @@ export const applyNodeEsmResolution = ({
49
49
  {
50
50
  parentUrl,
51
51
  },
52
- )
52
+ );
53
53
  }
54
- return resolution
54
+ return resolution;
55
55
  }
56
- return resolution
57
- }
56
+ return resolution;
57
+ };
58
58
 
59
59
  const applyPackageSpecifierResolution = (specifier, resolutionContext) => {
60
- const { parentUrl } = resolutionContext
60
+ const { parentUrl } = resolutionContext;
61
61
  // relative specifier
62
62
  if (
63
63
  specifier[0] === "/" ||
@@ -68,88 +68,88 @@ const applyPackageSpecifierResolution = (specifier, resolutionContext) => {
68
68
  const browserFieldResolution = applyBrowserFieldResolution(
69
69
  specifier,
70
70
  resolutionContext,
71
- )
71
+ );
72
72
  if (browserFieldResolution) {
73
- return browserFieldResolution
73
+ return browserFieldResolution;
74
74
  }
75
75
  }
76
76
  return {
77
77
  type: "relative_specifier",
78
78
  url: new URL(specifier, parentUrl).href,
79
- }
79
+ };
80
80
  }
81
81
  if (specifier[0] === "#") {
82
- return applyPackageImportsResolution(specifier, resolutionContext)
82
+ return applyPackageImportsResolution(specifier, resolutionContext);
83
83
  }
84
84
  try {
85
- const urlObject = new URL(specifier)
85
+ const urlObject = new URL(specifier);
86
86
  if (specifier.startsWith("node:")) {
87
87
  return {
88
88
  type: "node_builtin_specifier",
89
89
  url: specifier,
90
- }
90
+ };
91
91
  }
92
92
  return {
93
93
  type: "absolute_specifier",
94
94
  url: urlObject.href,
95
- }
95
+ };
96
96
  } catch (e) {
97
97
  // bare specifier
98
98
  const browserFieldResolution = applyBrowserFieldResolution(
99
99
  specifier,
100
100
  resolutionContext,
101
- )
101
+ );
102
102
  if (browserFieldResolution) {
103
- return browserFieldResolution
103
+ return browserFieldResolution;
104
104
  }
105
- const packageResolution = applyPackageResolve(specifier, resolutionContext)
106
- const search = new URL(specifier, "file://").search
105
+ const packageResolution = applyPackageResolve(specifier, resolutionContext);
106
+ const search = new URL(specifier, "file://").search;
107
107
  if (search) {
108
- packageResolution.url = `${packageResolution.url}${search}`
108
+ packageResolution.url = `${packageResolution.url}${search}`;
109
109
  }
110
- return packageResolution
110
+ return packageResolution;
111
111
  }
112
- }
112
+ };
113
113
 
114
114
  const applyBrowserFieldResolution = (specifier, resolutionContext) => {
115
115
  const { parentUrl, conditions, lookupPackageScope, readPackageJson } =
116
- resolutionContext
117
- const browserCondition = conditions.includes("browser")
116
+ resolutionContext;
117
+ const browserCondition = conditions.includes("browser");
118
118
  if (!browserCondition) {
119
- return null
119
+ return null;
120
120
  }
121
- const packageDirectoryUrl = lookupPackageScope(parentUrl)
121
+ const packageDirectoryUrl = lookupPackageScope(parentUrl);
122
122
  if (!packageDirectoryUrl) {
123
- return null
123
+ return null;
124
124
  }
125
- const packageJson = readPackageJson(packageDirectoryUrl)
125
+ const packageJson = readPackageJson(packageDirectoryUrl);
126
126
  if (!packageJson) {
127
- return null
127
+ return null;
128
128
  }
129
- const { browser } = packageJson
129
+ const { browser } = packageJson;
130
130
  if (!browser) {
131
- return null
131
+ return null;
132
132
  }
133
133
  if (typeof browser !== "object") {
134
- return null
134
+ return null;
135
135
  }
136
- let url
136
+ let url;
137
137
  if (specifier.startsWith(".")) {
138
- const specifierUrl = new URL(specifier, parentUrl).href
139
- const specifierRelativeUrl = specifierUrl.slice(packageDirectoryUrl.length)
140
- const secifierRelativeNotation = `./${specifierRelativeUrl}`
141
- const browserMapping = browser[secifierRelativeNotation]
138
+ const specifierUrl = new URL(specifier, parentUrl).href;
139
+ const specifierRelativeUrl = specifierUrl.slice(packageDirectoryUrl.length);
140
+ const secifierRelativeNotation = `./${specifierRelativeUrl}`;
141
+ const browserMapping = browser[secifierRelativeNotation];
142
142
  if (typeof browserMapping === "string") {
143
- url = new URL(browserMapping, packageDirectoryUrl).href
143
+ url = new URL(browserMapping, packageDirectoryUrl).href;
144
144
  } else if (browserMapping === false) {
145
- url = `file:///@ignore/${specifierUrl.slice("file:///")}`
145
+ url = `file:///@ignore/${specifierUrl.slice("file:///")}`;
146
146
  }
147
147
  } else {
148
- const browserMapping = browser[specifier]
148
+ const browserMapping = browser[specifier];
149
149
  if (typeof browserMapping === "string") {
150
- url = new URL(browserMapping, packageDirectoryUrl).href
150
+ url = new URL(browserMapping, packageDirectoryUrl).href;
151
151
  } else if (browserMapping === false) {
152
- url = `file:///@ignore/${specifier}`
152
+ url = `file:///@ignore/${specifier}`;
153
153
  }
154
154
  }
155
155
  if (url) {
@@ -158,50 +158,50 @@ const applyBrowserFieldResolution = (specifier, resolutionContext) => {
158
158
  packageDirectoryUrl,
159
159
  packageJson,
160
160
  url,
161
- }
161
+ };
162
162
  }
163
- return null
164
- }
163
+ return null;
164
+ };
165
165
 
166
166
  const applyPackageImportsResolution = (
167
167
  internalSpecifier,
168
168
  resolutionContext,
169
169
  ) => {
170
- const { parentUrl, lookupPackageScope, readPackageJson } = resolutionContext
170
+ const { parentUrl, lookupPackageScope, readPackageJson } = resolutionContext;
171
171
  if (internalSpecifier === "#" || internalSpecifier.startsWith("#/")) {
172
172
  throw createInvalidModuleSpecifierError(
173
173
  "not a valid internal imports specifier name",
174
174
  internalSpecifier,
175
175
  resolutionContext,
176
- )
176
+ );
177
177
  }
178
- const packageDirectoryUrl = lookupPackageScope(parentUrl)
178
+ const packageDirectoryUrl = lookupPackageScope(parentUrl);
179
179
  if (packageDirectoryUrl !== null) {
180
- const packageJson = readPackageJson(packageDirectoryUrl)
181
- const { imports } = packageJson
180
+ const packageJson = readPackageJson(packageDirectoryUrl);
181
+ const { imports } = packageJson;
182
182
  if (imports !== null && typeof imports === "object") {
183
183
  const resolved = applyPackageImportsExportsResolution(internalSpecifier, {
184
184
  ...resolutionContext,
185
185
  packageDirectoryUrl,
186
186
  packageJson,
187
187
  isImport: true,
188
- })
188
+ });
189
189
  if (resolved) {
190
- return resolved
190
+ return resolved;
191
191
  }
192
192
  }
193
193
  }
194
194
  throw createPackageImportNotDefinedError(internalSpecifier, {
195
195
  ...resolutionContext,
196
196
  packageDirectoryUrl,
197
- })
198
- }
197
+ });
198
+ };
199
199
 
200
200
  const applyPackageResolve = (packageSpecifier, resolutionContext) => {
201
201
  const { parentUrl, conditions, readPackageJson, preservesSymlink } =
202
- resolutionContext
202
+ resolutionContext;
203
203
  if (packageSpecifier === "") {
204
- throw new Error("invalid module specifier")
204
+ throw new Error("invalid module specifier");
205
205
  }
206
206
  if (
207
207
  conditions.includes("node") &&
@@ -210,10 +210,9 @@ const applyPackageResolve = (packageSpecifier, resolutionContext) => {
210
210
  return {
211
211
  type: "node_builtin_specifier",
212
212
  url: `node:${packageSpecifier}`,
213
- }
213
+ };
214
214
  }
215
- const { packageName, packageSubpath } =
216
- parsePackageSpecifier(packageSpecifier)
215
+ let { packageName, packageSubpath } = parsePackageSpecifier(packageSpecifier);
217
216
  if (
218
217
  packageName[0] === "." ||
219
218
  packageName.includes("\\") ||
@@ -223,102 +222,109 @@ const applyPackageResolve = (packageSpecifier, resolutionContext) => {
223
222
  `is not a valid package name`,
224
223
  packageName,
225
224
  resolutionContext,
226
- )
225
+ );
227
226
  }
228
227
  if (packageSubpath.endsWith("/")) {
229
- throw new Error("invalid module specifier")
228
+ throw new Error("invalid module specifier");
229
+ }
230
+ const questionCharIndex = packageName.indexOf("?");
231
+ if (questionCharIndex > -1) {
232
+ packageName = packageName.slice(0, questionCharIndex);
230
233
  }
231
234
  const selfResolution = applyPackageSelfResolution(packageSubpath, {
232
235
  ...resolutionContext,
233
236
  packageName,
234
- })
237
+ });
235
238
  if (selfResolution) {
236
- return selfResolution
239
+ return selfResolution;
237
240
  }
238
- let currentUrl = parentUrl
241
+ let currentUrl = parentUrl;
239
242
  while (currentUrl !== "file:///") {
240
243
  const packageDirectoryFacadeUrl = new URL(
241
244
  `node_modules/${packageName}/`,
242
245
  currentUrl,
243
- ).href
246
+ ).href;
244
247
  if (!existsSync(new URL(packageDirectoryFacadeUrl))) {
245
- currentUrl = getParentUrl(currentUrl)
246
- continue
248
+ currentUrl = getParentUrl(currentUrl);
249
+ continue;
247
250
  }
248
251
  const packageDirectoryUrl = preservesSymlink
249
252
  ? packageDirectoryFacadeUrl
250
- : resolvePackageSymlink(packageDirectoryFacadeUrl)
251
- const packageJson = readPackageJson(packageDirectoryUrl)
253
+ : resolvePackageSymlink(packageDirectoryFacadeUrl);
254
+ const packageJson = readPackageJson(packageDirectoryUrl);
252
255
  if (packageJson !== null) {
253
- const { exports } = packageJson
256
+ const { exports } = packageJson;
254
257
  if (exports !== null && exports !== undefined) {
255
258
  return applyPackageExportsResolution(packageSubpath, {
256
259
  ...resolutionContext,
257
260
  packageDirectoryUrl,
258
261
  packageJson,
259
262
  exports,
260
- })
263
+ });
261
264
  }
262
265
  }
263
266
  return applyLegacySubpathResolution(packageSubpath, {
264
267
  ...resolutionContext,
265
268
  packageDirectoryUrl,
266
269
  packageJson,
267
- })
270
+ });
268
271
  }
269
- throw createModuleNotFoundError(packageName, resolutionContext)
270
- }
272
+ throw createModuleNotFoundError(packageName, resolutionContext);
273
+ };
271
274
 
272
275
  const applyPackageSelfResolution = (packageSubpath, resolutionContext) => {
273
276
  const { parentUrl, packageName, lookupPackageScope, readPackageJson } =
274
- resolutionContext
275
- const packageDirectoryUrl = lookupPackageScope(parentUrl)
277
+ resolutionContext;
278
+ const packageDirectoryUrl = lookupPackageScope(parentUrl);
276
279
  if (!packageDirectoryUrl) {
277
- return undefined
280
+ return undefined;
278
281
  }
279
- const packageJson = readPackageJson(packageDirectoryUrl)
282
+ const packageJson = readPackageJson(packageDirectoryUrl);
280
283
  if (!packageJson) {
281
- return undefined
284
+ return undefined;
282
285
  }
283
286
  if (packageJson.name !== packageName) {
284
- return undefined
287
+ return undefined;
285
288
  }
286
- const { exports } = packageJson
289
+ const { exports } = packageJson;
287
290
  if (!exports) {
288
291
  const subpathResolution = applyLegacySubpathResolution(packageSubpath, {
289
292
  ...resolutionContext,
290
293
  packageDirectoryUrl,
291
294
  packageJson,
292
- })
295
+ });
293
296
  if (subpathResolution && subpathResolution.type !== "subpath") {
294
- return subpathResolution
297
+ return subpathResolution;
295
298
  }
296
- return undefined
299
+ return undefined;
297
300
  }
298
301
  return applyPackageExportsResolution(packageSubpath, {
299
302
  ...resolutionContext,
300
303
  packageDirectoryUrl,
301
304
  packageJson,
302
- })
303
- }
305
+ });
306
+ };
304
307
 
305
308
  // https://github.com/nodejs/node/blob/0367b5c35ea0f98b323175a4aaa8e651af7a91e7/lib/internal/modules/esm/resolve.js#L642
306
309
  const applyPackageExportsResolution = (packageSubpath, resolutionContext) => {
307
310
  if (packageSubpath === ".") {
308
- const mainExport = applyMainExportResolution(resolutionContext)
311
+ const mainExport = applyMainExportResolution(resolutionContext);
309
312
  if (!mainExport) {
310
- throw createPackagePathNotExportedError(packageSubpath, resolutionContext)
313
+ throw createPackagePathNotExportedError(
314
+ packageSubpath,
315
+ resolutionContext,
316
+ );
311
317
  }
312
318
  const resolved = applyPackageTargetResolution(mainExport, {
313
319
  ...resolutionContext,
314
320
  key: ".",
315
- })
321
+ });
316
322
  if (resolved) {
317
- return resolved
323
+ return resolved;
318
324
  }
319
- throw createPackagePathNotExportedError(packageSubpath, resolutionContext)
325
+ throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
320
326
  }
321
- const packageExportsInfo = readExports(resolutionContext)
327
+ const packageExportsInfo = readExports(resolutionContext);
322
328
  if (
323
329
  packageExportsInfo.type === "object" &&
324
330
  packageExportsInfo.allKeysAreRelative
@@ -326,52 +332,52 @@ const applyPackageExportsResolution = (packageSubpath, resolutionContext) => {
326
332
  const resolved = applyPackageImportsExportsResolution(packageSubpath, {
327
333
  ...resolutionContext,
328
334
  isImport: false,
329
- })
335
+ });
330
336
  if (resolved) {
331
- return resolved
337
+ return resolved;
332
338
  }
333
339
  }
334
- throw createPackagePathNotExportedError(packageSubpath, resolutionContext)
335
- }
340
+ throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
341
+ };
336
342
 
337
343
  const applyPackageImportsExportsResolution = (matchKey, resolutionContext) => {
338
- const { packageJson, isImport } = resolutionContext
339
- const matchObject = isImport ? packageJson.imports : packageJson.exports
344
+ const { packageJson, isImport } = resolutionContext;
345
+ const matchObject = isImport ? packageJson.imports : packageJson.exports;
340
346
 
341
347
  if (!matchKey.includes("*") && matchObject.hasOwnProperty(matchKey)) {
342
- const target = matchObject[matchKey]
348
+ const target = matchObject[matchKey];
343
349
  return applyPackageTargetResolution(target, {
344
350
  ...resolutionContext,
345
351
  key: matchKey,
346
352
  isImport,
347
- })
353
+ });
348
354
  }
349
355
  const expansionKeys = Object.keys(matchObject)
350
356
  .filter((key) => key.split("*").length === 2)
351
- .sort(comparePatternKeys)
357
+ .sort(comparePatternKeys);
352
358
  for (const expansionKey of expansionKeys) {
353
- const [patternBase, patternTrailer] = expansionKey.split("*")
354
- if (matchKey === patternBase) continue
355
- if (!matchKey.startsWith(patternBase)) continue
359
+ const [patternBase, patternTrailer] = expansionKey.split("*");
360
+ if (matchKey === patternBase) continue;
361
+ if (!matchKey.startsWith(patternBase)) continue;
356
362
  if (patternTrailer.length > 0) {
357
- if (!matchKey.endsWith(patternTrailer)) continue
358
- if (matchKey.length < expansionKey.length) continue
363
+ if (!matchKey.endsWith(patternTrailer)) continue;
364
+ if (matchKey.length < expansionKey.length) continue;
359
365
  }
360
- const target = matchObject[expansionKey]
366
+ const target = matchObject[expansionKey];
361
367
  const subpath = matchKey.slice(
362
368
  patternBase.length,
363
369
  matchKey.length - patternTrailer.length,
364
- )
370
+ );
365
371
  return applyPackageTargetResolution(target, {
366
372
  ...resolutionContext,
367
373
  key: matchKey,
368
374
  subpath,
369
375
  pattern: true,
370
376
  isImport,
371
- })
377
+ });
372
378
  }
373
- return null
374
- }
379
+ return null;
380
+ };
375
381
 
376
382
  const applyPackageTargetResolution = (target, resolutionContext) => {
377
383
  const {
@@ -382,20 +388,20 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
382
388
  subpath = "",
383
389
  pattern = false,
384
390
  isImport = false,
385
- } = resolutionContext
391
+ } = resolutionContext;
386
392
 
387
393
  if (typeof target === "string") {
388
394
  if (pattern === false && subpath !== "" && !target.endsWith("/")) {
389
- throw new Error("invalid module specifier")
395
+ throw new Error("invalid module specifier");
390
396
  }
391
397
  if (target.startsWith("./")) {
392
- const targetUrl = new URL(target, packageDirectoryUrl).href
398
+ const targetUrl = new URL(target, packageDirectoryUrl).href;
393
399
  if (!targetUrl.startsWith(packageDirectoryUrl)) {
394
400
  throw createInvalidPackageTargetError(
395
401
  `target must be inside package`,
396
402
  target,
397
403
  resolutionContext,
398
- )
404
+ );
399
405
  }
400
406
  return {
401
407
  type: isImport ? "field:imports" : "field:exports",
@@ -404,14 +410,14 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
404
410
  url: pattern
405
411
  ? targetUrl.replaceAll("*", subpath)
406
412
  : new URL(subpath, targetUrl).href,
407
- }
413
+ };
408
414
  }
409
415
  if (!isImport || target.startsWith("../") || isValidUrl(target)) {
410
416
  throw createInvalidPackageTargetError(
411
417
  `target must starst with "./"`,
412
418
  target,
413
419
  resolutionContext,
414
- )
420
+ );
415
421
  }
416
422
  return applyPackageResolve(
417
423
  pattern ? target.replaceAll("*", subpath) : `${target}${subpath}`,
@@ -419,17 +425,17 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
419
425
  ...resolutionContext,
420
426
  parentUrl: packageDirectoryUrl,
421
427
  },
422
- )
428
+ );
423
429
  }
424
430
  if (Array.isArray(target)) {
425
431
  if (target.length === 0) {
426
- return null
432
+ return null;
427
433
  }
428
- let lastResult
429
- let i = 0
434
+ let lastResult;
435
+ let i = 0;
430
436
  while (i < target.length) {
431
- const targetValue = target[i]
432
- i++
437
+ const targetValue = target[i];
438
+ i++;
433
439
  try {
434
440
  const resolved = applyPackageTargetResolution(targetValue, {
435
441
  ...resolutionContext,
@@ -437,77 +443,77 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
437
443
  subpath,
438
444
  pattern,
439
445
  isImport,
440
- })
446
+ });
441
447
  if (resolved) {
442
- return resolved
448
+ return resolved;
443
449
  }
444
- lastResult = resolved
450
+ lastResult = resolved;
445
451
  } catch (e) {
446
452
  if (e.code === "INVALID_PACKAGE_TARGET") {
447
- continue
453
+ continue;
448
454
  }
449
- lastResult = e
455
+ lastResult = e;
450
456
  }
451
457
  }
452
458
  if (lastResult) {
453
- throw lastResult
459
+ throw lastResult;
454
460
  }
455
- return null
461
+ return null;
456
462
  }
457
463
  if (target === null) {
458
- return null
464
+ return null;
459
465
  }
460
466
  if (typeof target === "object") {
461
- const keys = Object.keys(target)
467
+ const keys = Object.keys(target);
462
468
  for (const key of keys) {
463
469
  if (Number.isInteger(key)) {
464
- throw new Error("Invalid package configuration")
470
+ throw new Error("Invalid package configuration");
465
471
  }
466
472
  if (key === "default" || conditions.includes(key)) {
467
- const targetValue = target[key]
473
+ const targetValue = target[key];
468
474
  const resolved = applyPackageTargetResolution(targetValue, {
469
475
  ...resolutionContext,
470
476
  key,
471
477
  subpath,
472
478
  pattern,
473
479
  isImport,
474
- })
480
+ });
475
481
  if (resolved) {
476
- return resolved
482
+ return resolved;
477
483
  }
478
484
  }
479
485
  }
480
- return null
486
+ return null;
481
487
  }
482
488
  throw createInvalidPackageTargetError(
483
489
  `target must be a string, array, object or null`,
484
490
  target,
485
491
  resolutionContext,
486
- )
487
- }
492
+ );
493
+ };
488
494
 
489
495
  const readExports = ({ packageDirectoryUrl, packageJson }) => {
490
- const packageExports = packageJson.exports
496
+ const packageExports = packageJson.exports;
491
497
  if (Array.isArray(packageExports)) {
492
498
  return {
493
499
  type: "array",
494
- }
500
+ };
495
501
  }
496
502
  if (packageExports === null) {
497
- return {}
503
+ return {};
498
504
  }
499
505
  if (typeof packageExports === "object") {
500
- const keys = Object.keys(packageExports)
501
- const relativeKeys = []
502
- const conditionalKeys = []
506
+ const keys = Object.keys(packageExports);
507
+ const relativeKeys = [];
508
+ const conditionalKeys = [];
503
509
  keys.forEach((availableKey) => {
504
510
  if (availableKey.startsWith(".")) {
505
- relativeKeys.push(availableKey)
511
+ relativeKeys.push(availableKey);
506
512
  } else {
507
- conditionalKeys.push(availableKey)
513
+ conditionalKeys.push(availableKey);
508
514
  }
509
- })
510
- const hasRelativeKey = relativeKeys.length > 0
515
+ });
516
+ const hasRelativeKey = relativeKeys.length > 0;
511
517
  if (hasRelativeKey && conditionalKeys.length > 0) {
512
518
  throw new Error(
513
519
  `Invalid package configuration: cannot mix relative and conditional keys in package.exports
@@ -515,113 +521,113 @@ const readExports = ({ packageDirectoryUrl, packageJson }) => {
515
521
  ${conditionalKeys.map((key) => `"${key}"`).join("\n")}
516
522
  --- package directory url ---
517
523
  ${packageDirectoryUrl}`,
518
- )
524
+ );
519
525
  }
520
526
  return {
521
527
  type: "object",
522
528
  hasRelativeKey,
523
529
  allKeysAreRelative: relativeKeys.length === keys.length,
524
- }
530
+ };
525
531
  }
526
532
  if (typeof packageExports === "string") {
527
- return { type: "string" }
533
+ return { type: "string" };
528
534
  }
529
- return {}
530
- }
535
+ return {};
536
+ };
531
537
 
532
538
  const parsePackageSpecifier = (packageSpecifier) => {
533
539
  if (packageSpecifier[0] === "@") {
534
- const firstSlashIndex = packageSpecifier.indexOf("/")
540
+ const firstSlashIndex = packageSpecifier.indexOf("/");
535
541
  if (firstSlashIndex === -1) {
536
- throw new Error("invalid module specifier")
542
+ throw new Error("invalid module specifier");
537
543
  }
538
- const secondSlashIndex = packageSpecifier.indexOf("/", firstSlashIndex + 1)
544
+ const secondSlashIndex = packageSpecifier.indexOf("/", firstSlashIndex + 1);
539
545
  if (secondSlashIndex === -1) {
540
546
  return {
541
547
  packageName: packageSpecifier,
542
548
  packageSubpath: ".",
543
549
  isScoped: true,
544
- }
550
+ };
545
551
  }
546
- const packageName = packageSpecifier.slice(0, secondSlashIndex)
547
- const afterSecondSlash = packageSpecifier.slice(secondSlashIndex + 1)
548
- const packageSubpath = `./${afterSecondSlash}`
552
+ const packageName = packageSpecifier.slice(0, secondSlashIndex);
553
+ const afterSecondSlash = packageSpecifier.slice(secondSlashIndex + 1);
554
+ const packageSubpath = `./${afterSecondSlash}`;
549
555
  return {
550
556
  packageName,
551
557
  packageSubpath,
552
558
  isScoped: true,
553
- }
559
+ };
554
560
  }
555
- const firstSlashIndex = packageSpecifier.indexOf("/")
561
+ const firstSlashIndex = packageSpecifier.indexOf("/");
556
562
  if (firstSlashIndex === -1) {
557
563
  return {
558
564
  packageName: packageSpecifier,
559
565
  packageSubpath: ".",
560
- }
566
+ };
561
567
  }
562
- const packageName = packageSpecifier.slice(0, firstSlashIndex)
563
- const afterFirstSlash = packageSpecifier.slice(firstSlashIndex + 1)
564
- const packageSubpath = `./${afterFirstSlash}`
568
+ const packageName = packageSpecifier.slice(0, firstSlashIndex);
569
+ const afterFirstSlash = packageSpecifier.slice(firstSlashIndex + 1);
570
+ const packageSubpath = `./${afterFirstSlash}`;
565
571
  return {
566
572
  packageName,
567
573
  packageSubpath,
568
- }
569
- }
574
+ };
575
+ };
570
576
 
571
577
  const applyMainExportResolution = (resolutionContext) => {
572
- const { packageJson } = resolutionContext
573
- const packageExportsInfo = readExports(resolutionContext)
578
+ const { packageJson } = resolutionContext;
579
+ const packageExportsInfo = readExports(resolutionContext);
574
580
  if (
575
581
  packageExportsInfo.type === "array" ||
576
582
  packageExportsInfo.type === "string"
577
583
  ) {
578
- return packageJson.exports
584
+ return packageJson.exports;
579
585
  }
580
586
  if (packageExportsInfo.type === "object") {
581
587
  if (packageExportsInfo.hasRelativeKey) {
582
- return packageJson.exports["."]
588
+ return packageJson.exports["."];
583
589
  }
584
- return packageJson.exports
590
+ return packageJson.exports;
585
591
  }
586
- return undefined
587
- }
592
+ return undefined;
593
+ };
588
594
 
589
595
  const applyLegacySubpathResolution = (packageSubpath, resolutionContext) => {
590
- const { packageDirectoryUrl, packageJson } = resolutionContext
596
+ const { packageDirectoryUrl, packageJson } = resolutionContext;
591
597
 
592
598
  if (packageSubpath === ".") {
593
- return applyLegacyMainResolution(packageSubpath, resolutionContext)
599
+ return applyLegacyMainResolution(packageSubpath, resolutionContext);
594
600
  }
595
601
  const browserFieldResolution = applyBrowserFieldResolution(
596
602
  packageSubpath,
597
603
  resolutionContext,
598
- )
604
+ );
599
605
  if (browserFieldResolution) {
600
- return browserFieldResolution
606
+ return browserFieldResolution;
601
607
  }
602
608
  return {
603
609
  type: "subpath",
604
610
  packageDirectoryUrl,
605
611
  packageJson,
606
612
  url: new URL(packageSubpath, packageDirectoryUrl).href,
607
- }
608
- }
613
+ };
614
+ };
609
615
 
610
616
  const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
611
- const { conditions, packageDirectoryUrl, packageJson } = resolutionContext
617
+ const { conditions, packageDirectoryUrl, packageJson } = resolutionContext;
612
618
  for (const condition of conditions) {
613
- const conditionResolver = mainLegacyResolvers[condition]
619
+ const conditionResolver = mainLegacyResolvers[condition];
614
620
  if (!conditionResolver) {
615
- continue
621
+ continue;
616
622
  }
617
- const resolved = conditionResolver(resolutionContext)
623
+ const resolved = conditionResolver(resolutionContext);
618
624
  if (resolved) {
619
625
  return {
620
626
  type: resolved.type,
621
627
  packageDirectoryUrl,
622
628
  packageJson,
623
629
  url: new URL(resolved.path, packageDirectoryUrl).href,
624
- }
630
+ };
625
631
  }
626
632
  }
627
633
  return {
@@ -629,43 +635,43 @@ const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
629
635
  packageDirectoryUrl,
630
636
  packageJson,
631
637
  url: new URL("index.js", packageDirectoryUrl).href,
632
- }
633
- }
638
+ };
639
+ };
634
640
  const mainLegacyResolvers = {
635
641
  import: ({ packageJson }) => {
636
642
  if (typeof packageJson.module === "string") {
637
- return { type: "field:module", path: packageJson.module }
643
+ return { type: "field:module", path: packageJson.module };
638
644
  }
639
645
  if (typeof packageJson.jsnext === "string") {
640
- return { type: "field:jsnext", path: packageJson.jsnext }
646
+ return { type: "field:jsnext", path: packageJson.jsnext };
641
647
  }
642
648
  if (typeof packageJson.main === "string") {
643
- return { type: "field:main", path: packageJson.main }
649
+ return { type: "field:main", path: packageJson.main };
644
650
  }
645
- return null
651
+ return null;
646
652
  },
647
653
  browser: ({ packageDirectoryUrl, packageJson }) => {
648
654
  const browserMain = (() => {
649
655
  if (typeof packageJson.browser === "string") {
650
- return packageJson.browser
656
+ return packageJson.browser;
651
657
  }
652
658
  if (
653
659
  typeof packageJson.browser === "object" &&
654
660
  packageJson.browser !== null
655
661
  ) {
656
- return packageJson.browser["."]
662
+ return packageJson.browser["."];
657
663
  }
658
- return ""
659
- })()
664
+ return "";
665
+ })();
660
666
 
661
667
  if (!browserMain) {
662
668
  if (typeof packageJson.module === "string") {
663
669
  return {
664
670
  type: "field:module",
665
671
  path: packageJson.module,
666
- }
672
+ };
667
673
  }
668
- return null
674
+ return null;
669
675
  }
670
676
  if (
671
677
  typeof packageJson.module !== "string" ||
@@ -674,10 +680,10 @@ const mainLegacyResolvers = {
674
680
  return {
675
681
  type: "field:browser",
676
682
  path: browserMain,
677
- }
683
+ };
678
684
  }
679
- const browserMainUrlObject = new URL(browserMain, packageDirectoryUrl)
680
- const content = readFileSync(browserMainUrlObject, "utf-8")
685
+ const browserMainUrlObject = new URL(browserMain, packageDirectoryUrl);
686
+ const content = readFileSync(browserMainUrlObject, "utf-8");
681
687
  if (
682
688
  (/typeof exports\s*==/.test(content) &&
683
689
  /typeof module\s*==/.test(content)) ||
@@ -686,58 +692,58 @@ const mainLegacyResolvers = {
686
692
  return {
687
693
  type: "field:module",
688
694
  path: packageJson.module,
689
- }
695
+ };
690
696
  }
691
697
  return {
692
698
  type: "field:browser",
693
699
  path: browserMain,
694
- }
700
+ };
695
701
  },
696
702
  node: ({ packageJson }) => {
697
703
  if (typeof packageJson.main === "string") {
698
704
  return {
699
705
  type: "field:main",
700
706
  path: packageJson.main,
701
- }
707
+ };
702
708
  }
703
- return null
709
+ return null;
704
710
  },
705
- }
711
+ };
706
712
 
707
713
  const comparePatternKeys = (keyA, keyB) => {
708
714
  if (!keyA.endsWith("/") && !keyA.includes("*")) {
709
- throw new Error("Invalid package configuration")
715
+ throw new Error("Invalid package configuration");
710
716
  }
711
717
  if (!keyB.endsWith("/") && !keyB.includes("*")) {
712
- throw new Error("Invalid package configuration")
718
+ throw new Error("Invalid package configuration");
713
719
  }
714
- const aStarIndex = keyA.indexOf("*")
715
- const baseLengthA = aStarIndex > -1 ? aStarIndex + 1 : keyA.length
716
- const bStarIndex = keyB.indexOf("*")
717
- const baseLengthB = bStarIndex > -1 ? bStarIndex + 1 : keyB.length
720
+ const aStarIndex = keyA.indexOf("*");
721
+ const baseLengthA = aStarIndex > -1 ? aStarIndex + 1 : keyA.length;
722
+ const bStarIndex = keyB.indexOf("*");
723
+ const baseLengthB = bStarIndex > -1 ? bStarIndex + 1 : keyB.length;
718
724
  if (baseLengthA > baseLengthB) {
719
- return -1
725
+ return -1;
720
726
  }
721
727
  if (baseLengthB > baseLengthA) {
722
- return 1
728
+ return 1;
723
729
  }
724
730
  if (aStarIndex === -1) {
725
- return 1
731
+ return 1;
726
732
  }
727
733
  if (bStarIndex === -1) {
728
- return -1
734
+ return -1;
729
735
  }
730
736
  if (keyA.length > keyB.length) {
731
- return -1
737
+ return -1;
732
738
  }
733
739
  if (keyB.length > keyA.length) {
734
- return 1
740
+ return 1;
735
741
  }
736
- return 0
737
- }
742
+ return 0;
743
+ };
738
744
 
739
745
  const resolvePackageSymlink = (packageDirectoryUrl) => {
740
- const packageDirectoryPath = realpathSync(new URL(packageDirectoryUrl))
741
- const packageDirectoryResolvedUrl = pathToFileURL(packageDirectoryPath).href
742
- return `${packageDirectoryResolvedUrl}/`
743
- }
746
+ const packageDirectoryPath = realpathSync(new URL(packageDirectoryUrl));
747
+ const packageDirectoryResolvedUrl = pathToFileURL(packageDirectoryPath).href;
748
+ return `${packageDirectoryResolvedUrl}/`;
749
+ };