@jsenv/node-esm-resolution 0.0.8 → 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.8",
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,
@@ -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,