@jsenv/node-esm-resolution 0.0.1 → 0.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/node-esm-resolution",
3
- "version": "0.0.1",
3
+ "version": "0.0.5",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,7 +8,8 @@
8
8
  "directory": "packages/node-esm-resolution"
9
9
  },
10
10
  "publishConfig": {
11
- "access": "public"
11
+ "access": "public",
12
+ "registry": "https://registry.npmjs.org"
12
13
  },
13
14
  "type": "module",
14
15
  "exports": {
@@ -22,5 +23,8 @@
22
23
  "/dist/",
23
24
  "/src/",
24
25
  "/main.js"
25
- ]
26
- }
26
+ ],
27
+ "scripts": {
28
+ "test": "node ./test/test.mjs"
29
+ }
30
+ }
@@ -26,7 +26,7 @@ export const determineModuleSystem = (
26
26
  return "commonjs"
27
27
  }
28
28
  if (extension === ".json") {
29
- return "json"
29
+ return "url"
30
30
  }
31
31
  if (ambiguousExtensions.includes(extension)) {
32
32
  const packageUrl = lookupPackageScope(url)
@@ -39,7 +39,8 @@ export const determineModuleSystem = (
39
39
  }
40
40
  return "commonjs"
41
41
  }
42
- throw new Error("unsupported file extension")
42
+ return "url"
43
+ // throw new Error(`unsupported file extension (${extension})`)
43
44
  }
44
45
 
45
46
  const extensionFromUrl = (url) => {
@@ -1,10 +1,10 @@
1
1
  import { existsSync } from "node:fs"
2
2
 
3
- import { filesystemRootUrl, getParentUrl, asDirectoryUrl } from "./url_utils.js"
3
+ import { getParentUrl, asDirectoryUrl } from "./url_utils.js"
4
4
 
5
5
  export const lookupPackageScope = (url) => {
6
6
  let scopeUrl = asDirectoryUrl(url)
7
- while (scopeUrl !== filesystemRootUrl) {
7
+ while (scopeUrl !== "file:///") {
8
8
  if (scopeUrl.endsWith("node_modules/")) {
9
9
  return null
10
10
  }
@@ -14,7 +14,7 @@ import { existsSync, readFileSync } from "node:fs"
14
14
  import { isSpecifierForNodeBuiltin } from "./node_builtin_specifiers.js"
15
15
  import { lookupPackageScope } from "./lookup_package_scope.js"
16
16
  import { readPackageJson } from "./read_package_json.js"
17
- import { filesystemRootUrl, getParentUrl, isValidUrl } from "./url_utils.js"
17
+ import { getParentUrl, isValidUrl } from "./url_utils.js"
18
18
  import {
19
19
  createInvalidModuleSpecifierError,
20
20
  createModuleNotFoundError,
@@ -104,7 +104,11 @@ const applyPackageSpecifierResolution = ({
104
104
  }
105
105
  }
106
106
 
107
- const applyBrowserFieldResolution = ({ conditions, parentUrl, specifier }) => {
107
+ const applyBrowserFieldResolution = ({
108
+ conditions,
109
+ parentUrl,
110
+ packageSpecifier,
111
+ }) => {
108
112
  const browserCondition = conditions.includes("browser")
109
113
  if (!browserCondition) {
110
114
  return null
@@ -125,22 +129,24 @@ const applyBrowserFieldResolution = ({ conditions, parentUrl, specifier }) => {
125
129
  return null
126
130
  }
127
131
  let url
128
- if (specifier.startsWith(".")) {
129
- const specifierUrl = new URL(specifier, parentUrl).href
130
- const specifierRelativeToPackage = specifierUrl.slice(packageUrl.length)
131
- const specifierRelativeNotation = `./${specifierRelativeToPackage}`
132
- const browserMapping = browser[specifierRelativeNotation]
132
+ if (packageSpecifier.startsWith(".")) {
133
+ const packageSpecifierUrl = new URL(packageSpecifier, parentUrl).href
134
+ const packageSpecifierRelativeUrl = packageSpecifierUrl.slice(
135
+ packageUrl.length,
136
+ )
137
+ const packageSpecifierRelativeNotation = `./${packageSpecifierRelativeUrl}`
138
+ const browserMapping = browser[packageSpecifierRelativeNotation]
133
139
  if (typeof browserMapping === "string") {
134
140
  url = new URL(browserMapping, packageUrl).href
135
141
  } else if (browserMapping === false) {
136
- url = `file:///@ignore/${specifierUrl.slice("file:///")}`
142
+ url = `file:///@ignore/${packageSpecifierUrl.slice("file:///")}`
137
143
  }
138
144
  } else {
139
- const browserMapping = browser[specifier]
145
+ const browserMapping = browser[packageSpecifier]
140
146
  if (typeof browserMapping === "string") {
141
147
  url = new URL(browserMapping, packageUrl).href
142
148
  } else if (browserMapping === false) {
143
- url = `file:///@ignore/${specifier}`
149
+ url = `file:///@ignore/${packageSpecifier}`
144
150
  }
145
151
  }
146
152
  if (url) {
@@ -238,7 +244,7 @@ const applyPackageResolve = ({ conditions, parentUrl, packageSpecifier }) => {
238
244
  return selfResolution
239
245
  }
240
246
  let currentUrl = parentUrl
241
- while (currentUrl !== filesystemRootUrl) {
247
+ while (currentUrl !== "file:///") {
242
248
  const packageUrl = new URL(`node_modules/${packageName}/`, currentUrl).href
243
249
  if (!existsSync(new URL(packageUrl))) {
244
250
  currentUrl = getParentUrl(currentUrl)
package/src/url_utils.js CHANGED
@@ -1,6 +1,3 @@
1
- export const filesystemRootUrl =
2
- process.platform === "win32" ? `file///${process.cwd()[0]}:/` : "file:///"
3
-
4
1
  export const asDirectoryUrl = (url) => {
5
2
  const { pathname } = new URL(url)
6
3
  if (pathname.endsWith("/")) {
@@ -10,6 +7,28 @@ export const asDirectoryUrl = (url) => {
10
7
  }
11
8
 
12
9
  export const getParentUrl = (url) => {
10
+ if (url.startsWith("file://")) {
11
+ // With node.js new URL('../', 'file:///C:/').href
12
+ // returns "file:///C:/" instead of "file:///"
13
+ const ressource = url.slice("file://".length)
14
+ const slashLastIndex = ressource.lastIndexOf("/")
15
+ if (slashLastIndex === -1) {
16
+ return url
17
+ }
18
+ const lastCharIndex = ressource.length - 1
19
+ if (slashLastIndex === lastCharIndex) {
20
+ const slashBeforeLastIndex = ressource.lastIndexOf(
21
+ "/",
22
+ slashLastIndex - 1,
23
+ )
24
+ if (slashBeforeLastIndex === -1) {
25
+ return url
26
+ }
27
+ return `file://${ressource.slice(0, slashBeforeLastIndex + 1)}`
28
+ }
29
+
30
+ return `file://${ressource.slice(0, slashLastIndex + 1)}`
31
+ }
13
32
  return new URL(url.endsWith("/") ? "../" : "./", url).href
14
33
  }
15
34