@jsenv/node-esm-resolution 0.0.5 → 0.0.8

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.
package/main.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { applyNodeEsmResolution } from "./src/node_esm_resolution.js"
2
2
  export { determineModuleSystem } from "./src/determine_module_system.js"
3
3
  export { applyFileSystemMagicResolution } from "./src/filesystem_magic_resolution.js"
4
- export { lookupPackageScope } from "./src/lookup_package_scope.js"
5
- export { readPackageJson } from "./src/read_package_json.js"
4
+ export { defaultLookupPackageScope } from "./src/default_lookup_package_scope.js"
5
+ export { defaultReadPackageJson } from "./src/default_read_package_json.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/node-esm-resolution",
3
- "version": "0.0.5",
3
+ "version": "0.0.8",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -2,7 +2,7 @@ import { existsSync } from "node:fs"
2
2
 
3
3
  import { getParentUrl, asDirectoryUrl } from "./url_utils.js"
4
4
 
5
- export const lookupPackageScope = (url) => {
5
+ export const defaultLookupPackageScope = (url) => {
6
6
  let scopeUrl = asDirectoryUrl(url)
7
7
  while (scopeUrl !== "file:///") {
8
8
  if (scopeUrl.endsWith("node_modules/")) {
@@ -1,6 +1,6 @@
1
1
  import { readFileSync } from "node:fs"
2
2
 
3
- export const readPackageJson = (packageUrl) => {
3
+ export const defaultReadPackageJson = (packageUrl) => {
4
4
  const packageJsonUrl = new URL("package.json", packageUrl)
5
5
  const buffer = readFileSync(packageJsonUrl)
6
6
  const string = String(buffer)
@@ -1,5 +1,5 @@
1
- import { lookupPackageScope } from "./lookup_package_scope.js"
2
- import { readPackageJson } from "./read_package_json.js"
1
+ import { defaultLookupPackageScope } from "./default_lookup_package_scope.js"
2
+ import { defaultReadPackageJson } from "./default_read_package_json.js"
3
3
 
4
4
  // https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#packages_determining_module_system)
5
5
  export const determineModuleSystem = (
@@ -29,11 +29,11 @@ export const determineModuleSystem = (
29
29
  return "url"
30
30
  }
31
31
  if (ambiguousExtensions.includes(extension)) {
32
- const packageUrl = lookupPackageScope(url)
32
+ const packageUrl = defaultLookupPackageScope(url)
33
33
  if (!packageUrl) {
34
34
  return "commonjs"
35
35
  }
36
- const packageJson = readPackageJson(packageUrl)
36
+ const packageJson = defaultReadPackageJson(packageUrl)
37
37
  if (packageJson.type === "module") {
38
38
  return "module"
39
39
  }
@@ -35,14 +35,14 @@ export const applyFileSystemMagicResolution = (
35
35
  magicExtensions,
36
36
  })
37
37
  return {
38
- magicDirectoryIndex: true,
39
38
  ...result,
39
+ magicDirectoryIndex: true,
40
40
  }
41
41
  }
42
42
  return {
43
- isDirectory: true,
44
43
  found: true,
45
44
  url: fileUrl,
45
+ isDirectory: true,
46
46
  }
47
47
  }
48
48
  if (magicExtensions && magicExtensions.length) {
@@ -56,9 +56,9 @@ export const applyFileSystemMagicResolution = (
56
56
  if (extensionLeadingToFile) {
57
57
  // magic extension worked
58
58
  return {
59
- magicExtension: extensionLeadingToFile,
60
59
  found: true,
61
60
  url: `${fileUrl}${extensionLeadingToFile}`,
61
+ magicExtension: extensionLeadingToFile,
62
62
  }
63
63
  }
64
64
  }
@@ -12,8 +12,8 @@
12
12
  import { existsSync, readFileSync } from "node:fs"
13
13
 
14
14
  import { isSpecifierForNodeBuiltin } from "./node_builtin_specifiers.js"
15
- import { lookupPackageScope } from "./lookup_package_scope.js"
16
- import { readPackageJson } from "./read_package_json.js"
15
+ import { defaultLookupPackageScope } from "./default_lookup_package_scope.js"
16
+ import { defaultReadPackageJson } from "./default_read_package_json.js"
17
17
  import { getParentUrl, isValidUrl } from "./url_utils.js"
18
18
  import {
19
19
  createInvalidModuleSpecifierError,
@@ -27,11 +27,15 @@ export const applyNodeEsmResolution = ({
27
27
  conditions = ["node", "import"],
28
28
  parentUrl,
29
29
  specifier,
30
+ lookupPackageScope = defaultLookupPackageScope,
31
+ readPackageJson = defaultReadPackageJson,
30
32
  }) => {
31
33
  const resolution = applyPackageSpecifierResolution({
32
34
  conditions,
33
35
  parentUrl: String(parentUrl),
34
36
  specifier,
37
+ lookupPackageScope,
38
+ readPackageJson,
35
39
  })
36
40
  const { url } = resolution
37
41
  if (url.startsWith("file:")) {
@@ -51,6 +55,8 @@ const applyPackageSpecifierResolution = ({
51
55
  conditions,
52
56
  parentUrl,
53
57
  specifier,
58
+ lookupPackageScope,
59
+ readPackageJson,
54
60
  }) => {
55
61
  // relative specifier
56
62
  if (
@@ -63,6 +69,8 @@ const applyPackageSpecifierResolution = ({
63
69
  conditions,
64
70
  parentUrl,
65
71
  specifier,
72
+ lookupPackageScope,
73
+ readPackageJson,
66
74
  })
67
75
  if (browserFieldResolution) {
68
76
  return browserFieldResolution
@@ -78,6 +86,8 @@ const applyPackageSpecifierResolution = ({
78
86
  conditions,
79
87
  parentUrl,
80
88
  specifier,
89
+ lookupPackageScope,
90
+ readPackageJson,
81
91
  })
82
92
  }
83
93
  try {
@@ -92,6 +102,8 @@ const applyPackageSpecifierResolution = ({
92
102
  conditions,
93
103
  parentUrl,
94
104
  packageSpecifier: specifier,
105
+ lookupPackageScope,
106
+ readPackageJson,
95
107
  })
96
108
  if (browserFieldResolution) {
97
109
  return browserFieldResolution
@@ -100,6 +112,8 @@ const applyPackageSpecifierResolution = ({
100
112
  conditions,
101
113
  parentUrl,
102
114
  packageSpecifier: specifier,
115
+ lookupPackageScope,
116
+ readPackageJson,
103
117
  })
104
118
  }
105
119
  }
@@ -108,6 +122,8 @@ const applyBrowserFieldResolution = ({
108
122
  conditions,
109
123
  parentUrl,
110
124
  packageSpecifier,
125
+ lookupPackageScope,
126
+ readPackageJson,
111
127
  }) => {
112
128
  const browserCondition = conditions.includes("browser")
113
129
  if (!browserCondition) {
@@ -164,6 +180,8 @@ const applyPackageImportsResolution = ({
164
180
  conditions,
165
181
  parentUrl,
166
182
  specifier,
183
+ lookupPackageScope,
184
+ readPackageJson,
167
185
  }) => {
168
186
  if (!specifier.startsWith("#")) {
169
187
  throw createInvalidModuleSpecifierError({
@@ -192,6 +210,8 @@ const applyPackageImportsResolution = ({
192
210
  matchObject: imports,
193
211
  matchKey: specifier,
194
212
  isImports: true,
213
+ lookupPackageScope,
214
+ readPackageJson,
195
215
  })
196
216
  if (resolved) {
197
217
  return resolved
@@ -205,7 +225,13 @@ const applyPackageImportsResolution = ({
205
225
  })
206
226
  }
207
227
 
208
- const applyPackageResolve = ({ conditions, parentUrl, packageSpecifier }) => {
228
+ const applyPackageResolve = ({
229
+ conditions,
230
+ parentUrl,
231
+ packageSpecifier,
232
+ lookupPackageScope,
233
+ readPackageJson,
234
+ }) => {
209
235
  if (packageSpecifier === "") {
210
236
  throw new Error("invalid module specifier")
211
237
  }
@@ -239,6 +265,8 @@ const applyPackageResolve = ({ conditions, parentUrl, packageSpecifier }) => {
239
265
  parentUrl,
240
266
  packageName,
241
267
  packageSubpath,
268
+ lookupPackageScope,
269
+ readPackageJson,
242
270
  })
243
271
  if (selfResolution) {
244
272
  return selfResolution
@@ -261,6 +289,8 @@ const applyPackageResolve = ({ conditions, parentUrl, packageSpecifier }) => {
261
289
  packageJson,
262
290
  packageSubpath,
263
291
  exports,
292
+ lookupPackageScope,
293
+ readPackageJson,
264
294
  })
265
295
  }
266
296
  }
@@ -270,6 +300,8 @@ const applyPackageResolve = ({ conditions, parentUrl, packageSpecifier }) => {
270
300
  packageUrl,
271
301
  packageJson,
272
302
  packageSubpath,
303
+ lookupPackageScope,
304
+ readPackageJson,
273
305
  })
274
306
  }
275
307
  throw createModuleNotFoundError({
@@ -283,6 +315,8 @@ const applyPackageSelfResolution = ({
283
315
  parentUrl,
284
316
  packageName,
285
317
  packageSubpath,
318
+ lookupPackageScope,
319
+ readPackageJson,
286
320
  }) => {
287
321
  const packageUrl = lookupPackageScope(parentUrl)
288
322
  if (!packageUrl) {
@@ -303,6 +337,8 @@ const applyPackageSelfResolution = ({
303
337
  packageUrl,
304
338
  packageJson,
305
339
  packageSubpath,
340
+ lookupPackageScope,
341
+ readPackageJson,
306
342
  })
307
343
  if (subpathResolution && subpathResolution.type !== "subpath") {
308
344
  return subpathResolution
@@ -316,6 +352,8 @@ const applyPackageSelfResolution = ({
316
352
  packageJson,
317
353
  packageSubpath,
318
354
  exports,
355
+ lookupPackageScope,
356
+ readPackageJson,
319
357
  })
320
358
  }
321
359
 
@@ -327,6 +365,8 @@ const applyPackageExportsResolution = ({
327
365
  packageJson,
328
366
  packageSubpath,
329
367
  exports,
368
+ lookupPackageScope,
369
+ readPackageJson,
330
370
  }) => {
331
371
  const exportsInfo = readExports({ exports, packageUrl })
332
372
  if (packageSubpath === ".") {
@@ -345,6 +385,8 @@ const applyPackageExportsResolution = ({
345
385
  packageJson,
346
386
  key: ".",
347
387
  target: mainExport,
388
+ lookupPackageScope,
389
+ readPackageJson,
348
390
  })
349
391
  if (resolved) {
350
392
  return resolved
@@ -364,6 +406,8 @@ const applyPackageExportsResolution = ({
364
406
  matchObject: exports,
365
407
  matchKey: packageSubpath,
366
408
  isImports: false,
409
+ lookupPackageScope,
410
+ readPackageJson,
367
411
  })
368
412
  if (resolved) {
369
413
  return resolved
@@ -384,6 +428,8 @@ const applyPackageImportsExportsResolution = ({
384
428
  matchObject,
385
429
  matchKey,
386
430
  isImports,
431
+ lookupPackageScope,
432
+ readPackageJson,
387
433
  }) => {
388
434
  if (!matchKey.includes("*") && matchObject.hasOwnProperty(matchKey)) {
389
435
  const target = matchObject[matchKey]
@@ -395,6 +441,8 @@ const applyPackageImportsExportsResolution = ({
395
441
  key: matchKey,
396
442
  target,
397
443
  internal: isImports,
444
+ lookupPackageScope,
445
+ readPackageJson,
398
446
  })
399
447
  }
400
448
  const expansionKeys = Object.keys(matchObject)
@@ -423,6 +471,8 @@ const applyPackageImportsExportsResolution = ({
423
471
  subpath,
424
472
  pattern: true,
425
473
  internal: isImports,
474
+ lookupPackageScope,
475
+ readPackageJson,
426
476
  })
427
477
  }
428
478
  return null
@@ -438,6 +488,8 @@ const applyPackageTargetResolution = ({
438
488
  subpath = "",
439
489
  pattern = false,
440
490
  internal = false,
491
+ lookupPackageScope,
492
+ readPackageJson,
441
493
  }) => {
442
494
  if (typeof target === "string") {
443
495
  if (pattern === false && subpath !== "" && !target.endsWith("/")) {
@@ -480,6 +532,8 @@ const applyPackageTargetResolution = ({
480
532
  packageSpecifier: pattern
481
533
  ? target.replaceAll("*", subpath)
482
534
  : `${target}${subpath}`,
535
+ lookupPackageScope,
536
+ readPackageJson,
483
537
  })
484
538
  }
485
539
  if (Array.isArray(target)) {
@@ -502,6 +556,8 @@ const applyPackageTargetResolution = ({
502
556
  subpath,
503
557
  pattern,
504
558
  internal,
559
+ lookupPackageScope,
560
+ readPackageJson,
505
561
  })
506
562
  if (resolved) {
507
563
  return resolved
@@ -540,6 +596,8 @@ const applyPackageTargetResolution = ({
540
596
  subpath,
541
597
  pattern,
542
598
  internal,
599
+ lookupPackageScope,
600
+ readPackageJson,
543
601
  })
544
602
  if (resolved) {
545
603
  return resolved
@@ -658,6 +716,8 @@ const applyLegacySubpathResolution = ({
658
716
  packageUrl,
659
717
  packageJson,
660
718
  packageSubpath,
719
+ lookupPackageScope,
720
+ readPackageJson,
661
721
  }) => {
662
722
  if (packageSubpath === ".") {
663
723
  return applyLegacyMainResolution({
@@ -670,6 +730,8 @@ const applyLegacySubpathResolution = ({
670
730
  conditions,
671
731
  parentUrl,
672
732
  specifier: packageSubpath,
733
+ lookupPackageScope,
734
+ readPackageJson,
673
735
  })
674
736
  if (browserFieldResolution) {
675
737
  return browserFieldResolution
@@ -702,26 +764,39 @@ const applyLegacyMainResolution = ({ conditions, packageUrl, packageJson }) => {
702
764
  }
703
765
  }
704
766
  const mainLegacyResolvers = {
705
- import: ({ module, jsnext }) => {
706
- if (typeof module === "string") {
707
- return { type: "module", path: module }
767
+ import: (packageJson) => {
768
+ if (typeof packageJson.module === "string") {
769
+ return { type: "module", path: packageJson.module }
770
+ }
771
+ if (typeof packageJson.jsnext === "string") {
772
+ return { type: "jsnext", path: packageJson.jsnext }
708
773
  }
709
- if (typeof jsnext === "string") {
710
- return { type: "jsnext", path: jsnext }
774
+ if (typeof packageJson.main === "string") {
775
+ return { type: "main", path: packageJson.main }
711
776
  }
712
777
  return null
713
778
  },
714
- browser: ({ browser, module }, packageUrl) => {
779
+ browser: (packageJson, packageUrl) => {
715
780
  const browserMain =
716
- typeof browser === "string"
717
- ? browser
718
- : typeof browser === "object" && browser !== null
719
- ? browser["."]
781
+ typeof packageJson.browser === "string"
782
+ ? packageJson.browser
783
+ : typeof packageJson.browser === "object" &&
784
+ packageJson.browser !== null
785
+ ? packageJson.browser["."]
720
786
  : ""
721
787
  if (!browserMain) {
788
+ if (typeof packageJson.module === "string") {
789
+ return {
790
+ type: "module",
791
+ path: packageJson.module,
792
+ }
793
+ }
722
794
  return null
723
795
  }
724
- if (typeof module !== "string" || module === browserMain) {
796
+ if (
797
+ typeof packageJson.module !== "string" ||
798
+ packageJson.module === browserMain
799
+ ) {
725
800
  return {
726
801
  type: "browser",
727
802
  path: browserMain,
@@ -736,7 +811,7 @@ const mainLegacyResolvers = {
736
811
  ) {
737
812
  return {
738
813
  type: "module",
739
- path: module,
814
+ path: packageJson.module,
740
815
  }
741
816
  }
742
817
  return {
@@ -744,11 +819,11 @@ const mainLegacyResolvers = {
744
819
  path: browserMain,
745
820
  }
746
821
  },
747
- node: ({ main }) => {
748
- if (typeof main === "string") {
822
+ node: (packageJson) => {
823
+ if (typeof packageJson.main === "string") {
749
824
  return {
750
825
  type: "main",
751
- path: main,
826
+ path: packageJson.main,
752
827
  }
753
828
  }
754
829
  return null