bare-module 7.0.1 → 7.0.3

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/lib/loader.js CHANGED
@@ -647,20 +647,10 @@ module.exports = class ModuleLoader {
647
647
 
648
648
  const requested = typeAttributeFor(attributes)
649
649
 
650
- if (requested === undefined) {
651
- // An import attribute somewhere else may have changed the type. An import
652
- // that asks for no type in particular should get the module as it is, or
653
- // an error, but never the type someone else asked for.
654
- if (naturalType !== 0 && naturalType !== actual) {
655
- throw errors.TYPE_INCOMPATIBLE(
656
- `Module '${record.url.href}' is only of type '${attributeByType[actual]}' in this graph`
657
- )
658
- }
659
-
660
- return
661
- }
662
-
663
- const expected = typeByAttribute[requested]
650
+ // An import attribute somewhere else may have changed the type. An import
651
+ // that asks for no type in particular should get the module as it is, or
652
+ // an error, but never the type someone else asked for.
653
+ const expected = requested === undefined ? naturalType : typeByAttribute[requested]
664
654
 
665
655
  if (expected === undefined) {
666
656
  throw errors.UNKNOWN_MODULE_TYPE(
@@ -668,8 +658,10 @@ module.exports = class ModuleLoader {
668
658
  )
669
659
  }
670
660
 
671
- if (expected !== actual) {
672
- throw errors.TYPE_INCOMPATIBLE(`Module '${record.url.href}' is not of type '${requested}'`)
661
+ if (expected !== 0 && expected !== actual) {
662
+ throw errors.TYPE_INCOMPATIBLE(
663
+ `Module '${record.url.href}' is already of type '${attributeByType[actual]}' in the graph`
664
+ )
673
665
  }
674
666
  }
675
667
 
@@ -746,7 +738,11 @@ module.exports = class ModuleLoader {
746
738
  }
747
739
 
748
740
  if (record === null) {
749
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${specifier}'`, specifier, parentURL)
741
+ throw errors.MODULE_NOT_FOUND(
742
+ `Cannot find module '${specifier}' imported from '${parentURL.href}'`,
743
+ specifier,
744
+ parentURL
745
+ )
750
746
  }
751
747
 
752
748
  return record
@@ -781,6 +777,8 @@ module.exports = class ModuleLoader {
781
777
  this._resolveOptions()
782
778
  )
783
779
 
780
+ const candidates = []
781
+
784
782
  let next = resolver.next()
785
783
 
786
784
  while (next.done !== true) {
@@ -803,7 +801,9 @@ module.exports = class ModuleLoader {
803
801
  } else {
804
802
  const url = value.resolution
805
803
 
806
- if (url.protocol === 'builtin:' || url.protocol === 'data:' || this.get(url) !== null) {
804
+ candidates.push(url)
805
+
806
+ if (isTerminalResolution(url, condition) || this.get(url) !== null) {
807
807
  this._cacheResolution(parentURL, specifier, condition, url)
808
808
 
809
809
  return url
@@ -821,7 +821,7 @@ module.exports = class ModuleLoader {
821
821
  }
822
822
  }
823
823
 
824
- throw notFound(condition, specifier, parentURL)
824
+ throw notFound(condition, specifier, parentURL, candidates)
825
825
  }
826
826
 
827
827
  async _resolve(specifier, parentURL, condition) {
@@ -833,6 +833,8 @@ module.exports = class ModuleLoader {
833
833
  this._resolveOptions()
834
834
  )
835
835
 
836
+ const candidates = []
837
+
836
838
  let next = resolver.next()
837
839
 
838
840
  while (next.done !== true) {
@@ -863,7 +865,9 @@ module.exports = class ModuleLoader {
863
865
  } else {
864
866
  const url = value.resolution
865
867
 
866
- if (url.protocol === 'builtin:' || url.protocol === 'data:' || this.get(url) !== null) {
868
+ candidates.push(url)
869
+
870
+ if (isTerminalResolution(url, condition) || this.get(url) !== null) {
867
871
  this._cacheResolution(parentURL, specifier, condition, url)
868
872
 
869
873
  return url
@@ -883,7 +887,7 @@ module.exports = class ModuleLoader {
883
887
  }
884
888
  }
885
889
 
886
- throw notFound(condition, specifier, parentURL)
890
+ throw notFound(condition, specifier, parentURL, candidates)
887
891
  }
888
892
 
889
893
  _resolveArtifact(referrer, specifier, condition, parentURL = referrer.url) {
@@ -973,14 +977,23 @@ async function drive(generator, protocol, driveLink, semaphore, answers = null)
973
977
  return next.value
974
978
  }
975
979
 
976
- function notFound(condition, specifier, parentURL) {
980
+ function notFound(condition, specifier, parentURL, candidates = []) {
981
+ const kind = condition === 'addon' || condition === 'asset' ? condition : 'module'
982
+
983
+ let message = `Cannot find ${kind} '${specifier}' imported from '${parentURL.href}'`
984
+
985
+ if (candidates.length > 0) {
986
+ message += '\nCandidates:'
987
+ message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
988
+ }
989
+
977
990
  switch (condition) {
978
991
  case 'addon':
979
- return errors.ADDON_NOT_FOUND(`Cannot find addon '${specifier}'`, specifier, parentURL)
992
+ return errors.ADDON_NOT_FOUND(message, specifier, parentURL, candidates)
980
993
  case 'asset':
981
- return errors.ASSET_NOT_FOUND(`Cannot find asset '${specifier}'`, specifier, parentURL)
994
+ return errors.ASSET_NOT_FOUND(message, specifier, parentURL, candidates)
982
995
  default:
983
- return errors.MODULE_NOT_FOUND(`Cannot find module '${specifier}'`, specifier, parentURL)
996
+ return errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
984
997
  }
985
998
  }
986
999
 
@@ -1187,6 +1200,14 @@ function bundleOwns(bundle, url) {
1187
1200
  return bundle.exists(url.href)
1188
1201
  }
1189
1202
 
1203
+ function isTerminalResolution(url, condition) {
1204
+ const protocol = url.protocol
1205
+
1206
+ if (protocol === 'builtin:' || protocol === 'data:') return true
1207
+
1208
+ return protocol === 'linked:' && condition === 'addon'
1209
+ }
1210
+
1190
1211
  function isThenable(value) {
1191
1212
  return value !== null && typeof value === 'object' && typeof value.then === 'function'
1192
1213
  }
package/lib/module.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const lex = require('bare-module-lexer')
2
+ const { conditionMatches } = require('bare-module-resolve')
2
3
  const strip = require('bare-type-stripper')
3
4
  const binding = require('../binding')
4
5
  const { urlToPath, urlToDirname } = require('./url')
@@ -8,6 +9,8 @@ const { REEXPORT, ADDON, ASSET } = lex.constants
8
9
 
9
10
  const DEFERRED_PROTOCOL = 'deferred:'
10
11
 
12
+ const hostConditions = ['bare', 'node', ...Bare.Addon.host.split('-')]
13
+
11
14
  const UNLINKED = 0
12
15
  const LINKED = 1
13
16
  const EVALUATING = 2
@@ -367,21 +370,15 @@ function onrun(reason, promise, err = reason) {
367
370
  }
368
371
  }
369
372
 
370
- const entryConditions = ['require', 'import', 'asset', 'addon']
371
-
372
373
  function pickCondition(entry, condition) {
373
374
  if (typeof entry === 'string') return entry
374
375
 
375
376
  if (entry === null || typeof entry !== 'object') return null
376
377
 
377
- if (Object.hasOwn(entry, condition)) return pickCondition(entry[condition], condition)
378
-
379
- for (const key of Object.keys(entry)) {
380
- if (key !== 'default' && entryConditions.includes(key) === false) return null
378
+ for (const [, target] of conditionMatches(entry, [condition, ...hostConditions])) {
379
+ return pickCondition(target, condition)
381
380
  }
382
381
 
383
- if (Object.hasOwn(entry, 'default')) return pickCondition(entry.default, condition)
384
-
385
382
  return null
386
383
  }
387
384
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "7.0.1",
3
+ "version": "7.0.3",
4
4
  "description": "The module system that powers Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",