bare-module-traverse 1.1.0 → 1.2.1

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
@@ -17,8 +17,8 @@ function readModule (url) {
17
17
  // Read `url` if it exists, otherwise `null`
18
18
  }
19
19
 
20
- function listPrefix (url) {
21
- // Return a list of URLs that have `url` as a prefix. The list may be empty.
20
+ function * listPrefix (url) {
21
+ // Yield URLs that have `url` as a prefix. The list may be empty.
22
22
  }
23
23
 
24
24
  for (const dependency of traverse(new URL('file:///directory/file.js'), readModule, listPrefix)) {
@@ -35,8 +35,8 @@ async function readModule (url) {
35
35
  // Read `url` if it exists, otherwise `null`
36
36
  }
37
37
 
38
- async function listPrefix (url) {
39
- // Return a list of URLs that have `url` as a prefix. The list may be empty.
38
+ async function * listPrefix (url) {
39
+ // Yield URLs that have `url` as a prefix. The list may be empty.
40
40
  }
41
41
 
42
42
  for await (const dependency of traverse(new URL('file:///directory/file.js'), readModule, listPrefix)) {
@@ -48,7 +48,7 @@ for await (const dependency of traverse(new URL('file:///directory/file.js'), re
48
48
 
49
49
  #### `const dependencies = traverse(url[, options], readModule[, listPrefix])`
50
50
 
51
- Traverse the module graph rooted at `url`, which must be a WHATWG `URL` instance. `readModule` is called with a `URL` instance for every module to be read and must either return the module source, if it exists, or `null`. `listPrefix` is called with a `URL` instance of every prefix to be listed and must return a list of `URL` instances that have the specified `URL` as a prefix. If not provided, prefixes won't be traversed. If one or both of `readModule` or `listPrefix` returns a promise, synchronous iteration is not supported.
51
+ Traverse the module graph rooted at `url`, which must be a WHATWG `URL` instance. `readModule` is called with a `URL` instance for every module to be read and must either return the module source, if it exists, or `null`. `listPrefix` is called with a `URL` instance of every prefix to be listed and must return a list of `URL` instances that have the specified `URL` as a prefix. If not provided, prefixes won't be traversed. If `readModule` returns a promise or `listPrefix` returns a promise generator, synchronous iteration is not supported.
52
52
 
53
53
  Options include:
54
54
 
@@ -76,7 +76,7 @@ Synchronously iterate the module graph. Each yielded dependency has the followin
76
76
 
77
77
  #### `for await (const dependency of dependencies)`
78
78
 
79
- Asynchronously iterate the module graph. If one or both of `readModule` or `listPrefix` returns promises, these will be awaited. The same comments as `for (const dependency of dependencies)` apply.
79
+ Asynchronously iterate the module graph. If `readModule` returns a promise or `listPrefix` returns a promise generator, these will be awaited. The same comments as `for (const dependency of dependencies)` apply.
80
80
 
81
81
  ### Resolution
82
82
 
package/index.js CHANGED
@@ -32,7 +32,13 @@ module.exports = exports = function traverse (entry, opts, readModule, listPrefi
32
32
  if (value.module) {
33
33
  next = generator.next(readModule(value.module))
34
34
  } else if (value.prefix) {
35
- next = generator.next(listPrefix(value.prefix))
35
+ const result = []
36
+
37
+ for (const url of listPrefix(value.prefix)) {
38
+ result.push(url)
39
+ }
40
+
41
+ next = generator.next(result)
36
42
  } else {
37
43
  if (value.children) queue.push(value.children)
38
44
  else yield value.dependency
@@ -61,7 +67,13 @@ module.exports = exports = function traverse (entry, opts, readModule, listPrefi
61
67
  if (value.module) {
62
68
  next = generator.next(await readModule(value.module))
63
69
  } else if (value.prefix) {
64
- next = generator.next(await listPrefix(value.prefix))
70
+ const result = []
71
+
72
+ for await (const url of listPrefix(value.prefix)) {
73
+ result.push(url)
74
+ }
75
+
76
+ next = generator.next(result)
65
77
  } else {
66
78
  if (value.children) queue.push(value.children)
67
79
  else yield value.dependency
@@ -0,0 +1,38 @@
1
+ const lex = require('bare-module-lexer')
2
+ const resolve = require('../resolve')
3
+
4
+ module.exports = function (entry, parentURL, opts = {}) {
5
+ const runtime = require('#runtime')
6
+
7
+ const {
8
+ platform = runtime.platform,
9
+ arch = runtime.arch,
10
+ simulator = false
11
+ } = opts
12
+
13
+ let extensions
14
+ let conditions = ['bare', 'node', platform, arch]
15
+
16
+ if (simulator) conditions = [...conditions, 'simulator']
17
+
18
+ if (entry.type & lex.constants.ADDON) {
19
+ extensions = ['.bare', '.node']
20
+ conditions = ['addon', ...conditions]
21
+
22
+ return resolve.addon(entry.specifier, parentURL, { extensions, conditions, ...opts })
23
+ }
24
+
25
+ if (entry.type & lex.constants.ASSET) {
26
+ conditions = ['asset', ...conditions]
27
+ } else {
28
+ extensions = ['.js', '.cjs', '.mjs', '.json', '.bare', '.node']
29
+
30
+ if (entry.type & lex.constants.REQUIRE) {
31
+ conditions = ['require', ...conditions]
32
+ } else if (entry.type & lex.constants.IMPORT) {
33
+ conditions = ['import', ...conditions]
34
+ }
35
+ }
36
+
37
+ return resolve.module(entry.specifier, parentURL, { extensions, conditions, ...opts })
38
+ }
@@ -0,0 +1,10 @@
1
+ const lex = require('bare-module-lexer')
2
+ const resolve = require('../resolve')
3
+
4
+ module.exports = function (entry, parentURL, opts) {
5
+ if (entry.type & lex.constants.ADDON) {
6
+ return resolve.addon(entry.specifier, parentURL, opts)
7
+ }
8
+
9
+ return resolve.module(entry.specifier, parentURL, opts)
10
+ }
@@ -0,0 +1,22 @@
1
+ const lex = require('bare-module-lexer')
2
+ const resolve = require('../resolve')
3
+
4
+ module.exports = function (entry, parentURL, opts) {
5
+ let extensions
6
+ let conditions
7
+
8
+ if (entry.type & lex.constants.ADDON) {
9
+ extensions = ['.node']
10
+
11
+ return resolve.addon(entry.specifier, parentURL, { extensions, conditions, ...opts })
12
+ }
13
+
14
+ if (entry.type & lex.constants.REQUIRE) {
15
+ extensions = ['.js', '.json', '.node']
16
+ conditions = ['node', 'require']
17
+ } else if (entry.type & lex.constants.IMPORT) {
18
+ conditions = ['node', 'import']
19
+ }
20
+
21
+ return resolve.module(entry.specifier, parentURL, { extensions, conditions, ...opts })
22
+ }
package/lib/resolve.js CHANGED
@@ -1,68 +1,6 @@
1
- const lex = require('bare-module-lexer')
2
-
3
1
  exports.module = require('bare-module-resolve').module
4
2
  exports.addon = require('bare-addon-resolve').addon
5
3
 
6
- exports.default = function resolve (entry, parentURL, opts) {
7
- if (entry.type & lex.constants.ADDON) {
8
- return exports.addon(entry.specifier, parentURL, opts)
9
- }
10
-
11
- return exports.module(entry.specifier, parentURL, opts)
12
- }
13
-
14
- exports.bare = function resolve (entry, parentURL, opts = {}) {
15
- const runtime = require('#runtime')
16
-
17
- const {
18
- platform = runtime.platform,
19
- arch = runtime.arch,
20
- simulator = false
21
- } = opts
22
-
23
- let extensions
24
- let conditions = ['bare', 'node', platform, arch]
25
-
26
- if (simulator) conditions = [...conditions, 'simulator']
27
-
28
- if (entry.type & lex.constants.ADDON) {
29
- extensions = ['.bare', '.node']
30
- conditions = ['addon', ...conditions]
31
-
32
- return exports.addon(entry.specifier, parentURL, { extensions, conditions, ...opts })
33
- }
34
-
35
- if (entry.type & lex.constants.ASSET) {
36
- conditions = ['asset', ...conditions]
37
- } else {
38
- extensions = ['.js', '.cjs', '.mjs', '.json', '.bare', '.node']
39
-
40
- if (entry.type & lex.constants.REQUIRE) {
41
- conditions = ['require', ...conditions]
42
- } else if (entry.type & lex.constants.IMPORT) {
43
- conditions = ['import', ...conditions]
44
- }
45
- }
46
-
47
- return exports.module(entry.specifier, parentURL, { extensions, conditions, ...opts })
48
- }
49
-
50
- exports.node = function resolve (entry, parentURL, opts) {
51
- let extensions
52
- let conditions
53
-
54
- if (entry.type & lex.constants.ADDON) {
55
- extensions = ['.node']
56
-
57
- return exports.addon(entry.specifier, parentURL, { extensions, conditions, ...opts })
58
- }
59
-
60
- if (entry.type & lex.constants.REQUIRE) {
61
- extensions = ['.js', '.json', '.node']
62
- conditions = ['node', 'require']
63
- } else if (entry.type & lex.constants.IMPORT) {
64
- conditions = ['node', 'import']
65
- }
66
-
67
- return exports.module(entry.specifier, parentURL, { extensions, conditions, ...opts })
68
- }
4
+ exports.default = require('./resolve/default')
5
+ exports.bare = require('./resolve/bare')
6
+ exports.node = require('./resolve/node')
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": "./index.js",
7
7
  "./package": "./package.json",
8
- "./resolve": "./lib/resolve.js"
8
+ "./resolve": "./lib/resolve.js",
9
+ "./resolve/default": "./lib/resolve/default.js",
10
+ "./resolve/bare": "./lib/resolve/bare.js",
11
+ "./resolve/node": "./lib/resolve/node.js"
9
12
  },
10
13
  "files": [
11
14
  "index.js",