@jsenv/node-esm-resolution 0.0.7 → 0.1.0

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,9 @@
1
1
  export { applyNodeEsmResolution } from "./src/node_esm_resolution.js"
2
2
  export { determineModuleSystem } from "./src/determine_module_system.js"
3
- export { applyFileSystemMagicResolution } from "./src/filesystem_magic_resolution.js"
3
+ export {
4
+ applyFileSystemMagicResolution,
5
+ getExtensionsToTry,
6
+ } from "./src/filesystem_magic_resolution.js"
4
7
  export { defaultLookupPackageScope } from "./src/default_lookup_package_scope.js"
5
8
  export { defaultReadPackageJson } from "./src/default_read_package_json.js"
9
+ 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.7",
3
+ "version": "0.1.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,6 +25,6 @@
25
25
  "/main.js"
26
26
  ],
27
27
  "scripts": {
28
- "test": "node ./test/test.mjs"
28
+ "test": "node --conditions=development ./test/test.mjs"
29
29
  }
30
30
  }
@@ -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
+ }
@@ -1,10 +1,10 @@
1
1
  import { statSync } from "node:fs"
2
2
 
3
- import { urlToFilename } from "./url_utils.js"
3
+ import { urlToFilename, urlToExtension } 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,
@@ -69,3 +69,19 @@ export const applyFileSystemMagicResolution = (
69
69
  lastENOENTError,
70
70
  }
71
71
  }
72
+
73
+ export const getExtensionsToTry = (magicExtensions, importer) => {
74
+ if (!magicExtensions) {
75
+ return []
76
+ }
77
+ const extensionsSet = new Set()
78
+ magicExtensions.forEach((magicExtension) => {
79
+ if (magicExtension === "inherit") {
80
+ const importerExtension = urlToExtension(importer)
81
+ extensionsSet.add(importerExtension)
82
+ } else {
83
+ extensionsSet.add(magicExtension)
84
+ }
85
+ })
86
+ return Array.from(extensionsSet.values())
87
+ }
@@ -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,
@@ -771,6 +782,9 @@ const mainLegacyResolvers = {
771
782
  if (typeof packageJson.jsnext === "string") {
772
783
  return { type: "jsnext", path: packageJson.jsnext }
773
784
  }
785
+ if (typeof packageJson.main === "string") {
786
+ return { type: "main", path: packageJson.main }
787
+ }
774
788
  return null
775
789
  },
776
790
  browser: (packageJson, packageUrl) => {
package/src/url_utils.js CHANGED
@@ -54,3 +54,12 @@ export const urlToFilename = (url) => {
54
54
  : pathnameBeforeLastSlash.slice(slashLastIndex + 1)
55
55
  return filename
56
56
  }
57
+
58
+ export const urlToExtension = (url) => {
59
+ const filename = urlToFilename(url)
60
+ const dotLastIndex = filename.lastIndexOf(".")
61
+ if (dotLastIndex === -1) return ""
62
+ // if (dotLastIndex === pathname.length - 1) return ""
63
+ const extension = filename.slice(dotLastIndex)
64
+ return extension
65
+ }