bare-module-traverse 2.0.2 → 2.1.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
@@ -59,6 +59,11 @@ Options include:
59
59
  ```js
60
60
  options = {
61
61
  defaultType: constants.SCRIPT,
62
+ aliases: {
63
+ // Map an extension to a supported extension, e.g. `'.ts': '.js'`. The
64
+ // aliased extension is used for module type detection, so `readModule()`
65
+ // must return source compatible with that type.
66
+ },
62
67
  resolve: resolve.default
63
68
  }
64
69
  ```
package/index.d.ts CHANGED
@@ -17,8 +17,20 @@ interface Dependency {
17
17
  }
18
18
  }
19
19
 
20
+ type AliasableExtension =
21
+ | '.js'
22
+ | '.cjs'
23
+ | '.mjs'
24
+ | '.json'
25
+ | '.bundle'
26
+ | '.bare'
27
+ | '.node'
28
+ | '.bin'
29
+ | '.txt'
30
+
20
31
  interface TraverseOptions extends ResolveOptions {
21
32
  defaultType?: number
33
+ aliases?: Record<string, AliasableExtension>
22
34
  resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
23
35
  }
24
36
 
@@ -58,8 +70,8 @@ declare namespace traverse {
58
70
  >
59
71
 
60
72
  export interface Artifacts {
61
- addons: URL[]
62
- assets: URL[]
73
+ addons: URL[] | Set<string>
74
+ assets: URL[] | Set<string>
63
75
  }
64
76
 
65
77
  export const constants: {
package/index.js CHANGED
@@ -110,7 +110,7 @@ exports.constants = constants
110
110
  exports.resolve = resolve
111
111
 
112
112
  exports.module = function* (url, source, attributes, artifacts, visited, opts = {}) {
113
- const { resolutions = null, defaultType = constants.SCRIPT } = opts
113
+ const { resolutions = null, defaultType = constants.SCRIPT, aliases = null } = opts
114
114
 
115
115
  if (visited.has(url.href)) return false
116
116
 
@@ -195,7 +195,9 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
195
195
  const match = url.pathname.match(/\.[a-z]+$/)
196
196
 
197
197
  if (match !== null) {
198
- const [extension] = match
198
+ let [extension] = match
199
+
200
+ if (aliases !== null && extension in aliases) extension = aliases[extension]
199
201
 
200
202
  switch (extension) {
201
203
  case '.js': {
@@ -578,24 +580,28 @@ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, o
578
580
  return matches
579
581
  }
580
582
 
581
- function addURL(array, url) {
582
- let lo = 0
583
- let hi = array.length - 1
583
+ function addURL(collection, url) {
584
+ if (Array.isArray(collection)) {
585
+ let lo = 0
586
+ let hi = collection.length - 1
584
587
 
585
- while (lo <= hi) {
586
- const mid = lo + ((hi - lo) >> 1)
587
- const found = array[mid]
588
+ while (lo <= hi) {
589
+ const mid = lo + ((hi - lo) >> 1)
590
+ const found = collection[mid]
588
591
 
589
- if (found.href === url.href) return
592
+ if (found.href === url.href) return
590
593
 
591
- if (found.href < url.href) {
592
- lo = mid + 1
593
- } else {
594
- hi = mid - 1
594
+ if (found.href < url.href) {
595
+ lo = mid + 1
596
+ } else {
597
+ hi = mid - 1
598
+ }
595
599
  }
596
- }
597
600
 
598
- array.splice(lo, 0, url)
601
+ collection.splice(lo, 0, url)
602
+ } else {
603
+ collection.add(url.href)
604
+ }
599
605
  }
600
606
 
601
607
  function removeURL(array, url) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.0.2",
3
+ "version": "2.1.1",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {