@jsenv/node-esm-resolution 1.0.1 → 1.0.3

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