@jsenv/node-esm-resolution 0.0.2 → 0.0.4

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.2",
3
+ "version": "0.0.4",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -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) => {
package/src/url_utils.js CHANGED
@@ -7,6 +7,28 @@ export const asDirectoryUrl = (url) => {
7
7
  }
8
8
 
9
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
+ }
10
32
  return new URL(url.endsWith("/") ? "../" : "./", url).href
11
33
  }
12
34