bare-module-traverse 2.5.5 → 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 +150 -65
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -353,40 +353,57 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
353
353
 
354
354
  if (typeof imports !== 'object' || imports === null) return false
355
355
 
356
- const type = moduleType(url, {}, null, opts)
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)
366
366
 
367
367
  if (specifier === '#package') {
368
+ const scope = yield* readPackage(url, opts)
369
+
370
+ if (scope !== null && info === null) info = scope.info
371
+
368
372
  yield {
369
- children: exports.package(url, null, artifacts, visited, opts),
373
+ children: exports.package(
374
+ url,
375
+ scope === null ? null : scope.source,
376
+ artifacts,
377
+ visited,
378
+ opts
379
+ ),
370
380
  deferred: false
371
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)
372
386
  } else if (asset) {
373
387
  if (artifacts !== null) {
374
- addURL(artifacts.assets, url)
375
-
376
- yield {
377
- children: exports.module(url, null, {}, artifacts, visited, {
378
- ...opts,
379
- asset: true,
380
- importType: 0
381
- }),
382
- 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
+ }
383
402
  }
384
403
  }
385
- } else if (
386
- url.protocol !== builtinProtocol &&
387
- url.protocol !== linkedProtocol &&
388
- url.protocol !== deferredProtocol
389
- ) {
404
+ } else {
405
+ if (addon && artifacts !== null) addURL(artifacts.addons, url)
406
+
390
407
  yield {
391
408
  children: exports.module(url, null, {}, artifacts, visited, {
392
409
  ...opts,
@@ -397,15 +414,21 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
397
414
  }
398
415
  } else {
399
416
  for (const [condition, child] of Object.entries(entry)) {
400
- 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
+ })
401
422
  }
402
423
  }
403
424
  }
404
425
  }
405
426
 
427
+ const type = moduleType(url, {}, info, opts)
428
+
406
429
  const lexer = { imports: [], exports: [] }
407
430
 
408
- if (type === constants.SCRIPT || type === constants.MODULE) {
431
+ if (opts.asset !== true && (type === constants.SCRIPT || type === constants.MODULE)) {
409
432
  const lexed = lex(source)
410
433
 
411
434
  lexer.imports = lexed.imports
@@ -492,8 +515,7 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
492
515
  builtinProtocol = 'builtin:',
493
516
  linkedProtocol = 'linked:',
494
517
  deferredProtocol = 'deferred:',
495
- deferUnresolved = false,
496
- prefixes = new Map()
518
+ deferUnresolved = false
497
519
  } = opts
498
520
 
499
521
  const matchedConditions = []
@@ -530,51 +552,9 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
530
552
 
531
553
  resolved = true
532
554
  } else if (condition === 'asset') {
533
- const prefix = url
534
- const expand = artifacts !== null
535
-
536
- let expanded = prefixes.get(prefix.href)
537
-
538
- if (expanded === undefined) {
539
- const listed = yield { prefix, expand }
540
-
541
- const urls = []
542
-
543
- let prefixResolution = null
544
- let found = false
545
-
546
- for (const url of listed) {
547
- found = true
548
-
549
- if (expand === false) break
550
-
551
- const resolution = listed.resolved === true ? url : yield* postresolve(url)
552
-
553
- if (url.href === prefix.href) prefixResolution = resolution
554
-
555
- yield {
556
- children: exports.module(resolution, null, {}, artifacts, visited, {
557
- ...opts,
558
- asset: true
559
- }),
560
- deferred: true
561
- }
562
-
563
- urls.push(resolution)
564
- }
565
-
566
- if (found) {
567
- expanded = { resolution: prefixResolution || (yield* postresolve(prefix)), urls }
568
-
569
- prefixes.set(prefix.href, expanded)
570
- }
571
- }
555
+ const expanded = yield* expandAsset(url, artifacts, visited, opts)
572
556
 
573
557
  if (expanded !== undefined) {
574
- if (expand) {
575
- for (const url of expanded.urls) addURL(artifacts.assets, url)
576
- }
577
-
578
558
  resolution = expanded.resolution
579
559
 
580
560
  addResolution(imports, specifier, matchedConditions, resolution)
@@ -792,6 +772,22 @@ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, o
792
772
  return matches
793
773
  }
794
774
 
775
+ function* readPackage(url, opts) {
776
+ const { packages = new Map() } = opts
777
+
778
+ const cached = packages.get(url.href)
779
+
780
+ if (cached !== undefined) return cached
781
+
782
+ const source = yield { module: url, artifact: false }
783
+
784
+ const scope = source === null ? null : { url, source, info: JSON.parse(source) }
785
+
786
+ packages.set(url.href, scope)
787
+
788
+ return scope
789
+ }
790
+
795
791
  function* lookupPackage(url, opts) {
796
792
  const { packages = new Map() } = opts
797
793
 
@@ -824,6 +820,55 @@ function* lookupPackage(url, opts) {
824
820
  return null
825
821
  }
826
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
+
827
872
  function* postresolve(url) {
828
873
  return (yield { resolution: url }) || url
829
874
  }
@@ -1127,6 +1172,10 @@ function compressImportsMap(imports) {
1127
1172
  function compressImportsMapEntry(resolved) {
1128
1173
  if (typeof resolved === 'string') return resolved
1129
1174
 
1175
+ if (artifactConditions.some((condition) => condition in resolved)) {
1176
+ return compressArtifactImportsMapEntry(resolved)
1177
+ }
1178
+
1130
1179
  let entries = []
1131
1180
  let primary = null
1132
1181
 
@@ -1153,6 +1202,42 @@ function compressImportsMapEntry(resolved) {
1153
1202
  return Object.fromEntries(entries)
1154
1203
  }
1155
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
+
1156
1241
  function mixinImports(target, imports, url) {
1157
1242
  if (typeof imports === 'object' && imports !== null && 'imports' in imports) {
1158
1243
  imports = imports.imports
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.5.5",
3
+ "version": "2.5.7",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {