@jsenv/node-esm-resolution 1.0.1 → 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,9 +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
- let { packageName, packageSubpath } = parsePackageSpecifier(packageSpecifier)
215
+ let { packageName, packageSubpath } = parsePackageSpecifier(packageSpecifier);
216
216
  if (
217
217
  packageName[0] === "." ||
218
218
  packageName.includes("\\") ||
@@ -222,106 +222,109 @@ const applyPackageResolve = (packageSpecifier, resolutionContext) => {
222
222
  `is not a valid package name`,
223
223
  packageName,
224
224
  resolutionContext,
225
- )
225
+ );
226
226
  }
227
227
  if (packageSubpath.endsWith("/")) {
228
- throw new Error("invalid module specifier")
228
+ throw new Error("invalid module specifier");
229
229
  }
230
- const questionCharIndex = packageName.indexOf("?")
230
+ const questionCharIndex = packageName.indexOf("?");
231
231
  if (questionCharIndex > -1) {
232
- packageName = packageName.slice(0, questionCharIndex)
232
+ packageName = packageName.slice(0, questionCharIndex);
233
233
  }
234
234
  const selfResolution = applyPackageSelfResolution(packageSubpath, {
235
235
  ...resolutionContext,
236
236
  packageName,
237
- })
237
+ });
238
238
  if (selfResolution) {
239
- return selfResolution
239
+ return selfResolution;
240
240
  }
241
- let currentUrl = parentUrl
241
+ let currentUrl = parentUrl;
242
242
  while (currentUrl !== "file:///") {
243
243
  const packageDirectoryFacadeUrl = new URL(
244
244
  `node_modules/${packageName}/`,
245
245
  currentUrl,
246
- ).href
246
+ ).href;
247
247
  if (!existsSync(new URL(packageDirectoryFacadeUrl))) {
248
- currentUrl = getParentUrl(currentUrl)
249
- continue
248
+ currentUrl = getParentUrl(currentUrl);
249
+ continue;
250
250
  }
251
251
  const packageDirectoryUrl = preservesSymlink
252
252
  ? packageDirectoryFacadeUrl
253
- : resolvePackageSymlink(packageDirectoryFacadeUrl)
254
- const packageJson = readPackageJson(packageDirectoryUrl)
253
+ : resolvePackageSymlink(packageDirectoryFacadeUrl);
254
+ const packageJson = readPackageJson(packageDirectoryUrl);
255
255
  if (packageJson !== null) {
256
- const { exports } = packageJson
256
+ const { exports } = packageJson;
257
257
  if (exports !== null && exports !== undefined) {
258
258
  return applyPackageExportsResolution(packageSubpath, {
259
259
  ...resolutionContext,
260
260
  packageDirectoryUrl,
261
261
  packageJson,
262
262
  exports,
263
- })
263
+ });
264
264
  }
265
265
  }
266
266
  return applyLegacySubpathResolution(packageSubpath, {
267
267
  ...resolutionContext,
268
268
  packageDirectoryUrl,
269
269
  packageJson,
270
- })
270
+ });
271
271
  }
272
- throw createModuleNotFoundError(packageName, resolutionContext)
273
- }
272
+ throw createModuleNotFoundError(packageName, resolutionContext);
273
+ };
274
274
 
275
275
  const applyPackageSelfResolution = (packageSubpath, resolutionContext) => {
276
276
  const { parentUrl, packageName, lookupPackageScope, readPackageJson } =
277
- resolutionContext
278
- const packageDirectoryUrl = lookupPackageScope(parentUrl)
277
+ resolutionContext;
278
+ const packageDirectoryUrl = lookupPackageScope(parentUrl);
279
279
  if (!packageDirectoryUrl) {
280
- return undefined
280
+ return undefined;
281
281
  }
282
- const packageJson = readPackageJson(packageDirectoryUrl)
282
+ const packageJson = readPackageJson(packageDirectoryUrl);
283
283
  if (!packageJson) {
284
- return undefined
284
+ return undefined;
285
285
  }
286
286
  if (packageJson.name !== packageName) {
287
- return undefined
287
+ return undefined;
288
288
  }
289
- const { exports } = packageJson
289
+ const { exports } = packageJson;
290
290
  if (!exports) {
291
291
  const subpathResolution = applyLegacySubpathResolution(packageSubpath, {
292
292
  ...resolutionContext,
293
293
  packageDirectoryUrl,
294
294
  packageJson,
295
- })
295
+ });
296
296
  if (subpathResolution && subpathResolution.type !== "subpath") {
297
- return subpathResolution
297
+ return subpathResolution;
298
298
  }
299
- return undefined
299
+ return undefined;
300
300
  }
301
301
  return applyPackageExportsResolution(packageSubpath, {
302
302
  ...resolutionContext,
303
303
  packageDirectoryUrl,
304
304
  packageJson,
305
- })
306
- }
305
+ });
306
+ };
307
307
 
308
308
  // https://github.com/nodejs/node/blob/0367b5c35ea0f98b323175a4aaa8e651af7a91e7/lib/internal/modules/esm/resolve.js#L642
309
309
  const applyPackageExportsResolution = (packageSubpath, resolutionContext) => {
310
310
  if (packageSubpath === ".") {
311
- const mainExport = applyMainExportResolution(resolutionContext)
311
+ const mainExport = applyMainExportResolution(resolutionContext);
312
312
  if (!mainExport) {
313
- throw createPackagePathNotExportedError(packageSubpath, resolutionContext)
313
+ throw createPackagePathNotExportedError(
314
+ packageSubpath,
315
+ resolutionContext,
316
+ );
314
317
  }
315
318
  const resolved = applyPackageTargetResolution(mainExport, {
316
319
  ...resolutionContext,
317
320
  key: ".",
318
- })
321
+ });
319
322
  if (resolved) {
320
- return resolved
323
+ return resolved;
321
324
  }
322
- throw createPackagePathNotExportedError(packageSubpath, resolutionContext)
325
+ throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
323
326
  }
324
- const packageExportsInfo = readExports(resolutionContext)
327
+ const packageExportsInfo = readExports(resolutionContext);
325
328
  if (
326
329
  packageExportsInfo.type === "object" &&
327
330
  packageExportsInfo.allKeysAreRelative
@@ -329,52 +332,52 @@ const applyPackageExportsResolution = (packageSubpath, resolutionContext) => {
329
332
  const resolved = applyPackageImportsExportsResolution(packageSubpath, {
330
333
  ...resolutionContext,
331
334
  isImport: false,
332
- })
335
+ });
333
336
  if (resolved) {
334
- return resolved
337
+ return resolved;
335
338
  }
336
339
  }
337
- throw createPackagePathNotExportedError(packageSubpath, resolutionContext)
338
- }
340
+ throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
341
+ };
339
342
 
340
343
  const applyPackageImportsExportsResolution = (matchKey, resolutionContext) => {
341
- const { packageJson, isImport } = resolutionContext
342
- const matchObject = isImport ? packageJson.imports : packageJson.exports
344
+ const { packageJson, isImport } = resolutionContext;
345
+ const matchObject = isImport ? packageJson.imports : packageJson.exports;
343
346
 
344
347
  if (!matchKey.includes("*") && matchObject.hasOwnProperty(matchKey)) {
345
- const target = matchObject[matchKey]
348
+ const target = matchObject[matchKey];
346
349
  return applyPackageTargetResolution(target, {
347
350
  ...resolutionContext,
348
351
  key: matchKey,
349
352
  isImport,
350
- })
353
+ });
351
354
  }
352
355
  const expansionKeys = Object.keys(matchObject)
353
356
  .filter((key) => key.split("*").length === 2)
354
- .sort(comparePatternKeys)
357
+ .sort(comparePatternKeys);
355
358
  for (const expansionKey of expansionKeys) {
356
- const [patternBase, patternTrailer] = expansionKey.split("*")
357
- if (matchKey === patternBase) continue
358
- if (!matchKey.startsWith(patternBase)) continue
359
+ const [patternBase, patternTrailer] = expansionKey.split("*");
360
+ if (matchKey === patternBase) continue;
361
+ if (!matchKey.startsWith(patternBase)) continue;
359
362
  if (patternTrailer.length > 0) {
360
- if (!matchKey.endsWith(patternTrailer)) continue
361
- if (matchKey.length < expansionKey.length) continue
363
+ if (!matchKey.endsWith(patternTrailer)) continue;
364
+ if (matchKey.length < expansionKey.length) continue;
362
365
  }
363
- const target = matchObject[expansionKey]
366
+ const target = matchObject[expansionKey];
364
367
  const subpath = matchKey.slice(
365
368
  patternBase.length,
366
369
  matchKey.length - patternTrailer.length,
367
- )
370
+ );
368
371
  return applyPackageTargetResolution(target, {
369
372
  ...resolutionContext,
370
373
  key: matchKey,
371
374
  subpath,
372
375
  pattern: true,
373
376
  isImport,
374
- })
377
+ });
375
378
  }
376
- return null
377
- }
379
+ return null;
380
+ };
378
381
 
379
382
  const applyPackageTargetResolution = (target, resolutionContext) => {
380
383
  const {
@@ -385,20 +388,20 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
385
388
  subpath = "",
386
389
  pattern = false,
387
390
  isImport = false,
388
- } = resolutionContext
391
+ } = resolutionContext;
389
392
 
390
393
  if (typeof target === "string") {
391
394
  if (pattern === false && subpath !== "" && !target.endsWith("/")) {
392
- throw new Error("invalid module specifier")
395
+ throw new Error("invalid module specifier");
393
396
  }
394
397
  if (target.startsWith("./")) {
395
- const targetUrl = new URL(target, packageDirectoryUrl).href
398
+ const targetUrl = new URL(target, packageDirectoryUrl).href;
396
399
  if (!targetUrl.startsWith(packageDirectoryUrl)) {
397
400
  throw createInvalidPackageTargetError(
398
401
  `target must be inside package`,
399
402
  target,
400
403
  resolutionContext,
401
- )
404
+ );
402
405
  }
403
406
  return {
404
407
  type: isImport ? "field:imports" : "field:exports",
@@ -407,14 +410,14 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
407
410
  url: pattern
408
411
  ? targetUrl.replaceAll("*", subpath)
409
412
  : new URL(subpath, targetUrl).href,
410
- }
413
+ };
411
414
  }
412
415
  if (!isImport || target.startsWith("../") || isValidUrl(target)) {
413
416
  throw createInvalidPackageTargetError(
414
417
  `target must starst with "./"`,
415
418
  target,
416
419
  resolutionContext,
417
- )
420
+ );
418
421
  }
419
422
  return applyPackageResolve(
420
423
  pattern ? target.replaceAll("*", subpath) : `${target}${subpath}`,
@@ -422,17 +425,17 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
422
425
  ...resolutionContext,
423
426
  parentUrl: packageDirectoryUrl,
424
427
  },
425
- )
428
+ );
426
429
  }
427
430
  if (Array.isArray(target)) {
428
431
  if (target.length === 0) {
429
- return null
432
+ return null;
430
433
  }
431
- let lastResult
432
- let i = 0
434
+ let lastResult;
435
+ let i = 0;
433
436
  while (i < target.length) {
434
- const targetValue = target[i]
435
- i++
437
+ const targetValue = target[i];
438
+ i++;
436
439
  try {
437
440
  const resolved = applyPackageTargetResolution(targetValue, {
438
441
  ...resolutionContext,
@@ -440,77 +443,77 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
440
443
  subpath,
441
444
  pattern,
442
445
  isImport,
443
- })
446
+ });
444
447
  if (resolved) {
445
- return resolved
448
+ return resolved;
446
449
  }
447
- lastResult = resolved
450
+ lastResult = resolved;
448
451
  } catch (e) {
449
452
  if (e.code === "INVALID_PACKAGE_TARGET") {
450
- continue
453
+ continue;
451
454
  }
452
- lastResult = e
455
+ lastResult = e;
453
456
  }
454
457
  }
455
458
  if (lastResult) {
456
- throw lastResult
459
+ throw lastResult;
457
460
  }
458
- return null
461
+ return null;
459
462
  }
460
463
  if (target === null) {
461
- return null
464
+ return null;
462
465
  }
463
466
  if (typeof target === "object") {
464
- const keys = Object.keys(target)
467
+ const keys = Object.keys(target);
465
468
  for (const key of keys) {
466
469
  if (Number.isInteger(key)) {
467
- throw new Error("Invalid package configuration")
470
+ throw new Error("Invalid package configuration");
468
471
  }
469
472
  if (key === "default" || conditions.includes(key)) {
470
- const targetValue = target[key]
473
+ const targetValue = target[key];
471
474
  const resolved = applyPackageTargetResolution(targetValue, {
472
475
  ...resolutionContext,
473
476
  key,
474
477
  subpath,
475
478
  pattern,
476
479
  isImport,
477
- })
480
+ });
478
481
  if (resolved) {
479
- return resolved
482
+ return resolved;
480
483
  }
481
484
  }
482
485
  }
483
- return null
486
+ return null;
484
487
  }
485
488
  throw createInvalidPackageTargetError(
486
489
  `target must be a string, array, object or null`,
487
490
  target,
488
491
  resolutionContext,
489
- )
490
- }
492
+ );
493
+ };
491
494
 
492
495
  const readExports = ({ packageDirectoryUrl, packageJson }) => {
493
- const packageExports = packageJson.exports
496
+ const packageExports = packageJson.exports;
494
497
  if (Array.isArray(packageExports)) {
495
498
  return {
496
499
  type: "array",
497
- }
500
+ };
498
501
  }
499
502
  if (packageExports === null) {
500
- return {}
503
+ return {};
501
504
  }
502
505
  if (typeof packageExports === "object") {
503
- const keys = Object.keys(packageExports)
504
- const relativeKeys = []
505
- const conditionalKeys = []
506
+ const keys = Object.keys(packageExports);
507
+ const relativeKeys = [];
508
+ const conditionalKeys = [];
506
509
  keys.forEach((availableKey) => {
507
510
  if (availableKey.startsWith(".")) {
508
- relativeKeys.push(availableKey)
511
+ relativeKeys.push(availableKey);
509
512
  } else {
510
- conditionalKeys.push(availableKey)
513
+ conditionalKeys.push(availableKey);
511
514
  }
512
- })
513
- const hasRelativeKey = relativeKeys.length > 0
515
+ });
516
+ const hasRelativeKey = relativeKeys.length > 0;
514
517
  if (hasRelativeKey && conditionalKeys.length > 0) {
515
518
  throw new Error(
516
519
  `Invalid package configuration: cannot mix relative and conditional keys in package.exports
@@ -518,113 +521,113 @@ const readExports = ({ packageDirectoryUrl, packageJson }) => {
518
521
  ${conditionalKeys.map((key) => `"${key}"`).join("\n")}
519
522
  --- package directory url ---
520
523
  ${packageDirectoryUrl}`,
521
- )
524
+ );
522
525
  }
523
526
  return {
524
527
  type: "object",
525
528
  hasRelativeKey,
526
529
  allKeysAreRelative: relativeKeys.length === keys.length,
527
- }
530
+ };
528
531
  }
529
532
  if (typeof packageExports === "string") {
530
- return { type: "string" }
533
+ return { type: "string" };
531
534
  }
532
- return {}
533
- }
535
+ return {};
536
+ };
534
537
 
535
538
  const parsePackageSpecifier = (packageSpecifier) => {
536
539
  if (packageSpecifier[0] === "@") {
537
- const firstSlashIndex = packageSpecifier.indexOf("/")
540
+ const firstSlashIndex = packageSpecifier.indexOf("/");
538
541
  if (firstSlashIndex === -1) {
539
- throw new Error("invalid module specifier")
542
+ throw new Error("invalid module specifier");
540
543
  }
541
- const secondSlashIndex = packageSpecifier.indexOf("/", firstSlashIndex + 1)
544
+ const secondSlashIndex = packageSpecifier.indexOf("/", firstSlashIndex + 1);
542
545
  if (secondSlashIndex === -1) {
543
546
  return {
544
547
  packageName: packageSpecifier,
545
548
  packageSubpath: ".",
546
549
  isScoped: true,
547
- }
550
+ };
548
551
  }
549
- const packageName = packageSpecifier.slice(0, secondSlashIndex)
550
- const afterSecondSlash = packageSpecifier.slice(secondSlashIndex + 1)
551
- const packageSubpath = `./${afterSecondSlash}`
552
+ const packageName = packageSpecifier.slice(0, secondSlashIndex);
553
+ const afterSecondSlash = packageSpecifier.slice(secondSlashIndex + 1);
554
+ const packageSubpath = `./${afterSecondSlash}`;
552
555
  return {
553
556
  packageName,
554
557
  packageSubpath,
555
558
  isScoped: true,
556
- }
559
+ };
557
560
  }
558
- const firstSlashIndex = packageSpecifier.indexOf("/")
561
+ const firstSlashIndex = packageSpecifier.indexOf("/");
559
562
  if (firstSlashIndex === -1) {
560
563
  return {
561
564
  packageName: packageSpecifier,
562
565
  packageSubpath: ".",
563
- }
566
+ };
564
567
  }
565
- const packageName = packageSpecifier.slice(0, firstSlashIndex)
566
- const afterFirstSlash = packageSpecifier.slice(firstSlashIndex + 1)
567
- const packageSubpath = `./${afterFirstSlash}`
568
+ const packageName = packageSpecifier.slice(0, firstSlashIndex);
569
+ const afterFirstSlash = packageSpecifier.slice(firstSlashIndex + 1);
570
+ const packageSubpath = `./${afterFirstSlash}`;
568
571
  return {
569
572
  packageName,
570
573
  packageSubpath,
571
- }
572
- }
574
+ };
575
+ };
573
576
 
574
577
  const applyMainExportResolution = (resolutionContext) => {
575
- const { packageJson } = resolutionContext
576
- const packageExportsInfo = readExports(resolutionContext)
578
+ const { packageJson } = resolutionContext;
579
+ const packageExportsInfo = readExports(resolutionContext);
577
580
  if (
578
581
  packageExportsInfo.type === "array" ||
579
582
  packageExportsInfo.type === "string"
580
583
  ) {
581
- return packageJson.exports
584
+ return packageJson.exports;
582
585
  }
583
586
  if (packageExportsInfo.type === "object") {
584
587
  if (packageExportsInfo.hasRelativeKey) {
585
- return packageJson.exports["."]
588
+ return packageJson.exports["."];
586
589
  }
587
- return packageJson.exports
590
+ return packageJson.exports;
588
591
  }
589
- return undefined
590
- }
592
+ return undefined;
593
+ };
591
594
 
592
595
  const applyLegacySubpathResolution = (packageSubpath, resolutionContext) => {
593
- const { packageDirectoryUrl, packageJson } = resolutionContext
596
+ const { packageDirectoryUrl, packageJson } = resolutionContext;
594
597
 
595
598
  if (packageSubpath === ".") {
596
- return applyLegacyMainResolution(packageSubpath, resolutionContext)
599
+ return applyLegacyMainResolution(packageSubpath, resolutionContext);
597
600
  }
598
601
  const browserFieldResolution = applyBrowserFieldResolution(
599
602
  packageSubpath,
600
603
  resolutionContext,
601
- )
604
+ );
602
605
  if (browserFieldResolution) {
603
- return browserFieldResolution
606
+ return browserFieldResolution;
604
607
  }
605
608
  return {
606
609
  type: "subpath",
607
610
  packageDirectoryUrl,
608
611
  packageJson,
609
612
  url: new URL(packageSubpath, packageDirectoryUrl).href,
610
- }
611
- }
613
+ };
614
+ };
612
615
 
613
616
  const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
614
- const { conditions, packageDirectoryUrl, packageJson } = resolutionContext
617
+ const { conditions, packageDirectoryUrl, packageJson } = resolutionContext;
615
618
  for (const condition of conditions) {
616
- const conditionResolver = mainLegacyResolvers[condition]
619
+ const conditionResolver = mainLegacyResolvers[condition];
617
620
  if (!conditionResolver) {
618
- continue
621
+ continue;
619
622
  }
620
- const resolved = conditionResolver(resolutionContext)
623
+ const resolved = conditionResolver(resolutionContext);
621
624
  if (resolved) {
622
625
  return {
623
626
  type: resolved.type,
624
627
  packageDirectoryUrl,
625
628
  packageJson,
626
629
  url: new URL(resolved.path, packageDirectoryUrl).href,
627
- }
630
+ };
628
631
  }
629
632
  }
630
633
  return {
@@ -632,43 +635,43 @@ const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
632
635
  packageDirectoryUrl,
633
636
  packageJson,
634
637
  url: new URL("index.js", packageDirectoryUrl).href,
635
- }
636
- }
638
+ };
639
+ };
637
640
  const mainLegacyResolvers = {
638
641
  import: ({ packageJson }) => {
639
642
  if (typeof packageJson.module === "string") {
640
- return { type: "field:module", path: packageJson.module }
643
+ return { type: "field:module", path: packageJson.module };
641
644
  }
642
645
  if (typeof packageJson.jsnext === "string") {
643
- return { type: "field:jsnext", path: packageJson.jsnext }
646
+ return { type: "field:jsnext", path: packageJson.jsnext };
644
647
  }
645
648
  if (typeof packageJson.main === "string") {
646
- return { type: "field:main", path: packageJson.main }
649
+ return { type: "field:main", path: packageJson.main };
647
650
  }
648
- return null
651
+ return null;
649
652
  },
650
653
  browser: ({ packageDirectoryUrl, packageJson }) => {
651
654
  const browserMain = (() => {
652
655
  if (typeof packageJson.browser === "string") {
653
- return packageJson.browser
656
+ return packageJson.browser;
654
657
  }
655
658
  if (
656
659
  typeof packageJson.browser === "object" &&
657
660
  packageJson.browser !== null
658
661
  ) {
659
- return packageJson.browser["."]
662
+ return packageJson.browser["."];
660
663
  }
661
- return ""
662
- })()
664
+ return "";
665
+ })();
663
666
 
664
667
  if (!browserMain) {
665
668
  if (typeof packageJson.module === "string") {
666
669
  return {
667
670
  type: "field:module",
668
671
  path: packageJson.module,
669
- }
672
+ };
670
673
  }
671
- return null
674
+ return null;
672
675
  }
673
676
  if (
674
677
  typeof packageJson.module !== "string" ||
@@ -677,10 +680,10 @@ const mainLegacyResolvers = {
677
680
  return {
678
681
  type: "field:browser",
679
682
  path: browserMain,
680
- }
683
+ };
681
684
  }
682
- const browserMainUrlObject = new URL(browserMain, packageDirectoryUrl)
683
- const content = readFileSync(browserMainUrlObject, "utf-8")
685
+ const browserMainUrlObject = new URL(browserMain, packageDirectoryUrl);
686
+ const content = readFileSync(browserMainUrlObject, "utf-8");
684
687
  if (
685
688
  (/typeof exports\s*==/.test(content) &&
686
689
  /typeof module\s*==/.test(content)) ||
@@ -689,58 +692,58 @@ const mainLegacyResolvers = {
689
692
  return {
690
693
  type: "field:module",
691
694
  path: packageJson.module,
692
- }
695
+ };
693
696
  }
694
697
  return {
695
698
  type: "field:browser",
696
699
  path: browserMain,
697
- }
700
+ };
698
701
  },
699
702
  node: ({ packageJson }) => {
700
703
  if (typeof packageJson.main === "string") {
701
704
  return {
702
705
  type: "field:main",
703
706
  path: packageJson.main,
704
- }
707
+ };
705
708
  }
706
- return null
709
+ return null;
707
710
  },
708
- }
711
+ };
709
712
 
710
713
  const comparePatternKeys = (keyA, keyB) => {
711
714
  if (!keyA.endsWith("/") && !keyA.includes("*")) {
712
- throw new Error("Invalid package configuration")
715
+ throw new Error("Invalid package configuration");
713
716
  }
714
717
  if (!keyB.endsWith("/") && !keyB.includes("*")) {
715
- throw new Error("Invalid package configuration")
718
+ throw new Error("Invalid package configuration");
716
719
  }
717
- const aStarIndex = keyA.indexOf("*")
718
- const baseLengthA = aStarIndex > -1 ? aStarIndex + 1 : keyA.length
719
- const bStarIndex = keyB.indexOf("*")
720
- 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;
721
724
  if (baseLengthA > baseLengthB) {
722
- return -1
725
+ return -1;
723
726
  }
724
727
  if (baseLengthB > baseLengthA) {
725
- return 1
728
+ return 1;
726
729
  }
727
730
  if (aStarIndex === -1) {
728
- return 1
731
+ return 1;
729
732
  }
730
733
  if (bStarIndex === -1) {
731
- return -1
734
+ return -1;
732
735
  }
733
736
  if (keyA.length > keyB.length) {
734
- return -1
737
+ return -1;
735
738
  }
736
739
  if (keyB.length > keyA.length) {
737
- return 1
740
+ return 1;
738
741
  }
739
- return 0
740
- }
742
+ return 0;
743
+ };
741
744
 
742
745
  const resolvePackageSymlink = (packageDirectoryUrl) => {
743
- const packageDirectoryPath = realpathSync(new URL(packageDirectoryUrl))
744
- const packageDirectoryResolvedUrl = pathToFileURL(packageDirectoryPath).href
745
- return `${packageDirectoryResolvedUrl}/`
746
- }
746
+ const packageDirectoryPath = realpathSync(new URL(packageDirectoryUrl));
747
+ const packageDirectoryResolvedUrl = pathToFileURL(packageDirectoryPath).href;
748
+ return `${packageDirectoryResolvedUrl}/`;
749
+ };