bare-module-traverse 2.1.1 → 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.
Files changed (3) hide show
  1. package/README.md +4 -1
  2. package/index.js +28 -3
  3. 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.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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {