bare-module 7.0.2 → 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
@@ -807,7 +803,7 @@ module.exports = class ModuleLoader {
807
803
 
808
804
  candidates.push(url)
809
805
 
810
- if (url.protocol === 'builtin:' || url.protocol === 'data:' || this.get(url) !== null) {
806
+ if (isTerminalResolution(url, condition) || this.get(url) !== null) {
811
807
  this._cacheResolution(parentURL, specifier, condition, url)
812
808
 
813
809
  return url
@@ -871,7 +867,7 @@ module.exports = class ModuleLoader {
871
867
 
872
868
  candidates.push(url)
873
869
 
874
- if (url.protocol === 'builtin:' || url.protocol === 'data:' || this.get(url) !== null) {
870
+ if (isTerminalResolution(url, condition) || this.get(url) !== null) {
875
871
  this._cacheResolution(parentURL, specifier, condition, url)
876
872
 
877
873
  return url
@@ -982,28 +978,22 @@ async function drive(generator, protocol, driveLink, semaphore, answers = null)
982
978
  }
983
979
 
984
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
+
985
990
  switch (condition) {
986
991
  case 'addon':
987
- return errors.ADDON_NOT_FOUND(
988
- `Cannot find addon '${specifier}'`,
989
- specifier,
990
- parentURL,
991
- candidates
992
- )
992
+ return errors.ADDON_NOT_FOUND(message, specifier, parentURL, candidates)
993
993
  case 'asset':
994
- return errors.ASSET_NOT_FOUND(
995
- `Cannot find asset '${specifier}'`,
996
- specifier,
997
- parentURL,
998
- candidates
999
- )
994
+ return errors.ASSET_NOT_FOUND(message, specifier, parentURL, candidates)
1000
995
  default:
1001
- return errors.MODULE_NOT_FOUND(
1002
- `Cannot find module '${specifier}'`,
1003
- specifier,
1004
- parentURL,
1005
- candidates
1006
- )
996
+ return errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
1007
997
  }
1008
998
  }
1009
999
 
@@ -1210,6 +1200,14 @@ function bundleOwns(bundle, url) {
1210
1200
  return bundle.exists(url.href)
1211
1201
  }
1212
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
+
1213
1211
  function isThenable(value) {
1214
1212
  return value !== null && typeof value === 'object' && typeof value.then === 'function'
1215
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.2",
3
+ "version": "7.0.3",
4
4
  "description": "The module system that powers Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",