bare-module-traverse 2.0.1 → 2.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/README.md CHANGED
@@ -59,6 +59,11 @@ Options include:
59
59
  ```js
60
60
  options = {
61
61
  defaultType: constants.SCRIPT,
62
+ aliases: {
63
+ // Map an extension to a supported extension, e.g. `'.ts': '.js'`. The
64
+ // aliased extension is used for module type detection, so `readModule()`
65
+ // must return source compatible with that type.
66
+ },
62
67
  resolve: resolve.default
63
68
  }
64
69
  ```
package/index.d.ts CHANGED
@@ -17,8 +17,20 @@ interface Dependency {
17
17
  }
18
18
  }
19
19
 
20
+ type AliasableExtension =
21
+ | '.js'
22
+ | '.cjs'
23
+ | '.mjs'
24
+ | '.json'
25
+ | '.bundle'
26
+ | '.bare'
27
+ | '.node'
28
+ | '.bin'
29
+ | '.txt'
30
+
20
31
  interface TraverseOptions extends ResolveOptions {
21
32
  defaultType?: number
33
+ aliases?: Record<string, AliasableExtension>
22
34
  resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
23
35
  }
24
36
 
package/index.js CHANGED
@@ -110,7 +110,7 @@ exports.constants = constants
110
110
  exports.resolve = resolve
111
111
 
112
112
  exports.module = function* (url, source, attributes, artifacts, visited, opts = {}) {
113
- const { resolutions = null, defaultType = constants.SCRIPT } = opts
113
+ const { resolutions = null, defaultType = constants.SCRIPT, aliases = null } = opts
114
114
 
115
115
  if (visited.has(url.href)) return false
116
116
 
@@ -195,15 +195,18 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
195
195
  const match = url.pathname.match(/\.[a-z]+$/)
196
196
 
197
197
  if (match !== null) {
198
- const [extension] = match
198
+ let [extension] = match
199
+
200
+ if (aliases !== null && extension in aliases) extension = aliases[extension]
199
201
 
200
202
  switch (extension) {
201
- case '.js':
203
+ case '.js': {
202
204
  const isESM =
203
205
  defaultType === constants.MODULE || (info !== null && info.type === 'module')
204
206
 
205
207
  type = isESM ? constants.MODULE : constants.SCRIPT
206
208
  break
209
+ }
207
210
  case '.cjs':
208
211
  type = constants.SCRIPT
209
212
  break
@@ -6,11 +6,11 @@ module.exports = function (entry, parentURL, opts = {}) {
6
6
  const { host = runtime.host, hosts = [host] } = opts
7
7
 
8
8
  let extensions
9
- let conditions
9
+ let conditions = hosts.map((host) => ['node', ...host.split('-')])
10
10
 
11
11
  if (entry.type & lex.constants.ADDON) {
12
12
  extensions = ['.node']
13
- conditions = hosts.map((host) => ['node', 'addon', ...host.split('-')])
13
+ conditions = conditions.map((conditions) => ['addon', ...conditions])
14
14
 
15
15
  return resolve.addon(entry.specifier || '.', parentURL, {
16
16
  ...opts,
@@ -21,14 +21,12 @@ module.exports = function (entry, parentURL, opts = {}) {
21
21
  }
22
22
 
23
23
  if (entry.type & lex.constants.ASSET) {
24
- conditions = ['node', 'asset']
24
+ conditions = conditions.map((conditions) => ['asset', ...conditions])
25
25
  } else if (entry.type & lex.constants.REQUIRE) {
26
26
  extensions = ['.js', '.json', '.node']
27
- conditions = ['node', 'require']
27
+ conditions = conditions.map((conditions) => ['require', ...conditions])
28
28
  } else if (entry.type & lex.constants.IMPORT) {
29
- conditions = ['node', 'import']
30
- } else {
31
- conditions = ['node']
29
+ conditions = conditions.map((conditions) => ['import', ...conditions])
32
30
  }
33
31
 
34
32
  return resolve.module(entry.specifier, parentURL, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {
@@ -37,10 +37,11 @@
37
37
  "lib"
38
38
  ],
39
39
  "scripts": {
40
- "test": "npm run lint && npm run test:bare && npm run test:node",
41
- "test:bare": "bare test.js",
42
- "test:node": "node test.js",
43
- "lint": "prettier . --check"
40
+ "format": "prettier --write . && lunte --fix",
41
+ "lint": "prettier --check . && lunte",
42
+ "test": "npm run test:bare && npm run test:node",
43
+ "test:bare": "brittle-bare --coverage test.js",
44
+ "test:node": "brittle-node --coverage test.js"
44
45
  },
45
46
  "repository": {
46
47
  "type": "git",
@@ -61,6 +62,7 @@
61
62
  "bare-buffer": "^3.0.2",
62
63
  "bare-url": "^2.1.3",
63
64
  "brittle": "^3.2.1",
65
+ "lunte": "^1.8.0",
64
66
  "prettier": "^3.4.2",
65
67
  "prettier-config-holepunch": "^2.0.0"
66
68
  },