bare-module 7.0.2 → 7.1.0

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/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import Buffer from 'bare-buffer'
2
2
  import URL from 'bare-url'
3
+ import { ResolutionsMap } from 'bare-module-resolve'
3
4
  import { constants } from 'bare-module-traverse'
4
5
  import Protocol from './lib/protocol'
5
6
  import Loader from './lib/loader'
@@ -38,6 +39,11 @@ interface Module {
38
39
  * [Protocols](https://github.com/holepunchto/bare-module#protocols) for usage.
39
40
  */
40
41
  readonly protocol: Protocol
42
+ /**
43
+ * The resolutions map of the module graph, with keys being serialized parent URLs and values
44
+ * being `"imports"` maps.
45
+ */
46
+ readonly resolutions: ResolutionsMap
41
47
  /** The WHATWG `URL` identifier of the module. */
42
48
  readonly url: URL
43
49
  }
package/lib/loader.js CHANGED
@@ -450,6 +450,7 @@ module.exports = class ModuleLoader {
450
450
  engines,
451
451
  ...opts,
452
452
  deferUnresolved: true,
453
+ resolutions: this._resolutions,
453
454
  packages: this._packages,
454
455
  prefixes: this._prefixes,
455
456
  resolve: traverse.resolve.bare
@@ -559,6 +560,10 @@ module.exports = class ModuleLoader {
559
560
  _linkBundle(bundle) {
560
561
  this._bundles.push(bundle)
561
562
 
563
+ for (const href in bundle.resolutions) {
564
+ if (href in this._resolutions === false) this._resolutions[href] = bundle.resolutions[href]
565
+ }
566
+
562
567
  const protocol = this._protocol.extend({
563
568
  exists(context, url) {
564
569
  if (bundleOwns(bundle, url)) return true
@@ -601,13 +606,7 @@ module.exports = class ModuleLoader {
601
606
  }
602
607
  })
603
608
 
604
- this._linkSync(
605
- this._root(new URL(bundle.main), null, {
606
- imports: bundle.imports,
607
- resolutions: bundle.resolutions
608
- }),
609
- protocol
610
- )
609
+ this._linkSync(this._root(new URL(bundle.main), null, { imports: bundle.imports }), protocol)
611
610
  }
612
611
 
613
612
  _ingest(dependency) {
@@ -647,20 +646,10 @@ module.exports = class ModuleLoader {
647
646
 
648
647
  const requested = typeAttributeFor(attributes)
649
648
 
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]
649
+ // An import attribute somewhere else may have changed the type. An import
650
+ // that asks for no type in particular should get the module as it is, or
651
+ // an error, but never the type someone else asked for.
652
+ const expected = requested === undefined ? naturalType : typeByAttribute[requested]
664
653
 
665
654
  if (expected === undefined) {
666
655
  throw errors.UNKNOWN_MODULE_TYPE(
@@ -668,8 +657,10 @@ module.exports = class ModuleLoader {
668
657
  )
669
658
  }
670
659
 
671
- if (expected !== actual) {
672
- throw errors.TYPE_INCOMPATIBLE(`Module '${record.url.href}' is not of type '${requested}'`)
660
+ if (expected !== 0 && expected !== actual) {
661
+ throw errors.TYPE_INCOMPATIBLE(
662
+ `Module '${record.url.href}' is already of type '${attributeByType[actual]}' in the graph`
663
+ )
673
664
  }
674
665
  }
675
666
 
@@ -746,20 +737,22 @@ module.exports = class ModuleLoader {
746
737
  }
747
738
 
748
739
  if (record === null) {
749
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${specifier}'`, specifier, parentURL)
740
+ throw errors.MODULE_NOT_FOUND(
741
+ `Cannot find module '${specifier}' imported from '${parentURL.href}'`,
742
+ specifier,
743
+ parentURL
744
+ )
750
745
  }
751
746
 
752
747
  return record
753
748
  }
754
749
 
755
750
  _cacheResolution(parentURL, specifier, condition, url) {
756
- let imports = this._resolutions[parentURL.href]
751
+ const imports = this._resolutions[parentURL.href]
757
752
 
758
- if (typeof imports !== 'object' || imports === null) {
759
- imports = this._resolutions[parentURL.href] = Object.create(null)
760
- }
761
-
762
- if (specifier in imports) return
753
+ // An entry is taken as every import of its module when that module is
754
+ // linked, so none is started for a module that isn't linked yet.
755
+ if (typeof imports !== 'object' || imports === null || specifier in imports) return
763
756
 
764
757
  imports[specifier] = { [condition]: url.href }
765
758
  }
@@ -807,7 +800,7 @@ module.exports = class ModuleLoader {
807
800
 
808
801
  candidates.push(url)
809
802
 
810
- if (url.protocol === 'builtin:' || url.protocol === 'data:' || this.get(url) !== null) {
803
+ if (isTerminalResolution(url, condition) || this.get(url) !== null) {
811
804
  this._cacheResolution(parentURL, specifier, condition, url)
812
805
 
813
806
  return url
@@ -871,7 +864,7 @@ module.exports = class ModuleLoader {
871
864
 
872
865
  candidates.push(url)
873
866
 
874
- if (url.protocol === 'builtin:' || url.protocol === 'data:' || this.get(url) !== null) {
867
+ if (isTerminalResolution(url, condition) || this.get(url) !== null) {
875
868
  this._cacheResolution(parentURL, specifier, condition, url)
876
869
 
877
870
  return url
@@ -982,28 +975,22 @@ async function drive(generator, protocol, driveLink, semaphore, answers = null)
982
975
  }
983
976
 
984
977
  function notFound(condition, specifier, parentURL, candidates = []) {
978
+ const kind = condition === 'addon' || condition === 'asset' ? condition : 'module'
979
+
980
+ let message = `Cannot find ${kind} '${specifier}' imported from '${parentURL.href}'`
981
+
982
+ if (candidates.length > 0) {
983
+ message += '\nCandidates:'
984
+ message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
985
+ }
986
+
985
987
  switch (condition) {
986
988
  case 'addon':
987
- return errors.ADDON_NOT_FOUND(
988
- `Cannot find addon '${specifier}'`,
989
- specifier,
990
- parentURL,
991
- candidates
992
- )
989
+ return errors.ADDON_NOT_FOUND(message, specifier, parentURL, candidates)
993
990
  case 'asset':
994
- return errors.ASSET_NOT_FOUND(
995
- `Cannot find asset '${specifier}'`,
996
- specifier,
997
- parentURL,
998
- candidates
999
- )
991
+ return errors.ASSET_NOT_FOUND(message, specifier, parentURL, candidates)
1000
992
  default:
1001
- return errors.MODULE_NOT_FOUND(
1002
- `Cannot find module '${specifier}'`,
1003
- specifier,
1004
- parentURL,
1005
- candidates
1006
- )
993
+ return errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
1007
994
  }
1008
995
  }
1009
996
 
@@ -1210,6 +1197,14 @@ function bundleOwns(bundle, url) {
1210
1197
  return bundle.exists(url.href)
1211
1198
  }
1212
1199
 
1200
+ function isTerminalResolution(url, condition) {
1201
+ const protocol = url.protocol
1202
+
1203
+ if (protocol === 'builtin:' || protocol === 'data:') return true
1204
+
1205
+ return protocol === 'linked:' && condition === 'addon'
1206
+ }
1207
+
1213
1208
  function isThenable(value) {
1214
1209
  return value !== null && typeof value === 'object' && typeof value.then === 'function'
1215
1210
  }
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
@@ -60,6 +63,10 @@ module.exports = exports = class Module {
60
63
  return this._loader._protocolFor(this.url)
61
64
  }
62
65
 
66
+ get resolutions() {
67
+ return this._loader._resolutions
68
+ }
69
+
63
70
  // For Node.js compatibility
64
71
  get filename() {
65
72
  return urlToPath(this.url)
@@ -367,21 +374,15 @@ function onrun(reason, promise, err = reason) {
367
374
  }
368
375
  }
369
376
 
370
- const entryConditions = ['require', 'import', 'asset', 'addon']
371
-
372
377
  function pickCondition(entry, condition) {
373
378
  if (typeof entry === 'string') return entry
374
379
 
375
380
  if (entry === null || typeof entry !== 'object') return null
376
381
 
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
382
+ for (const [, target] of conditionMatches(entry, [condition, ...hostConditions])) {
383
+ return pickCondition(target, condition)
381
384
  }
382
385
 
383
- if (Object.hasOwn(entry, 'default')) return pickCondition(entry.default, condition)
384
-
385
386
  return null
386
387
  }
387
388
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "7.0.2",
3
+ "version": "7.1.0",
4
4
  "description": "The module system that powers Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",