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 +4 -1
- package/index.d.ts +2 -2
- package/index.js +45 -16
- package/package.json +1 -1
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
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: {
|
|
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(
|
|
584
|
-
|
|
585
|
-
|
|
608
|
+
function addURL(collection, url) {
|
|
609
|
+
if (Array.isArray(collection)) {
|
|
610
|
+
let lo = 0
|
|
611
|
+
let hi = collection.length - 1
|
|
586
612
|
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
613
|
+
while (lo <= hi) {
|
|
614
|
+
const mid = lo + ((hi - lo) >> 1)
|
|
615
|
+
const found = collection[mid]
|
|
590
616
|
|
|
591
|
-
|
|
617
|
+
if (found.href === url.href) return
|
|
592
618
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
619
|
+
if (found.href < url.href) {
|
|
620
|
+
lo = mid + 1
|
|
621
|
+
} else {
|
|
622
|
+
hi = mid - 1
|
|
623
|
+
}
|
|
597
624
|
}
|
|
598
|
-
}
|
|
599
625
|
|
|
600
|
-
|
|
626
|
+
collection.splice(lo, 0, url)
|
|
627
|
+
} else {
|
|
628
|
+
collection.add(url.href)
|
|
629
|
+
}
|
|
601
630
|
}
|
|
602
631
|
|
|
603
632
|
function removeURL(array, url) {
|