@jsenv/node-esm-resolution 0.0.6 → 0.0.10

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
@@ -3,3 +3,4 @@ export { determineModuleSystem } from "./src/determine_module_system.js"
3
3
  export { applyFileSystemMagicResolution } from "./src/filesystem_magic_resolution.js"
4
4
  export { defaultLookupPackageScope } from "./src/default_lookup_package_scope.js"
5
5
  export { defaultReadPackageJson } from "./src/default_read_package_json.js"
6
+ export { readCustomConditionsFromProcessArgs } from "./src/custom_conditions.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/node-esm-resolution",
3
- "version": "0.0.6",
3
+ "version": "0.0.10",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,15 @@
1
+ // https://nodejs.org/api/packages.html#resolving-user-conditions
2
+ export const readCustomConditionsFromProcessArgs = () => {
3
+ const packageConditions = []
4
+ process.execArgv.forEach((arg) => {
5
+ if (arg.includes("-C=")) {
6
+ const packageCondition = arg.slice(0, "-C=".length)
7
+ packageConditions.push(packageCondition)
8
+ }
9
+ if (arg.includes("--conditions=")) {
10
+ const packageCondition = arg.slice(0, "--conditions=".length)
11
+ packageConditions.push(packageCondition)
12
+ }
13
+ })
14
+ return packageConditions
15
+ }
@@ -4,7 +4,7 @@ import { urlToFilename } from "./url_utils.js"
4
4
 
5
5
  export const applyFileSystemMagicResolution = (
6
6
  fileUrl,
7
- { magicDirectoryIndex, magicExtensions },
7
+ { fileStat, magicDirectoryIndex, magicExtensions },
8
8
  ) => {
9
9
  let lastENOENTError = null
10
10
  const fileStatOrNull = (url) => {
@@ -18,8 +18,8 @@ export const applyFileSystemMagicResolution = (
18
18
  throw e
19
19
  }
20
20
  }
21
+ fileStat = fileStat === undefined ? fileStatOrNull(fileUrl) : fileStat
21
22
 
22
- const fileStat = fileStatOrNull(fileUrl)
23
23
  if (fileStat && fileStat.isFile()) {
24
24
  return {
25
25
  found: true,
@@ -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
  }
@@ -22,9 +22,10 @@ import {
22
22
  createPackagePathNotExportedError,
23
23
  createInvalidPackageTargetError,
24
24
  } from "./errors.js"
25
+ import { readCustomConditionsFromProcessArgs } from "./custom_conditions.js"
25
26
 
26
27
  export const applyNodeEsmResolution = ({
27
- conditions = ["node", "import"],
28
+ conditions = [...readCustomConditionsFromProcessArgs(), "node", "import"],
28
29
  parentUrl,
29
30
  specifier,
30
31
  lookupPackageScope = defaultLookupPackageScope,
@@ -92,6 +93,12 @@ const applyPackageSpecifierResolution = ({
92
93
  }
93
94
  try {
94
95
  const urlObject = new URL(specifier)
96
+ if (specifier.startsWith("node:")) {
97
+ return {
98
+ type: "node_builtin_specifier",
99
+ url: specifier,
100
+ }
101
+ }
95
102
  return {
96
103
  type: "absolute_specifier",
97
104
  url: urlObject.href,
@@ -746,7 +753,11 @@ const applyLegacySubpathResolution = ({
746
753
 
747
754
  const applyLegacyMainResolution = ({ conditions, packageUrl, packageJson }) => {
748
755
  for (const condition of conditions) {
749
- const resolved = mainLegacyResolvers[condition](packageJson, packageUrl)
756
+ const conditionResolver = mainLegacyResolvers[condition]
757
+ if (!conditionResolver) {
758
+ continue
759
+ }
760
+ const resolved = conditionResolver(packageJson, packageUrl)
750
761
  if (resolved) {
751
762
  return {
752
763
  type: resolved.type,
@@ -764,26 +775,39 @@ const applyLegacyMainResolution = ({ conditions, packageUrl, packageJson }) => {
764
775
  }
765
776
  }
766
777
  const mainLegacyResolvers = {
767
- import: ({ module, jsnext }) => {
768
- if (typeof module === "string") {
769
- return { type: "module", path: module }
778
+ import: (packageJson) => {
779
+ if (typeof packageJson.module === "string") {
780
+ return { type: "module", path: packageJson.module }
770
781
  }
771
- if (typeof jsnext === "string") {
772
- return { type: "jsnext", path: jsnext }
782
+ if (typeof packageJson.jsnext === "string") {
783
+ return { type: "jsnext", path: packageJson.jsnext }
784
+ }
785
+ if (typeof packageJson.main === "string") {
786
+ return { type: "main", path: packageJson.main }
773
787
  }
774
788
  return null
775
789
  },
776
- browser: ({ browser, module }, packageUrl) => {
790
+ browser: (packageJson, packageUrl) => {
777
791
  const browserMain =
778
- typeof browser === "string"
779
- ? browser
780
- : typeof browser === "object" && browser !== null
781
- ? browser["."]
792
+ typeof packageJson.browser === "string"
793
+ ? packageJson.browser
794
+ : typeof packageJson.browser === "object" &&
795
+ packageJson.browser !== null
796
+ ? packageJson.browser["."]
782
797
  : ""
783
798
  if (!browserMain) {
799
+ if (typeof packageJson.module === "string") {
800
+ return {
801
+ type: "module",
802
+ path: packageJson.module,
803
+ }
804
+ }
784
805
  return null
785
806
  }
786
- if (typeof module !== "string" || module === browserMain) {
807
+ if (
808
+ typeof packageJson.module !== "string" ||
809
+ packageJson.module === browserMain
810
+ ) {
787
811
  return {
788
812
  type: "browser",
789
813
  path: browserMain,
@@ -798,7 +822,7 @@ const mainLegacyResolvers = {
798
822
  ) {
799
823
  return {
800
824
  type: "module",
801
- path: module,
825
+ path: packageJson.module,
802
826
  }
803
827
  }
804
828
  return {
@@ -806,11 +830,11 @@ const mainLegacyResolvers = {
806
830
  path: browserMain,
807
831
  }
808
832
  },
809
- node: ({ main }) => {
810
- if (typeof main === "string") {
833
+ node: (packageJson) => {
834
+ if (typeof packageJson.main === "string") {
811
835
  return {
812
836
  type: "main",
813
- path: main,
837
+ path: packageJson.main,
814
838
  }
815
839
  }
816
840
  return null