bare-module-traverse 1.6.0 → 1.7.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
@@ -21,11 +21,7 @@ function* listPrefix(url) {
21
21
  // Yield URLs that have `url` as a prefix. The list may be empty.
22
22
  }
23
23
 
24
- for (const dependency of traverse(
25
- new URL('file:///directory/file.js'),
26
- readModule,
27
- listPrefix
28
- )) {
24
+ for (const dependency of traverse(new URL('file:///directory/file.js'), readModule, listPrefix)) {
29
25
  console.log(dependency)
30
26
  }
31
27
  ```
@@ -62,6 +58,7 @@ Options include:
62
58
 
63
59
  ```js
64
60
  options = {
61
+ defaultType: constants.SCRIPT,
65
62
  resolve: resolve.default
66
63
  }
67
64
  ```
@@ -78,6 +75,11 @@ dependency = {
78
75
  source: 'string' | Buffer, // Source as returned by `readModule()`
79
76
  imports: {
80
77
  // See https://github.com/holepunchto/bare-module#imports
78
+ },
79
+ lexer: {
80
+ imports: [
81
+ // See https://github.com/holepunchto/bare-module-lexer#api
82
+ ]
81
83
  }
82
84
  }
83
85
  ```
@@ -158,7 +160,7 @@ next.value = {
158
160
 
159
161
  ```js
160
162
  next.value = {
161
- children: URL
163
+ children: Generator
162
164
  }
163
165
  ```
164
166
 
@@ -166,7 +168,18 @@ next.value = {
166
168
 
167
169
  ```js
168
170
  next.value = {
169
- dependency: URL
171
+ dependency: {
172
+ url: URL,
173
+ source: 'string' | Buffer,
174
+ imports: {
175
+ // See https://github.com/holepunchto/bare-module#imports
176
+ },
177
+ lexer: {
178
+ imports: [
179
+ // See https://github.com/holepunchto/bare-module-lexer#api
180
+ ]
181
+ }
182
+ }
170
183
  }
171
184
  ```
172
185
 
package/index.d.ts CHANGED
@@ -12,9 +12,13 @@ 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 {
21
+ defaultType?: number
18
22
  resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
19
23
  }
20
24
 
@@ -58,6 +62,17 @@ declare namespace traverse {
58
62
  assets: URL[]
59
63
  }
60
64
 
65
+ export const constants: {
66
+ SCRIPT: number
67
+ MODULE: number
68
+ JSON: number
69
+ BUNDLE: number
70
+ ADDON: number
71
+ BINARY: number
72
+ TEXT: number
73
+ ASSET: number
74
+ }
75
+
61
76
  export function module(
62
77
  url: URL,
63
78
  source: string | Buffer,
package/index.js CHANGED
@@ -4,12 +4,18 @@ const lex = require('bare-module-lexer')
4
4
  const resolve = require('./lib/resolve')
5
5
  const errors = require('./lib/errors')
6
6
 
7
- module.exports = exports = function traverse(
8
- entry,
9
- opts,
10
- readModule,
11
- listPrefix
12
- ) {
7
+ const constants = {
8
+ SCRIPT: 1,
9
+ MODULE: 2,
10
+ JSON: 3,
11
+ BUNDLE: 4,
12
+ ADDON: 5,
13
+ BINARY: 6,
14
+ TEXT: 7,
15
+ ASSET: 8
16
+ }
17
+
18
+ module.exports = exports = function traverse(entry, opts, readModule, listPrefix) {
13
19
  if (typeof opts === 'function') {
14
20
  listPrefix = readModule
15
21
  readModule = opts
@@ -20,9 +26,7 @@ module.exports = exports = function traverse(
20
26
  *[Symbol.iterator]() {
21
27
  const artifacts = { addons: [], assets: [] }
22
28
 
23
- const queue = [
24
- exports.module(entry, null, {}, artifacts, new Set(), opts)
25
- ]
29
+ const queue = [exports.module(entry, null, {}, artifacts, new Set(), opts)]
26
30
 
27
31
  while (queue.length > 0) {
28
32
  const generator = queue.pop()
@@ -63,9 +67,7 @@ module.exports = exports = function traverse(
63
67
  async *[Symbol.asyncIterator]() {
64
68
  const artifacts = { addons: [], assets: [] }
65
69
 
66
- const queue = [
67
- exports.module(entry, null, {}, artifacts, new Set(), opts)
68
- ]
70
+ const queue = [exports.module(entry, null, {}, artifacts, new Set(), opts)]
69
71
 
70
72
  while (queue.length > 0) {
71
73
  const generator = queue.pop()
@@ -105,17 +107,11 @@ module.exports = exports = function traverse(
105
107
  }
106
108
  }
107
109
 
110
+ exports.constants = constants
108
111
  exports.resolve = resolve
109
112
 
110
- exports.module = function* (
111
- url,
112
- source,
113
- attributes,
114
- artifacts,
115
- visited,
116
- opts = {}
117
- ) {
118
- const { resolutions = null } = opts
113
+ exports.module = function* (url, source, attributes, artifacts, visited, opts = {}) {
114
+ const { resolutions = null, defaultType = constants.SCRIPT } = opts
119
115
 
120
116
  if (visited.has(url.href)) return false
121
117
 
@@ -123,36 +119,30 @@ exports.module = function* (
123
119
  source = yield { module: url }
124
120
 
125
121
  if (source === null) {
126
- throw errors.MODULE_NOT_FOUND(
127
- `Cannot find module '${url.href}'`,
128
- url.href
129
- )
122
+ throw errors.MODULE_NOT_FOUND(`Cannot find module '${url.href}'`, url.href)
130
123
  }
131
124
  }
132
125
 
133
126
  visited.add(url.href)
134
127
 
135
128
  if (resolutions) {
136
- if (
137
- yield* exports.preresolved(
138
- url,
139
- source,
140
- resolutions,
141
- artifacts,
142
- visited,
143
- opts
144
- )
145
- ) {
129
+ if (yield* exports.preresolved(url, source, resolutions, artifacts, visited, opts)) {
146
130
  return true
147
131
  }
148
132
  }
149
133
 
134
+ attributes = attributes || {}
135
+
150
136
  const imports = {}
151
137
 
138
+ let info = null
139
+
152
140
  for (const packageURL of lookupPackageScope(url, opts)) {
153
141
  const source = yield { module: packageURL }
154
142
 
155
143
  if (source !== null) {
144
+ info = JSON.parse(source)
145
+
156
146
  imports['#package'] = packageURL.href
157
147
 
158
148
  yield {
@@ -163,11 +153,7 @@ exports.module = function* (
163
153
  }
164
154
  }
165
155
 
166
- if (
167
- typeof attributes === 'object' &&
168
- attributes !== null &&
169
- typeof attributes.imports === 'string'
170
- ) {
156
+ if (typeof attributes.imports === 'string') {
171
157
  const url = new URL(attributes.imports)
172
158
 
173
159
  const source = yield { module: url }
@@ -180,9 +166,80 @@ exports.module = function* (
180
166
  }
181
167
  }
182
168
 
183
- yield* exports.imports(url, source, imports, artifacts, visited, opts)
169
+ let type = 0
170
+
171
+ if (typeof attributes.type === 'string') {
172
+ switch (attributes.type) {
173
+ case 'script':
174
+ type = constants.SCRIPT
175
+ break
176
+ case 'module':
177
+ type = constants.MODULE
178
+ break
179
+ case 'json':
180
+ type = constants.JSON
181
+ break
182
+ case 'bundle':
183
+ type = constants.BUNDLE
184
+ break
185
+ case 'addon':
186
+ type = constants.ADDON
187
+ break
188
+ case 'binary':
189
+ type = constants.BINARY
190
+ break
191
+ case 'text':
192
+ type = constants.TEXT
193
+ break
194
+ }
195
+ } else {
196
+ const match = url.pathname.match(/\.[a-z]+$/)
197
+
198
+ if (match !== null) {
199
+ const [extension] = match
200
+
201
+ switch (extension) {
202
+ case '.js':
203
+ const isESM =
204
+ defaultType === constants.MODULE || (info !== null && info.type === 'module')
205
+
206
+ type = isESM ? constants.MODULE : constants.SCRIPT
207
+ break
208
+ case '.cjs':
209
+ type = constants.SCRIPT
210
+ break
211
+ case '.mjs':
212
+ type = constants.MODULE
213
+ break
214
+ case '.json':
215
+ type = constants.JSON
216
+ break
217
+ case '.bundle':
218
+ type = constants.BUNDLE
219
+ break
220
+ case '.bare':
221
+ case '.node':
222
+ type = constants.ADDON
223
+ break
224
+ case '.bin':
225
+ type = constants.BINARY
226
+ break
227
+ case '.txt':
228
+ type = constants.TEXT
229
+ break
230
+ }
231
+ }
232
+ }
233
+
234
+ const lexer = { imports: [] }
184
235
 
185
- yield { dependency: { url, source, imports: compressImportsMap(imports) } }
236
+ if (type === constants.SCRIPT || constants.MODULE) {
237
+ yield* exports.imports(url, source, imports, artifacts, lexer, visited, opts)
238
+ }
239
+
240
+ yield {
241
+ dependency: { url, source, imports: compressImportsMap(imports), lexer }
242
+ }
186
243
 
187
244
  return true
188
245
  }
@@ -195,7 +252,7 @@ exports.package = function* (url, source, artifacts, visited, opts = {}) {
195
252
  const info = JSON.parse(source)
196
253
 
197
254
  if (info) {
198
- yield { dependency: { url, source, imports: {} } }
255
+ yield { dependency: { url, source, imports: {}, lexer: { imports: [] } } }
199
256
 
200
257
  if (info.addon) {
201
258
  yield { children: exports.prebuilds(url, artifacts, visited, opts) }
@@ -213,14 +270,7 @@ exports.package = function* (url, source, artifacts, visited, opts = {}) {
213
270
  return false
214
271
  }
215
272
 
216
- exports.preresolved = function* (
217
- url,
218
- source,
219
- resolutions,
220
- artifacts,
221
- visited,
222
- opts = {}
223
- ) {
273
+ exports.preresolved = function* (url, source, resolutions, artifacts, visited, opts = {}) {
224
274
  const imports = resolutions[url.href]
225
275
 
226
276
  if (typeof imports !== 'object' || imports === null) return false
@@ -249,19 +299,19 @@ exports.preresolved = function* (
249
299
  }
250
300
  }
251
301
 
252
- yield { dependency: { url, source, imports: compressImportsMap(imports) } }
302
+ yield {
303
+ dependency: {
304
+ url,
305
+ source,
306
+ imports: compressImportsMap(imports),
307
+ lexer: { imports: [] }
308
+ }
309
+ }
253
310
 
254
311
  return true
255
312
  }
256
313
 
257
- exports.imports = function* (
258
- parentURL,
259
- source,
260
- imports,
261
- artifacts,
262
- visited,
263
- opts = {}
264
- ) {
314
+ exports.imports = function* (parentURL, source, imports, artifacts, lexer, visited, opts = {}) {
265
315
  const {
266
316
  resolve = exports.resolve.default,
267
317
  builtinProtocol = 'builtin:',
@@ -301,6 +351,8 @@ exports.imports = function* (
301
351
  })
302
352
  }
303
353
 
354
+ lexer.imports.push(entry)
355
+
304
356
  queue.push({ entry, specifier, condition })
305
357
  }
306
358
 
@@ -343,14 +395,7 @@ exports.imports = function* (
343
395
 
344
396
  for (const url of prefix) {
345
397
  yield {
346
- children: exports.module(
347
- url,
348
- null,
349
- {},
350
- artifacts,
351
- visited,
352
- opts
353
- )
398
+ children: exports.module(url, null, {}, artifacts, visited, opts)
354
399
  }
355
400
 
356
401
  addURL(artifacts.assets, url)
@@ -371,14 +416,7 @@ exports.imports = function* (
371
416
  }
372
417
 
373
418
  yield {
374
- children: exports.module(
375
- url,
376
- source,
377
- attributes,
378
- artifacts,
379
- visited,
380
- opts
381
- )
419
+ children: exports.module(url, source, attributes, artifacts, visited, opts)
382
420
  }
383
421
 
384
422
  resolved = yielded = true
@@ -397,36 +435,21 @@ exports.imports = function* (
397
435
 
398
436
  matchedConditions.pop()
399
437
 
400
- let message = `Cannot find ${condition === 'default' ? 'module' : condition} '${specifier}' imported from '${parentURL.href}'`
438
+ if (resolutions === 0) {
439
+ let message = `Cannot find ${condition === 'default' ? 'module' : condition} '${specifier}' imported from '${parentURL.href}'`
401
440
 
402
- if (candidates.length > 0) {
403
- message += '\nCandidates:'
404
- message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
405
- }
441
+ if (candidates.length > 0) {
442
+ message += '\nCandidates:'
443
+ message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
444
+ }
406
445
 
407
- if (resolutions === 0) {
408
446
  switch (condition) {
409
447
  case 'addon':
410
- throw errors.ADDON_NOT_FOUND(
411
- message,
412
- specifier,
413
- parentURL,
414
- candidates
415
- )
448
+ throw errors.ADDON_NOT_FOUND(message, specifier, parentURL, candidates)
416
449
  case 'asset':
417
- throw errors.ASSET_NOT_FOUND(
418
- message,
419
- specifier,
420
- parentURL,
421
- candidates
422
- )
450
+ throw errors.ASSET_NOT_FOUND(message, specifier, parentURL, candidates)
423
451
  default:
424
- throw errors.MODULE_NOT_FOUND(
425
- message,
426
- specifier,
427
- parentURL,
428
- candidates
429
- )
452
+ throw errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
430
453
  }
431
454
  }
432
455
  }
@@ -474,13 +497,7 @@ exports.prebuilds = function* (packageURL, artifacts, visited, opts = {}) {
474
497
  return yielded
475
498
  }
476
499
 
477
- exports.assets = function* (
478
- patterns,
479
- parentURL,
480
- artifacts,
481
- visited,
482
- opts = {}
483
- ) {
500
+ exports.assets = function* (patterns, parentURL, artifacts, visited, opts = {}) {
484
501
  const matches = yield* exports.patternMatches(patterns, parentURL, [], opts)
485
502
 
486
503
  let yielded = false
@@ -502,12 +519,7 @@ exports.assets = function* (
502
519
  return yielded
503
520
  }
504
521
 
505
- exports.patternMatches = function* patternMatches(
506
- pattern,
507
- parentURL,
508
- matches,
509
- opts = {}
510
- ) {
522
+ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, opts = {}) {
511
523
  const { conditions = [], matchedConditions = [] } = opts
512
524
 
513
525
  if (typeof pattern === 'string') {
@@ -549,11 +561,7 @@ exports.patternMatches = function* patternMatches(
549
561
  } else if (typeof pattern === 'object' && pattern !== null) {
550
562
  let yielded = false
551
563
 
552
- for (const [condition, patternValue, subset] of conditionMatches(
553
- pattern,
554
- conditions,
555
- opts
556
- )) {
564
+ for (const [condition, patternValue, subset] of conditionMatches(pattern, conditions, opts)) {
557
565
  matchedConditions.push(condition)
558
566
 
559
567
  if (
@@ -685,9 +693,7 @@ function mixinImports(target, imports, url) {
685
693
  }
686
694
 
687
695
  if (typeof imports !== 'object' || imports === null) {
688
- throw errors.INVALID_IMPORTS_MAP(
689
- `Imports map at '${url.href}' is not valid`
690
- )
696
+ throw errors.INVALID_IMPORTS_MAP(`Imports map at '${url.href}' is not valid`)
691
697
  }
692
698
 
693
699
  return { ...target, ...imports }
package/lib/errors.js CHANGED
@@ -1,5 +1,5 @@
1
1
  module.exports = class ModuleTraverseError extends Error {
2
- constructor(msg, code, fn = ModuleTraverseError) {
2
+ constructor(msg, fn = ModuleTraverseError, code = fn.name) {
3
3
  super(`${code}: ${msg}`)
4
4
  this.code = code
5
5
 
@@ -13,11 +13,7 @@ module.exports = class ModuleTraverseError extends Error {
13
13
  }
14
14
 
15
15
  static MODULE_NOT_FOUND(msg, specifier, referrer = null, candidates = []) {
16
- const err = new ModuleTraverseError(
17
- msg,
18
- 'MODULE_NOT_FOUND',
19
- ModuleTraverseError.MODULE_NOT_FOUND
20
- )
16
+ const err = new ModuleTraverseError(msg, ModuleTraverseError.MODULE_NOT_FOUND)
21
17
 
22
18
  err.specifier = specifier
23
19
  err.referrer = referrer
@@ -27,11 +23,7 @@ module.exports = class ModuleTraverseError extends Error {
27
23
  }
28
24
 
29
25
  static ADDON_NOT_FOUND(msg, specifier, referrer = null, candidates = []) {
30
- const err = new ModuleTraverseError(
31
- msg,
32
- 'ADDON_NOT_FOUND',
33
- ModuleTraverseError.ADDON_NOT_FOUND
34
- )
26
+ const err = new ModuleTraverseError(msg, ModuleTraverseError.ADDON_NOT_FOUND)
35
27
 
36
28
  err.specifier = specifier
37
29
  err.referrer = referrer
@@ -41,11 +33,7 @@ module.exports = class ModuleTraverseError extends Error {
41
33
  }
42
34
 
43
35
  static ASSET_NOT_FOUND(msg, specifier, referrer = null, candidates = []) {
44
- const err = new ModuleTraverseError(
45
- msg,
46
- 'ASSET_NOT_FOUND',
47
- ModuleTraverseError.ASSET_NOT_FOUND
48
- )
36
+ const err = new ModuleTraverseError(msg, ModuleTraverseError.ASSET_NOT_FOUND)
49
37
 
50
38
  err.specifier = specifier
51
39
  err.referrer = referrer
@@ -55,10 +43,6 @@ module.exports = class ModuleTraverseError extends Error {
55
43
  }
56
44
 
57
45
  static INVALID_IMPORTS_MAP(msg) {
58
- return new ModuleTraverseError(
59
- msg,
60
- 'INVALID_IMPORTS_MAP',
61
- ModuleTraverseError.INVALID_IMPORTS_MAP
62
- )
46
+ return new ModuleTraverseError(msg, ModuleTraverseError.INVALID_IMPORTS_MAP)
63
47
  }
64
48
  }
@@ -8,11 +8,7 @@ interface BareResolveOptions extends ResolveOptions {
8
8
  target?: string[]
9
9
  }
10
10
 
11
- declare function resolve(
12
- entry: Import,
13
- parentURL: URL,
14
- opts?: BareResolveOptions
15
- ): Resolver
11
+ declare function resolve(entry: Import, parentURL: URL, opts?: BareResolveOptions): Resolver
16
12
 
17
13
  declare namespace resolve {
18
14
  export { type BareResolveOptions }
@@ -1,10 +1,6 @@
1
1
  import { type Import } from 'bare-module-lexer'
2
2
  import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
3
3
 
4
- declare function resolve(
5
- entry: Import,
6
- parentURL: URL,
7
- opts?: ResolveOptions
8
- ): Resolver
4
+ declare function resolve(entry: Import, parentURL: URL, opts?: ResolveOptions): Resolver
9
5
 
10
6
  export = resolve
@@ -8,11 +8,7 @@ interface NodeResolveOptions extends ResolveOptions {
8
8
  target?: string[]
9
9
  }
10
10
 
11
- declare function resolve(
12
- entry: Import,
13
- parentURL: URL,
14
- opts?: NodeResolveOptions
15
- ): Resolver
11
+ declare function resolve(entry: Import, parentURL: URL, opts?: NodeResolveOptions): Resolver
16
12
 
17
13
  declare namespace resolve {
18
14
  export { type NodeResolveOptions }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "1.6.0",
3
+ "version": "1.7.1",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {
@@ -62,7 +62,7 @@
62
62
  "bare-url": "^2.1.3",
63
63
  "brittle": "^3.2.1",
64
64
  "prettier": "^3.4.2",
65
- "prettier-config-standard": "^7.0.0"
65
+ "prettier-config-holepunch": "^2.0.0"
66
66
  },
67
67
  "peerDependencies": {
68
68
  "bare-buffer": "*",