bare-module-traverse 2.5.6 → 2.5.7

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 (2) hide show
  1. package/index.js +120 -63
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -356,10 +356,10 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
356
356
  let info = null
357
357
 
358
358
  for (const [specifier, entry] of Object.entries(imports)) {
359
- const stack = [{ entry, asset: false }]
359
+ const stack = [{ entry, addon: false, asset: false }]
360
360
 
361
361
  while (stack.length > 0) {
362
- const { entry, asset } = stack.pop()
362
+ const { entry, addon, asset } = stack.pop()
363
363
 
364
364
  if (typeof entry === 'string') {
365
365
  const url = new URL(entry)
@@ -379,24 +379,31 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
379
379
  ),
380
380
  deferred: false
381
381
  }
382
+ } else if (url.protocol === deferredProtocol) {
383
+ continue
384
+ } else if (url.protocol === builtinProtocol || url.protocol === linkedProtocol) {
385
+ if (addon && artifacts !== null) addURL(artifacts.addons, url)
382
386
  } else if (asset) {
383
387
  if (artifacts !== null) {
384
- addURL(artifacts.assets, url)
385
-
386
- yield {
387
- children: exports.module(url, null, {}, artifacts, visited, {
388
- ...opts,
389
- asset: true,
390
- importType: 0
391
- }),
392
- deferred: true
388
+ const expanded = yield* expandAsset(url, artifacts, visited, { ...opts, importType: 0 })
389
+
390
+ // Nothing to expand is left for the module itself to report.
391
+ if (expanded === undefined) {
392
+ addURL(artifacts.assets, url)
393
+
394
+ yield {
395
+ children: exports.module(url, null, {}, artifacts, visited, {
396
+ ...opts,
397
+ asset: true,
398
+ importType: 0
399
+ }),
400
+ deferred: true
401
+ }
393
402
  }
394
403
  }
395
- } else if (
396
- url.protocol !== builtinProtocol &&
397
- url.protocol !== linkedProtocol &&
398
- url.protocol !== deferredProtocol
399
- ) {
404
+ } else {
405
+ if (addon && artifacts !== null) addURL(artifacts.addons, url)
406
+
400
407
  yield {
401
408
  children: exports.module(url, null, {}, artifacts, visited, {
402
409
  ...opts,
@@ -407,7 +414,11 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
407
414
  }
408
415
  } else {
409
416
  for (const [condition, child] of Object.entries(entry)) {
410
- stack.push({ entry: child, asset: asset || condition === 'asset' })
417
+ stack.push({
418
+ entry: child,
419
+ addon: addon || condition === 'addon',
420
+ asset: asset || condition === 'asset'
421
+ })
411
422
  }
412
423
  }
413
424
  }
@@ -417,7 +428,7 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
417
428
 
418
429
  const lexer = { imports: [], exports: [] }
419
430
 
420
- if (type === constants.SCRIPT || type === constants.MODULE) {
431
+ if (opts.asset !== true && (type === constants.SCRIPT || type === constants.MODULE)) {
421
432
  const lexed = lex(source)
422
433
 
423
434
  lexer.imports = lexed.imports
@@ -504,8 +515,7 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
504
515
  builtinProtocol = 'builtin:',
505
516
  linkedProtocol = 'linked:',
506
517
  deferredProtocol = 'deferred:',
507
- deferUnresolved = false,
508
- prefixes = new Map()
518
+ deferUnresolved = false
509
519
  } = opts
510
520
 
511
521
  const matchedConditions = []
@@ -542,51 +552,9 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
542
552
 
543
553
  resolved = true
544
554
  } else if (condition === 'asset') {
545
- const prefix = url
546
- const expand = artifacts !== null
547
-
548
- let expanded = prefixes.get(prefix.href)
549
-
550
- if (expanded === undefined) {
551
- const listed = yield { prefix, expand }
552
-
553
- const urls = []
554
-
555
- let prefixResolution = null
556
- let found = false
557
-
558
- for (const url of listed) {
559
- found = true
560
-
561
- if (expand === false) break
562
-
563
- const resolution = listed.resolved === true ? url : yield* postresolve(url)
564
-
565
- if (url.href === prefix.href) prefixResolution = resolution
566
-
567
- yield {
568
- children: exports.module(resolution, null, {}, artifacts, visited, {
569
- ...opts,
570
- asset: true
571
- }),
572
- deferred: true
573
- }
574
-
575
- urls.push(resolution)
576
- }
577
-
578
- if (found) {
579
- expanded = { resolution: prefixResolution || (yield* postresolve(prefix)), urls }
580
-
581
- prefixes.set(prefix.href, expanded)
582
- }
583
- }
555
+ const expanded = yield* expandAsset(url, artifacts, visited, opts)
584
556
 
585
557
  if (expanded !== undefined) {
586
- if (expand) {
587
- for (const url of expanded.urls) addURL(artifacts.assets, url)
588
- }
589
-
590
558
  resolution = expanded.resolution
591
559
 
592
560
  addResolution(imports, specifier, matchedConditions, resolution)
@@ -852,6 +820,55 @@ function* lookupPackage(url, opts) {
852
820
  return null
853
821
  }
854
822
 
823
+ function* expandAsset(prefix, artifacts, visited, opts) {
824
+ const { prefixes = new Map() } = opts
825
+
826
+ const expand = artifacts !== null
827
+
828
+ let expanded = prefixes.get(prefix.href)
829
+
830
+ if (expanded === undefined) {
831
+ const listed = yield { prefix, expand }
832
+
833
+ const urls = []
834
+
835
+ let prefixResolution = null
836
+ let found = false
837
+
838
+ for (const url of listed) {
839
+ found = true
840
+
841
+ if (expand === false) break
842
+
843
+ const resolution = listed.resolved === true ? url : yield* postresolve(url)
844
+
845
+ if (url.href === prefix.href) prefixResolution = resolution
846
+
847
+ yield {
848
+ children: exports.module(resolution, null, {}, artifacts, visited, {
849
+ ...opts,
850
+ asset: true
851
+ }),
852
+ deferred: true
853
+ }
854
+
855
+ urls.push(resolution)
856
+ }
857
+
858
+ if (found) {
859
+ expanded = { resolution: prefixResolution || (yield* postresolve(prefix)), urls }
860
+
861
+ prefixes.set(prefix.href, expanded)
862
+ }
863
+ }
864
+
865
+ if (expanded !== undefined && expand) {
866
+ for (const url of expanded.urls) addURL(artifacts.assets, url)
867
+ }
868
+
869
+ return expanded
870
+ }
871
+
855
872
  function* postresolve(url) {
856
873
  return (yield { resolution: url }) || url
857
874
  }
@@ -1155,6 +1172,10 @@ function compressImportsMap(imports) {
1155
1172
  function compressImportsMapEntry(resolved) {
1156
1173
  if (typeof resolved === 'string') return resolved
1157
1174
 
1175
+ if (artifactConditions.some((condition) => condition in resolved)) {
1176
+ return compressArtifactImportsMapEntry(resolved)
1177
+ }
1178
+
1158
1179
  let entries = []
1159
1180
  let primary = null
1160
1181
 
@@ -1181,6 +1202,42 @@ function compressImportsMapEntry(resolved) {
1181
1202
  return Object.fromEntries(entries)
1182
1203
  }
1183
1204
 
1205
+ // Only the condition an addon or asset was resolved for tells it apart from a
1206
+ // module, so those conditions must survive compression.
1207
+ const artifactConditions = ['addon', 'asset']
1208
+
1209
+ function compressArtifactImportsMapEntry(resolved) {
1210
+ const artifacts = {}
1211
+ const rest = {}
1212
+
1213
+ for (const [condition, entry] of Object.entries(resolved)) {
1214
+ if (artifactConditions.includes(condition)) {
1215
+ artifacts[condition] = compressImportsMapEntry(entry)
1216
+ } else {
1217
+ rest[condition] = entry
1218
+ }
1219
+ }
1220
+
1221
+ const conditions = Object.keys(rest)
1222
+
1223
+ if (conditions.length === 0) return artifacts
1224
+
1225
+ let compressed = compressImportsMapEntry(rest)
1226
+
1227
+ if (typeof compressed === 'string') {
1228
+ compressed = { [conditions.length === 1 ? conditions[0] : 'default']: compressed }
1229
+ }
1230
+
1231
+ const { default: fallback, ...matched } = compressed
1232
+
1233
+ // A `default` ahead of an artifact would be matched for it instead.
1234
+ const entry = { ...matched, ...artifacts }
1235
+
1236
+ if (fallback !== undefined) entry.default = fallback
1237
+
1238
+ return entry
1239
+ }
1240
+
1184
1241
  function mixinImports(target, imports, url) {
1185
1242
  if (typeof imports === 'object' && imports !== null && 'imports' in imports) {
1186
1243
  imports = imports.imports
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.5.6",
3
+ "version": "2.5.7",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {