bare-module-traverse 1.7.0 → 1.7.2

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
  ```
package/index.d.ts CHANGED
@@ -18,6 +18,7 @@ interface Dependency {
18
18
  }
19
19
 
20
20
  interface TraverseOptions extends ResolveOptions {
21
+ defaultType?: number
21
22
  resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
22
23
  }
23
24
 
@@ -61,6 +62,17 @@ declare namespace traverse {
61
62
  assets: URL[]
62
63
  }
63
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
+
64
76
  export function module(
65
77
  url: URL,
66
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,76 @@ exports.module = function* (
180
166
  }
181
167
  }
182
168
 
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
+
183
234
  const lexer = { imports: [] }
184
235
 
185
- yield* exports.imports(url, source, imports, artifacts, lexer, visited, opts)
236
+ if (type === constants.SCRIPT || constants.MODULE) {
237
+ yield* exports.imports(url, source, imports, artifacts, lexer, visited, opts)
238
+ }
186
239
 
187
240
  yield {
188
241
  dependency: { url, source, imports: compressImportsMap(imports), lexer }
@@ -217,14 +270,13 @@ exports.package = function* (url, source, artifacts, visited, opts = {}) {
217
270
  return false
218
271
  }
219
272
 
220
- exports.preresolved = function* (
221
- url,
222
- source,
223
- resolutions,
224
- artifacts,
225
- visited,
226
- opts = {}
227
- ) {
273
+ exports.preresolved = function* (url, source, resolutions, artifacts, visited, opts = {}) {
274
+ const {
275
+ builtinProtocol = 'builtin:',
276
+ linkedProtocol = 'linked:',
277
+ deferredProtocol = 'deferred:'
278
+ } = opts
279
+
228
280
  const imports = resolutions[url.href]
229
281
 
230
282
  if (typeof imports !== 'object' || imports === null) return false
@@ -242,7 +294,11 @@ exports.preresolved = function* (
242
294
  yield {
243
295
  children: exports.package(url, null, artifacts, visited, opts)
244
296
  }
245
- } else {
297
+ } else if (
298
+ url.protocol !== builtinProtocol &&
299
+ url.protocol !== linkedProtocol &&
300
+ url.protocol !== deferredProtocol
301
+ ) {
246
302
  yield {
247
303
  children: exports.module(url, null, {}, artifacts, visited, opts)
248
304
  }
@@ -265,15 +321,7 @@ exports.preresolved = function* (
265
321
  return true
266
322
  }
267
323
 
268
- exports.imports = function* (
269
- parentURL,
270
- source,
271
- imports,
272
- artifacts,
273
- lexer,
274
- visited,
275
- opts = {}
276
- ) {
324
+ exports.imports = function* (parentURL, source, imports, artifacts, lexer, visited, opts = {}) {
277
325
  const {
278
326
  resolve = exports.resolve.default,
279
327
  builtinProtocol = 'builtin:',
@@ -357,14 +405,7 @@ exports.imports = function* (
357
405
 
358
406
  for (const url of prefix) {
359
407
  yield {
360
- children: exports.module(
361
- url,
362
- null,
363
- {},
364
- artifacts,
365
- visited,
366
- opts
367
- )
408
+ children: exports.module(url, null, {}, artifacts, visited, opts)
368
409
  }
369
410
 
370
411
  addURL(artifacts.assets, url)
@@ -385,14 +426,7 @@ exports.imports = function* (
385
426
  }
386
427
 
387
428
  yield {
388
- children: exports.module(
389
- url,
390
- source,
391
- attributes,
392
- artifacts,
393
- visited,
394
- opts
395
- )
429
+ children: exports.module(url, source, attributes, artifacts, visited, opts)
396
430
  }
397
431
 
398
432
  resolved = yielded = true
@@ -421,26 +455,11 @@ exports.imports = function* (
421
455
 
422
456
  switch (condition) {
423
457
  case 'addon':
424
- throw errors.ADDON_NOT_FOUND(
425
- message,
426
- specifier,
427
- parentURL,
428
- candidates
429
- )
458
+ throw errors.ADDON_NOT_FOUND(message, specifier, parentURL, candidates)
430
459
  case 'asset':
431
- throw errors.ASSET_NOT_FOUND(
432
- message,
433
- specifier,
434
- parentURL,
435
- candidates
436
- )
460
+ throw errors.ASSET_NOT_FOUND(message, specifier, parentURL, candidates)
437
461
  default:
438
- throw errors.MODULE_NOT_FOUND(
439
- message,
440
- specifier,
441
- parentURL,
442
- candidates
443
- )
462
+ throw errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
444
463
  }
445
464
  }
446
465
  }
@@ -488,13 +507,7 @@ exports.prebuilds = function* (packageURL, artifacts, visited, opts = {}) {
488
507
  return yielded
489
508
  }
490
509
 
491
- exports.assets = function* (
492
- patterns,
493
- parentURL,
494
- artifacts,
495
- visited,
496
- opts = {}
497
- ) {
510
+ exports.assets = function* (patterns, parentURL, artifacts, visited, opts = {}) {
498
511
  const matches = yield* exports.patternMatches(patterns, parentURL, [], opts)
499
512
 
500
513
  let yielded = false
@@ -516,12 +529,7 @@ exports.assets = function* (
516
529
  return yielded
517
530
  }
518
531
 
519
- exports.patternMatches = function* patternMatches(
520
- pattern,
521
- parentURL,
522
- matches,
523
- opts = {}
524
- ) {
532
+ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, opts = {}) {
525
533
  const { conditions = [], matchedConditions = [] } = opts
526
534
 
527
535
  if (typeof pattern === 'string') {
@@ -563,11 +571,7 @@ exports.patternMatches = function* patternMatches(
563
571
  } else if (typeof pattern === 'object' && pattern !== null) {
564
572
  let yielded = false
565
573
 
566
- for (const [condition, patternValue, subset] of conditionMatches(
567
- pattern,
568
- conditions,
569
- opts
570
- )) {
574
+ for (const [condition, patternValue, subset] of conditionMatches(pattern, conditions, opts)) {
571
575
  matchedConditions.push(condition)
572
576
 
573
577
  if (
@@ -699,9 +703,7 @@ function mixinImports(target, imports, url) {
699
703
  }
700
704
 
701
705
  if (typeof imports !== 'object' || imports === null) {
702
- throw errors.INVALID_IMPORTS_MAP(
703
- `Imports map at '${url.href}' is not valid`
704
- )
706
+ throw errors.INVALID_IMPORTS_MAP(`Imports map at '${url.href}' is not valid`)
705
707
  }
706
708
 
707
709
  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.7.0",
3
+ "version": "1.7.2",
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": "*",