bare-module-traverse 1.7.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
  ```
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,7 @@ 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 = {}) {
228
274
  const imports = resolutions[url.href]
229
275
 
230
276
  if (typeof imports !== 'object' || imports === null) return false
@@ -265,15 +311,7 @@ exports.preresolved = function* (
265
311
  return true
266
312
  }
267
313
 
268
- exports.imports = function* (
269
- parentURL,
270
- source,
271
- imports,
272
- artifacts,
273
- lexer,
274
- visited,
275
- opts = {}
276
- ) {
314
+ exports.imports = function* (parentURL, source, imports, artifacts, lexer, visited, opts = {}) {
277
315
  const {
278
316
  resolve = exports.resolve.default,
279
317
  builtinProtocol = 'builtin:',
@@ -357,14 +395,7 @@ exports.imports = function* (
357
395
 
358
396
  for (const url of prefix) {
359
397
  yield {
360
- children: exports.module(
361
- url,
362
- null,
363
- {},
364
- artifacts,
365
- visited,
366
- opts
367
- )
398
+ children: exports.module(url, null, {}, artifacts, visited, opts)
368
399
  }
369
400
 
370
401
  addURL(artifacts.assets, url)
@@ -385,14 +416,7 @@ exports.imports = function* (
385
416
  }
386
417
 
387
418
  yield {
388
- children: exports.module(
389
- url,
390
- source,
391
- attributes,
392
- artifacts,
393
- visited,
394
- opts
395
- )
419
+ children: exports.module(url, source, attributes, artifacts, visited, opts)
396
420
  }
397
421
 
398
422
  resolved = yielded = true
@@ -421,26 +445,11 @@ exports.imports = function* (
421
445
 
422
446
  switch (condition) {
423
447
  case 'addon':
424
- throw errors.ADDON_NOT_FOUND(
425
- message,
426
- specifier,
427
- parentURL,
428
- candidates
429
- )
448
+ throw errors.ADDON_NOT_FOUND(message, specifier, parentURL, candidates)
430
449
  case 'asset':
431
- throw errors.ASSET_NOT_FOUND(
432
- message,
433
- specifier,
434
- parentURL,
435
- candidates
436
- )
450
+ throw errors.ASSET_NOT_FOUND(message, specifier, parentURL, candidates)
437
451
  default:
438
- throw errors.MODULE_NOT_FOUND(
439
- message,
440
- specifier,
441
- parentURL,
442
- candidates
443
- )
452
+ throw errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
444
453
  }
445
454
  }
446
455
  }
@@ -488,13 +497,7 @@ exports.prebuilds = function* (packageURL, artifacts, visited, opts = {}) {
488
497
  return yielded
489
498
  }
490
499
 
491
- exports.assets = function* (
492
- patterns,
493
- parentURL,
494
- artifacts,
495
- visited,
496
- opts = {}
497
- ) {
500
+ exports.assets = function* (patterns, parentURL, artifacts, visited, opts = {}) {
498
501
  const matches = yield* exports.patternMatches(patterns, parentURL, [], opts)
499
502
 
500
503
  let yielded = false
@@ -516,12 +519,7 @@ exports.assets = function* (
516
519
  return yielded
517
520
  }
518
521
 
519
- exports.patternMatches = function* patternMatches(
520
- pattern,
521
- parentURL,
522
- matches,
523
- opts = {}
524
- ) {
522
+ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, opts = {}) {
525
523
  const { conditions = [], matchedConditions = [] } = opts
526
524
 
527
525
  if (typeof pattern === 'string') {
@@ -563,11 +561,7 @@ exports.patternMatches = function* patternMatches(
563
561
  } else if (typeof pattern === 'object' && pattern !== null) {
564
562
  let yielded = false
565
563
 
566
- for (const [condition, patternValue, subset] of conditionMatches(
567
- pattern,
568
- conditions,
569
- opts
570
- )) {
564
+ for (const [condition, patternValue, subset] of conditionMatches(pattern, conditions, opts)) {
571
565
  matchedConditions.push(condition)
572
566
 
573
567
  if (
@@ -699,9 +693,7 @@ function mixinImports(target, imports, url) {
699
693
  }
700
694
 
701
695
  if (typeof imports !== 'object' || imports === null) {
702
- throw errors.INVALID_IMPORTS_MAP(
703
- `Imports map at '${url.href}' is not valid`
704
- )
696
+ throw errors.INVALID_IMPORTS_MAP(`Imports map at '${url.href}' is not valid`)
705
697
  }
706
698
 
707
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.7.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": "*",