bare-module-traverse 1.5.1 → 1.7.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
@@ -78,6 +78,11 @@ dependency = {
78
78
  source: 'string' | Buffer, // Source as returned by `readModule()`
79
79
  imports: {
80
80
  // See https://github.com/holepunchto/bare-module#imports
81
+ },
82
+ lexer: {
83
+ imports: [
84
+ // See https://github.com/holepunchto/bare-module-lexer#api
85
+ ]
81
86
  }
82
87
  }
83
88
  ```
@@ -158,7 +163,7 @@ next.value = {
158
163
 
159
164
  ```js
160
165
  next.value = {
161
- children: URL
166
+ children: Generator
162
167
  }
163
168
  ```
164
169
 
@@ -166,7 +171,18 @@ next.value = {
166
171
 
167
172
  ```js
168
173
  next.value = {
169
- dependency: URL
174
+ dependency: {
175
+ url: URL,
176
+ source: 'string' | Buffer,
177
+ imports: {
178
+ // See https://github.com/holepunchto/bare-module#imports
179
+ },
180
+ lexer: {
181
+ imports: [
182
+ // See https://github.com/holepunchto/bare-module-lexer#api
183
+ ]
184
+ }
185
+ }
170
186
  }
171
187
  ```
172
188
 
package/index.d.ts CHANGED
@@ -12,6 +12,9 @@ interface Dependency {
12
12
  url: URL
13
13
  source: string | Buffer
14
14
  imports: ImportsMap
15
+ lexer: {
16
+ imports: Import[]
17
+ }
15
18
  }
16
19
 
17
20
  interface TraverseOptions extends ResolveOptions {
package/index.js CHANGED
@@ -123,7 +123,10 @@ exports.module = function* (
123
123
  source = yield { module: url }
124
124
 
125
125
  if (source === null) {
126
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${url.href}'`)
126
+ throw errors.MODULE_NOT_FOUND(
127
+ `Cannot find module '${url.href}'`,
128
+ url.href
129
+ )
127
130
  }
128
131
  }
129
132
 
@@ -177,9 +180,13 @@ exports.module = function* (
177
180
  }
178
181
  }
179
182
 
180
- yield* exports.imports(url, source, imports, artifacts, visited, opts)
183
+ const lexer = { imports: [] }
184
+
185
+ yield* exports.imports(url, source, imports, artifacts, lexer, visited, opts)
181
186
 
182
- yield { dependency: { url, source, imports: compressImportsMap(imports) } }
187
+ yield {
188
+ dependency: { url, source, imports: compressImportsMap(imports), lexer }
189
+ }
183
190
 
184
191
  return true
185
192
  }
@@ -192,7 +199,7 @@ exports.package = function* (url, source, artifacts, visited, opts = {}) {
192
199
  const info = JSON.parse(source)
193
200
 
194
201
  if (info) {
195
- yield { dependency: { url, source, imports: {} } }
202
+ yield { dependency: { url, source, imports: {}, lexer: { imports: [] } } }
196
203
 
197
204
  if (info.addon) {
198
205
  yield { children: exports.prebuilds(url, artifacts, visited, opts) }
@@ -246,7 +253,14 @@ exports.preresolved = function* (
246
253
  }
247
254
  }
248
255
 
249
- yield { dependency: { url, source, imports: compressImportsMap(imports) } }
256
+ yield {
257
+ dependency: {
258
+ url,
259
+ source,
260
+ imports: compressImportsMap(imports),
261
+ lexer: { imports: [] }
262
+ }
263
+ }
250
264
 
251
265
  return true
252
266
  }
@@ -256,6 +270,7 @@ exports.imports = function* (
256
270
  source,
257
271
  imports,
258
272
  artifacts,
273
+ lexer,
259
274
  visited,
260
275
  opts = {}
261
276
  ) {
@@ -298,6 +313,8 @@ exports.imports = function* (
298
313
  })
299
314
  }
300
315
 
316
+ lexer.imports.push(entry)
317
+
301
318
  queue.push({ entry, specifier, condition })
302
319
  }
303
320
 
@@ -307,6 +324,7 @@ exports.imports = function* (
307
324
  matchedConditions.push(condition)
308
325
 
309
326
  const resolver = resolve(entry, parentURL, { ...opts, matchedConditions })
327
+ const candidates = []
310
328
 
311
329
  let next = resolver.next()
312
330
  let resolutions = 0
@@ -319,6 +337,8 @@ exports.imports = function* (
319
337
  } else {
320
338
  const url = value.resolution
321
339
 
340
+ candidates.push(url)
341
+
322
342
  let resolved = false
323
343
 
324
344
  if (
@@ -392,18 +412,34 @@ exports.imports = function* (
392
412
  matchedConditions.pop()
393
413
 
394
414
  if (resolutions === 0) {
415
+ let message = `Cannot find ${condition === 'default' ? 'module' : condition} '${specifier}' imported from '${parentURL.href}'`
416
+
417
+ if (candidates.length > 0) {
418
+ message += '\nCandidates:'
419
+ message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
420
+ }
421
+
395
422
  switch (condition) {
396
423
  case 'addon':
397
424
  throw errors.ADDON_NOT_FOUND(
398
- `Cannot find addon '${specifier}' imported from '${parentURL.href}'`
425
+ message,
426
+ specifier,
427
+ parentURL,
428
+ candidates
399
429
  )
400
430
  case 'asset':
401
431
  throw errors.ASSET_NOT_FOUND(
402
- `Cannot find asset '${specifier}' imported from '${parentURL.href}'`
432
+ message,
433
+ specifier,
434
+ parentURL,
435
+ candidates
403
436
  )
404
437
  default:
405
438
  throw errors.MODULE_NOT_FOUND(
406
- `Cannot find module '${specifier}' imported from '${parentURL.href}'`
439
+ message,
440
+ specifier,
441
+ parentURL,
442
+ candidates
407
443
  )
408
444
  }
409
445
  }
package/lib/errors.js CHANGED
@@ -12,28 +12,46 @@ module.exports = class ModuleTraverseError extends Error {
12
12
  return 'ModuleTraverseError'
13
13
  }
14
14
 
15
- static MODULE_NOT_FOUND(msg) {
16
- return new ModuleTraverseError(
15
+ static MODULE_NOT_FOUND(msg, specifier, referrer = null, candidates = []) {
16
+ const err = new ModuleTraverseError(
17
17
  msg,
18
18
  'MODULE_NOT_FOUND',
19
19
  ModuleTraverseError.MODULE_NOT_FOUND
20
20
  )
21
+
22
+ err.specifier = specifier
23
+ err.referrer = referrer
24
+ err.candidates = candidates
25
+
26
+ return err
21
27
  }
22
28
 
23
- static ADDON_NOT_FOUND(msg) {
24
- return new ModuleTraverseError(
29
+ static ADDON_NOT_FOUND(msg, specifier, referrer = null, candidates = []) {
30
+ const err = new ModuleTraverseError(
25
31
  msg,
26
32
  'ADDON_NOT_FOUND',
27
33
  ModuleTraverseError.ADDON_NOT_FOUND
28
34
  )
35
+
36
+ err.specifier = specifier
37
+ err.referrer = referrer
38
+ err.candidates = candidates
39
+
40
+ return err
29
41
  }
30
42
 
31
- static ASSET_NOT_FOUND(msg) {
32
- return new ModuleTraverseError(
43
+ static ASSET_NOT_FOUND(msg, specifier, referrer = null, candidates = []) {
44
+ const err = new ModuleTraverseError(
33
45
  msg,
34
46
  'ASSET_NOT_FOUND',
35
47
  ModuleTraverseError.ASSET_NOT_FOUND
36
48
  )
49
+
50
+ err.specifier = specifier
51
+ err.referrer = referrer
52
+ err.candidates = candidates
53
+
54
+ return err
37
55
  }
38
56
 
39
57
  static INVALID_IMPORTS_MAP(msg) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "1.5.1",
3
+ "version": "1.7.0",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {