bare-module-traverse 1.0.0 → 1.0.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 +4 -1
- package/index.js +26 -9
- package/lib/errors.js +18 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -106,13 +106,16 @@ while (queue.length > 0) {
|
|
|
106
106
|
|
|
107
107
|
Options are the same as `traverse()` for all functions.
|
|
108
108
|
|
|
109
|
+
> [!WARNING]
|
|
110
|
+
> These functions are currently subject to change between minor releases. If using them directly, make sure to specify a tilde range (`~1.2.3`) when declaring the module dependency.
|
|
111
|
+
|
|
109
112
|
#### `const generator = traverse.module(url, source, artifacts, visited[, options])`
|
|
110
113
|
|
|
111
114
|
#### `const generator = traverse.package(url, source, artifacts, visited[, options])`
|
|
112
115
|
|
|
113
116
|
#### `const generator = traverse.preresolved(url, source, resolutions, artifacts, visited[, options])`
|
|
114
117
|
|
|
115
|
-
#### `const generator = traverse.imports(
|
|
118
|
+
#### `const generator = traverse.imports(parentURL, source, imports, artifacts, visited[, options])`
|
|
116
119
|
|
|
117
120
|
#### `const generator = traverse.assets(patterns, parentURL, artifacts, visited[, options])`
|
|
118
121
|
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const { lookupPackageScope } = require('bare-module-resolve')
|
|
2
2
|
const lex = require('bare-module-lexer')
|
|
3
3
|
const resolve = require('./lib/resolve')
|
|
4
|
+
const errors = require('./lib/errors')
|
|
4
5
|
|
|
5
6
|
module.exports = exports = function traverse (entry, opts, readModule, listPrefix) {
|
|
6
7
|
if (typeof opts === 'function') {
|
|
@@ -17,7 +18,7 @@ module.exports = exports = function traverse (entry, opts, readModule, listPrefi
|
|
|
17
18
|
* [Symbol.iterator] () {
|
|
18
19
|
const artifacts = createArtifacts()
|
|
19
20
|
|
|
20
|
-
const queue = [exports.module(entry,
|
|
21
|
+
const queue = [exports.module(entry, null, artifacts, new Set(), opts)]
|
|
21
22
|
|
|
22
23
|
while (queue.length > 0) {
|
|
23
24
|
const generator = queue.pop()
|
|
@@ -46,7 +47,7 @@ module.exports = exports = function traverse (entry, opts, readModule, listPrefi
|
|
|
46
47
|
async * [Symbol.asyncIterator] () {
|
|
47
48
|
const artifacts = createArtifacts()
|
|
48
49
|
|
|
49
|
-
const queue = [exports.module(entry,
|
|
50
|
+
const queue = [exports.module(entry, null, artifacts, new Set(), opts)]
|
|
50
51
|
|
|
51
52
|
while (queue.length > 0) {
|
|
52
53
|
const generator = queue.pop()
|
|
@@ -117,6 +118,14 @@ exports.module = function * (url, source, artifacts, visited, opts = {}) {
|
|
|
117
118
|
|
|
118
119
|
if (visited.has(url.href)) return false
|
|
119
120
|
|
|
121
|
+
if (source === null) {
|
|
122
|
+
source = yield { module: url }
|
|
123
|
+
|
|
124
|
+
if (source === null) {
|
|
125
|
+
throw errors.MODULE_NOT_FOUND(`Cannot find module '${url.href}'`)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
120
129
|
visited.add(url.href)
|
|
121
130
|
|
|
122
131
|
if (resolutions) {
|
|
@@ -177,14 +186,11 @@ exports.preresolved = function * (url, source, resolutions, artifacts, visited,
|
|
|
177
186
|
|
|
178
187
|
if (typeof entry === 'string') {
|
|
179
188
|
const url = new URL(entry)
|
|
180
|
-
const source = yield { module: url }
|
|
181
|
-
|
|
182
|
-
if (source === null) continue
|
|
183
189
|
|
|
184
190
|
if (specifier === '#package') {
|
|
185
|
-
yield { children: exports.package(url,
|
|
191
|
+
yield { children: exports.package(url, null, artifacts, visited, opts) }
|
|
186
192
|
} else {
|
|
187
|
-
yield { children: exports.module(url,
|
|
193
|
+
yield { children: exports.module(url, null, artifacts, visited, opts) }
|
|
188
194
|
}
|
|
189
195
|
} else {
|
|
190
196
|
stack.unshift(...Object.values(entry))
|
|
@@ -197,13 +203,16 @@ exports.preresolved = function * (url, source, resolutions, artifacts, visited,
|
|
|
197
203
|
return true
|
|
198
204
|
}
|
|
199
205
|
|
|
200
|
-
exports.imports = function * (
|
|
206
|
+
exports.imports = function * (parentURL, source, imports, artifacts, visited, opts = {}) {
|
|
201
207
|
const { resolve = defaultResolve } = opts
|
|
202
208
|
|
|
209
|
+
let yielded = false
|
|
210
|
+
|
|
203
211
|
for (const entry of lex(source).imports) {
|
|
204
|
-
const resolver = resolve(entry,
|
|
212
|
+
const resolver = resolve(entry, parentURL, opts)
|
|
205
213
|
|
|
206
214
|
let next = resolver.next()
|
|
215
|
+
let resolved = false
|
|
207
216
|
|
|
208
217
|
while (next.done !== true) {
|
|
209
218
|
const value = next.value
|
|
@@ -232,13 +241,21 @@ exports.imports = function * (url, source, imports, artifacts, visited, opts = {
|
|
|
232
241
|
|
|
233
242
|
yield { children: exports.module(url, source, artifacts, visited, opts) }
|
|
234
243
|
|
|
244
|
+
resolved = yielded = true
|
|
245
|
+
|
|
235
246
|
break
|
|
236
247
|
}
|
|
237
248
|
|
|
238
249
|
next = resolver.next()
|
|
239
250
|
}
|
|
240
251
|
}
|
|
252
|
+
|
|
253
|
+
if (!resolved) {
|
|
254
|
+
throw errors.MODULE_NOT_FOUND(`Cannot find module '${entry.specifier}' imported from '${parentURL.href}'`)
|
|
255
|
+
}
|
|
241
256
|
}
|
|
257
|
+
|
|
258
|
+
return yielded
|
|
242
259
|
}
|
|
243
260
|
|
|
244
261
|
exports.assets = function * (patterns, parentURL, artifacts, visited, opts = {}) {
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = class ModuleTraverseError extends Error {
|
|
2
|
+
constructor (msg, code, fn = ModuleTraverseError) {
|
|
3
|
+
super(`${code}: ${msg}`)
|
|
4
|
+
this.code = code
|
|
5
|
+
|
|
6
|
+
if (Error.captureStackTrace) {
|
|
7
|
+
Error.captureStackTrace(this, fn)
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get name () {
|
|
12
|
+
return 'ModuleTraverseError'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static MODULE_NOT_FOUND (msg) {
|
|
16
|
+
return new ModuleTraverseError(msg, 'MODULE_NOT_FOUND', ModuleTraverseError.MODULE_NOT_FOUND)
|
|
17
|
+
}
|
|
18
|
+
}
|