bare-module-traverse 2.1.0 → 2.1.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
@@ -62,7 +62,10 @@ options = {
62
62
  aliases: {
63
63
  // Map an extension to a supported extension, e.g. `'.ts': '.js'`. The
64
64
  // aliased extension is used for module type detection, so `readModule()`
65
- // must return source compatible with that type.
65
+ // must return source compatible with that type. Aliased modules are also
66
+ // emitted with the aliased extension, and resolutions to them are
67
+ // rewritten to match, so `'.ts': '.js'` yields a `file:///foo.js`
68
+ // dependency rather than `file:///foo.ts`.
66
69
  },
67
70
  resolve: resolve.default
68
71
  }
package/index.d.ts CHANGED
@@ -70,8 +70,8 @@ declare namespace traverse {
70
70
  >
71
71
 
72
72
  export interface Artifacts {
73
- addons: URL[]
74
- assets: URL[]
73
+ addons: URL[] | Set<string>
74
+ assets: URL[] | Set<string>
75
75
  }
76
76
 
77
77
  export const constants: {
package/index.js CHANGED
@@ -109,6 +109,26 @@ module.exports = exports = function traverse(entry, opts, readModule, listPrefix
109
109
  exports.constants = constants
110
110
  exports.resolve = resolve
111
111
 
112
+ exports.alias = function alias(url, opts = {}) {
113
+ const { aliases = null } = opts
114
+
115
+ if (aliases === null) return url
116
+
117
+ const match = url.pathname.match(/\.[a-z]+$/)
118
+
119
+ if (match === null) return url
120
+
121
+ const [extension] = match
122
+
123
+ if (extension in aliases === false) return url
124
+
125
+ url = new URL(url)
126
+
127
+ url.pathname = url.pathname.slice(0, -extension.length) + aliases[extension]
128
+
129
+ return url
130
+ }
131
+
112
132
  exports.module = function* (url, source, attributes, artifacts, visited, opts = {}) {
113
133
  const { resolutions = null, defaultType = constants.SCRIPT, aliases = null } = opts
114
134
 
@@ -242,7 +262,12 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
242
262
  }
243
263
 
244
264
  yield {
245
- dependency: { url, source, imports: compressImportsMap(imports), lexer }
265
+ dependency: {
266
+ url: exports.alias(url, opts),
267
+ source,
268
+ imports: compressImportsMap(imports),
269
+ lexer
270
+ }
246
271
  }
247
272
 
248
273
  return true
@@ -317,7 +342,7 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
317
342
 
318
343
  yield {
319
344
  dependency: {
320
- url,
345
+ url: exports.alias(url, opts),
321
346
  source,
322
347
  imports: compressImportsMap(imports),
323
348
  lexer: { imports: [] }
@@ -421,7 +446,7 @@ exports.imports = function* (parentURL, source, imports, artifacts, lexer, visit
421
446
  const source = yield { module: url }
422
447
 
423
448
  if (source !== null) {
424
- addResolution(imports, specifier, matchedConditions, url)
449
+ addResolution(imports, specifier, matchedConditions, exports.alias(url, opts))
425
450
 
426
451
  let attributes = entry.attributes
427
452
 
@@ -580,24 +605,28 @@ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, o
580
605
  return matches
581
606
  }
582
607
 
583
- function addURL(array, url) {
584
- let lo = 0
585
- let hi = array.length - 1
608
+ function addURL(collection, url) {
609
+ if (Array.isArray(collection)) {
610
+ let lo = 0
611
+ let hi = collection.length - 1
586
612
 
587
- while (lo <= hi) {
588
- const mid = lo + ((hi - lo) >> 1)
589
- const found = array[mid]
613
+ while (lo <= hi) {
614
+ const mid = lo + ((hi - lo) >> 1)
615
+ const found = collection[mid]
590
616
 
591
- if (found.href === url.href) return
617
+ if (found.href === url.href) return
592
618
 
593
- if (found.href < url.href) {
594
- lo = mid + 1
595
- } else {
596
- hi = mid - 1
619
+ if (found.href < url.href) {
620
+ lo = mid + 1
621
+ } else {
622
+ hi = mid - 1
623
+ }
597
624
  }
598
- }
599
625
 
600
- array.splice(lo, 0, url)
626
+ collection.splice(lo, 0, url)
627
+ } else {
628
+ collection.add(url.href)
629
+ }
601
630
  }
602
631
 
603
632
  function removeURL(array, url) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {