bare-module 7.1.0 → 7.2.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/README.md CHANGED
@@ -301,7 +301,9 @@ options = {
301
301
  // The import attributes which instruct how the file or module should be loaded.
302
302
  // `type` is one of the strings `script`, `module`, `json`, `bundle`, `addon`,
303
303
  // `binary` and `text`. A type that is not one of them is turned down rather
304
- // than passed over, as is a `type` that is not a string.
304
+ // than passed over, as is a `type` that is not a string. `imports` is the
305
+ // specifier of an "imports" map to apply to the module and its dependencies,
306
+ // resolved relative to the module calling `require()`.
305
307
  with: { type: 'json' }
306
308
  }
307
309
  ```
@@ -314,10 +316,26 @@ The module representing the entry script where the program was launched.
314
316
 
315
317
  The registry of loaded modules for this module's graph, keyed by URL href.
316
318
 
317
- ### `const path = require.resolve(specifier[, parentURL])`
319
+ ### `const path = require.resolve(specifier[, parentURL][, options])`
318
320
 
319
321
  Use the internal machinery of `require()` to resolve the `specifier` string relative to the URL `parentURL` and return the path string.
320
322
 
323
+ Options include:
324
+
325
+ ```js
326
+ options = {
327
+ // The import attributes which instruct how the file or module would be
328
+ // loaded, as for `require()`. If passed, the graph of the resolved module is
329
+ // linked, but not evaluated, with the attributes applied so that its
330
+ // resolutions are recorded in `module.resolutions`. This allows the module to
331
+ // later be prepared from its path alone, such as when spawning a thread with
332
+ // an imports map applied.
333
+ with: { imports: './imports.json' }
334
+ }
335
+ ```
336
+
337
+ When `parentURL` is passed, the resolutions are recorded for `parentURL` rather than the calling module. This is useful when resolving on behalf of another module.
338
+
321
339
  ### `require.addon([specifier][, parentURL])`
322
340
 
323
341
  Also used to import modules but specifically loads only addon modules. `specifier` is resolved relative to `parentURL` using the [addon resolution](https://github.com/holepunchto/bare-addon-resolve#algorithm) algorithm.
@@ -379,10 +397,12 @@ The directory name of the current module.
379
397
 
380
398
  The file name of the current module.
381
399
 
382
- ### `const href = import.meta.resolve(specifier[, parentURL])`
400
+ ### `const href = import.meta.resolve(specifier[, parentURL][, options])`
383
401
 
384
402
  A module-relative resolution function which returns the URL string for the module. The `specifier` is a string which is resolved relative to the `parentURL` which is a WHATWG URL.
385
403
 
404
+ Options are the same as for [`require.resolve()`](#const-path--requireresolvespecifier-parenturl-options).
405
+
386
406
  ### `import.meta.addon([specifier][, parentURL])`
387
407
 
388
408
  Also used to import modules but specifically loads only addon modules. `specifier` is resolved relative to `parentURL` using the [addon resolution](https://github.com/holepunchto/bare-addon-resolve#algorithm) algorithm.
@@ -589,6 +609,18 @@ Link the module graph rooted at `entry`, a WHATWG `URL`, awaiting each read thro
589
609
 
590
610
  The synchronous equivalent of `loader.link()`. It drives the protocol's synchronous methods (`resolveSync`, `existsSync`, `readSync`, and `listSync`), which throw an `UNEXPECTED_PROMISE` error when the protocol can only answer asynchronously.
591
611
 
612
+ ### `const modules = await loader.instantiate(urls[, options])`
613
+
614
+ Compile the modules at `urls` and everything they reach, without evaluating any of them. `urls` is a WHATWG `URL`, a string, or any number of either. Returns the module records.
615
+
616
+ This is the phase `loader.link()` performs on the way to an entry, for modules the graph already holds and is not entering through, so it answers whether a source can be run at all without any of the consequences of running it. Unlike `link()` it does not set `loader.main`.
617
+
618
+ A module the graph has already compiled is left as it is, so this is asked after a `patch()` rather than before one. What it answers is whether the source compiles, and not whether everything it names exists: an unresolved specifier is deferred, which is what lets a module name something that is not there on this platform and never ask for it.
619
+
620
+ ### `const modules = loader.instantiateSync(urls[, options])`
621
+
622
+ The synchronous equivalent of `loader.instantiate()`.
623
+
592
624
  ### `const exports = await loader.import(entry[, options])`
593
625
 
594
626
  Link and evaluate the graph rooted at `entry`, returning its exports. Awaits the entry's evaluation, so a top-level `await` in the entry settles before the exports are returned.
@@ -597,6 +629,16 @@ Link and evaluate the graph rooted at `entry`, returning its exports. Awaits the
597
629
 
598
630
  The synchronous equivalent of `loader.import()`. It cannot await, so a top-level `await` in the entry is unsupported.
599
631
 
632
+ ### `const urls = loader.patch(bundle)`
633
+
634
+ Make the contents of `bundle` what the graph reads, in front of whatever it read before, and forget what it knew about each key. Returns the URLs the patch covers. A later patch of the same key wins over an earlier one.
635
+
636
+ Forgetting a module is not re-reading it: nothing runs again until something asks for it, and whoever already imported it keeps what it exported. Reaching a patched module through an importer the graph still holds requires evicting that importer too.
637
+
638
+ ### `const urls = loader.evict(urls)`
639
+
640
+ Forget what the graph knows about the modules at `urls`, a WHATWG `URL`, a string, or any number of either, so that they are read and resolved again when next asked for. Returns the URLs that were known.
641
+
600
642
  ### `const module = loader.get(url)`
601
643
 
602
644
  Return the module record cached under `url`, a WHATWG `URL`, or `null` if none is loaded.
package/index.d.ts CHANGED
@@ -22,7 +22,12 @@ interface Attributes {
22
22
  * How the module should be loaded: one of `'script'`, `'module'`, `'json'`, `'bundle'`,
23
23
  * `'addon'`, `'binary'`, or `'text'`.
24
24
  */
25
- type: Lowercase<keyof typeof constants>
25
+ type?: Lowercase<keyof typeof constants>
26
+ /**
27
+ * The specifier of an `"imports"` map to apply to the module and its dependencies, resolved
28
+ * relative to the parent URL.
29
+ */
30
+ imports?: string
26
31
  }
27
32
 
28
33
  interface Module {
@@ -98,7 +103,22 @@ declare namespace Module {
98
103
  (parentURL: string | URL, opts?: RequireOptions): unknown
99
104
  main: Module
100
105
  cache: Cache
101
- resolve: (specifier: string, parentURL?: URL) => string
106
+ /**
107
+ * Resolve `specifier` relative to `parentURL` without evaluating the matching module. If
108
+ * import attributes are passed, the graph of the module is linked with them so that its
109
+ * resolutions are recorded.
110
+ * @param specifier - The module specifier to resolve.
111
+ * @param parentURL - The WHATWG `URL` to resolve `specifier` relative to. Defaults to the URL
112
+ * of the module that `require()` was created for.
113
+ * @param opts - Resolve options; `with` holds the import attributes.
114
+ * @returns The path of the resolved module.
115
+ * @throws {MODULE_NOT_FOUND} no module matching `specifier` could be found relative to
116
+ * `parentURL`.
117
+ */
118
+ resolve: {
119
+ (specifier: string, opts?: RequireOptions): string
120
+ (specifier: string, parentURL?: string | URL, opts?: RequireOptions): string
121
+ }
102
122
  /**
103
123
  * Resolve `specifier` relative to `parentURL` and evaluate the matching addon.
104
124
  * @param specifier - The addon specifier to resolve. Defaults to `'.'`.
package/lib/loader.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import Buffer from 'bare-buffer'
2
+ import Bundle from 'bare-bundle'
2
3
  import URL from 'bare-url'
3
4
  import { Builtins, ImportsMap, ResolutionsMap } from 'bare-module-resolve'
4
- import { Attributes, Cache, Module } from '..'
5
+ import Module, { Attributes, Cache } from '..'
5
6
  import Protocol from './protocol'
6
7
 
7
8
  interface ModuleLoader {
@@ -20,11 +21,27 @@ interface ModuleLoader {
20
21
 
21
22
  get(url: URL): Module | null
22
23
 
23
- link(entry: URL, source?: Buffer | string | null, opts?: LinkOptions): Promise<Module>
24
- linkSync(entry: URL, source?: Buffer | string | null, opts?: LinkOptions): Module
24
+ patch(bundle: Bundle): URL[]
25
+ evict(urls: URL | string | Iterable<URL | string>): URL[]
25
26
 
26
- import(entry: URL, opts?: ImportOptions): Promise<unknown>
27
- importSync(entry: URL, opts?: ImportOptions): unknown
27
+ instantiate(
28
+ urls: URL | string | Iterable<URL | string>,
29
+ opts?: ModuleLoader.LinkOptions
30
+ ): Promise<Module[]>
31
+ instantiateSync(
32
+ urls: URL | string | Iterable<URL | string>,
33
+ opts?: ModuleLoader.LinkOptions
34
+ ): Module[]
35
+
36
+ link(
37
+ entry: URL,
38
+ source?: Buffer | string | null,
39
+ opts?: ModuleLoader.LinkOptions
40
+ ): Promise<Module>
41
+ linkSync(entry: URL, source?: Buffer | string | null, opts?: ModuleLoader.LinkOptions): Module
42
+
43
+ import(entry: URL, opts?: ModuleLoader.ImportOptions): Promise<unknown>
44
+ importSync(entry: URL, opts?: ModuleLoader.ImportOptions): unknown
28
45
  }
29
46
 
30
47
  declare class ModuleLoader {
package/lib/loader.js CHANGED
@@ -98,6 +98,14 @@ function assertURL(url, name) {
98
98
  throw new TypeError(`${name} must be a URL. Received type ${typeof url} (${url})`)
99
99
  }
100
100
 
101
+ function* eachURL(urls) {
102
+ if (isURL(urls) || typeof urls === 'string') urls = [urls]
103
+
104
+ for (const url of urls) {
105
+ yield assertURL(typeof url === 'string' ? new URL(url) : url, 'URL')
106
+ }
107
+ }
108
+
101
109
  function typeAttributeFor(attributes) {
102
110
  if (attributes === undefined || attributes === null) return undefined
103
111
 
@@ -180,6 +188,7 @@ module.exports = class ModuleLoader {
180
188
  this._resolutions = resolutionsFor(resolutions)
181
189
  this._manifests = Object.create(null)
182
190
  this._bundles = []
191
+ this._patches = null
183
192
  this._visited = new Set()
184
193
  this._packages = new Map()
185
194
  this._prefixes = new Map()
@@ -220,6 +229,73 @@ module.exports = class ModuleLoader {
220
229
  return this._cached(assertURL(url, 'URL').href) || null
221
230
  }
222
231
 
232
+ patch(bundle) {
233
+ if (this._patches === null) {
234
+ this._patches = new Map()
235
+ this._protocol = patching(this._protocol, this._patches)
236
+ }
237
+
238
+ const patched = []
239
+
240
+ for (const href of bundle.keys()) {
241
+ const url = new URL(href)
242
+
243
+ this._patches.set(href, bundle)
244
+
245
+ this._forget(url)
246
+
247
+ const imports = bundle.resolutions[href]
248
+
249
+ if (imports) this._resolutions[href] = imports
250
+
251
+ patched.push(url)
252
+ }
253
+
254
+ return patched
255
+ }
256
+
257
+ evict(urls) {
258
+ const evicted = []
259
+
260
+ for (const url of eachURL(urls)) {
261
+ if (this._forget(url)) evicted.push(url)
262
+
263
+ delete this._resolutions[url.href]
264
+ }
265
+
266
+ return evicted
267
+ }
268
+
269
+ async instantiate(urls, opts = {}) {
270
+ const records = []
271
+
272
+ for (const url of eachURL(urls)) {
273
+ if (url.protocol === 'builtin:') this._lookup(url)
274
+ else await this._link(this._root(url, null, opts), this._protocolFor(url))
275
+
276
+ records.push(this._lookup(traverse.alias(url, opts)))
277
+ }
278
+
279
+ this._instantiate()
280
+
281
+ return records
282
+ }
283
+
284
+ instantiateSync(urls, opts = {}) {
285
+ const records = []
286
+
287
+ for (const url of eachURL(urls)) {
288
+ if (url.protocol === 'builtin:') this._lookup(url)
289
+ else this._linkSync(this._root(url, null, opts), this._protocolFor(url))
290
+
291
+ records.push(this._lookup(traverse.alias(url, opts)))
292
+ }
293
+
294
+ this._instantiate()
295
+
296
+ return records
297
+ }
298
+
223
299
  async link(entry, source = null, opts = {}) {
224
300
  assertURL(entry, 'Entry')
225
301
 
@@ -397,6 +473,24 @@ module.exports = class ModuleLoader {
397
473
  return null
398
474
  }
399
475
 
476
+ _forget(url) {
477
+ const { href } = url
478
+
479
+ const known = href in this._cache
480
+
481
+ delete this._cache[href]
482
+
483
+ this._visited.delete(href)
484
+ this._packages.delete(href)
485
+
486
+ this._answers.listed.delete(href)
487
+ this._answers.resolved.delete(href)
488
+
489
+ if (this._main !== null && this._main.url.href === href) this._main = null
490
+
491
+ return known
492
+ }
493
+
400
494
  _builtinRecord(url) {
401
495
  const name = url.pathname
402
496
 
@@ -686,24 +780,20 @@ module.exports = class ModuleLoader {
686
780
  require.main = loader.main
687
781
  require.cache = loader.cache
688
782
 
689
- require.resolve = function (specifier, parentURL = referrer.url) {
690
- return urlToPath(loader._resolveSync(specifier, toURL(parentURL, referrer.url), 'require'))
783
+ require.resolve = function (specifier, parentURL, opts) {
784
+ return urlToPath(loader._resolveModule(referrer, specifier, 'require', parentURL, opts))
691
785
  }
692
786
 
693
- require.asset = function (specifier, parentURL = referrer.url) {
694
- return urlToPath(
695
- loader._resolveArtifact(referrer, specifier, 'asset', toURL(parentURL, referrer.url))
696
- )
787
+ require.asset = function (specifier, parentURL) {
788
+ return urlToPath(loader._resolveArtifact(referrer, specifier, 'asset', parentURL))
697
789
  }
698
790
 
699
- require.addon = function (specifier = '.', parentURL = referrer.url) {
700
- return loader._addon(referrer, specifier, toURL(parentURL, referrer.url))
791
+ require.addon = function (specifier = '.', parentURL) {
792
+ return loader._addon(referrer, specifier, parentURL)
701
793
  }
702
794
 
703
- require.addon.resolve = function (specifier = '.', parentURL = referrer.url) {
704
- return urlToPath(
705
- loader._resolveArtifact(referrer, specifier, 'addon', toURL(parentURL, referrer.url))
706
- )
795
+ require.addon.resolve = function (specifier = '.', parentURL) {
796
+ return urlToPath(loader._resolveArtifact(referrer, specifier, 'addon', parentURL))
707
797
  }
708
798
 
709
799
  require.addon.host = Bare.Addon.host
@@ -731,7 +821,10 @@ module.exports = class ModuleLoader {
731
821
  let record = this._lookup(url)
732
822
 
733
823
  if (record === null) {
734
- this.linkSync(url, null, { attributes, importType: lex.constants.REQUIRE })
824
+ this.linkSync(url, null, {
825
+ attributes: this._resolveAttributesSync(attributes, parentURL),
826
+ importType: lex.constants.REQUIRE
827
+ })
735
828
 
736
829
  record = this.get(url)
737
830
  }
@@ -887,13 +980,67 @@ module.exports = class ModuleLoader {
887
980
  throw notFound(condition, specifier, parentURL, candidates)
888
981
  }
889
982
 
890
- _resolveArtifact(referrer, specifier, condition, parentURL = referrer.url) {
983
+ _resolveModule(referrer, specifier, condition, parentURL, opts) {
984
+ if (isOptions(parentURL)) {
985
+ opts = parentURL
986
+ parentURL = undefined
987
+ }
988
+
989
+ parentURL = parentURLFor(referrer, parentURL)
990
+
991
+ const url = this._resolveSync(specifier, parentURL, condition)
992
+
993
+ const attributes = opts && opts.with
994
+
995
+ // The graph is linked as it would be when the resolved module is loaded,
996
+ // without being evaluated, so that its resolutions are recorded for anyone
997
+ // preparing it from the resolved URL alone.
998
+ if (attributes !== undefined && attributes !== null) {
999
+ const importType =
1000
+ lex.constants.RESOLVE |
1001
+ (condition === 'require' ? lex.constants.REQUIRE : lex.constants.IMPORT)
1002
+
1003
+ this._linkResolved(url, parentURL, attributes, importType)
1004
+ }
1005
+
1006
+ return url
1007
+ }
1008
+
1009
+ _linkResolved(url, parentURL, attributes, importType) {
1010
+ if (this._lookup(url) !== null) return
1011
+
1012
+ attributes = this._resolveAttributesSync(attributes, parentURL)
1013
+
1014
+ this._linkSync(this._root(url, null, { attributes, importType }), this._protocolFor(url))
1015
+
1016
+ this._instantiate()
1017
+ }
1018
+
1019
+ _resolveAttributesSync(attributes, parentURL) {
1020
+ if (!hasImportsAttribute(attributes)) return attributes
1021
+
1022
+ const url = this._resolveSync(attributes.imports, parentURL, 'default')
1023
+
1024
+ return { ...attributes, imports: url.href }
1025
+ }
1026
+
1027
+ async _resolveAttributes(attributes, parentURL) {
1028
+ if (!hasImportsAttribute(attributes)) return attributes
1029
+
1030
+ const url = await this._resolve(attributes.imports, parentURL, 'default')
1031
+
1032
+ return { ...attributes, imports: url.href }
1033
+ }
1034
+
1035
+ _resolveArtifact(referrer, specifier, condition, parentURL) {
1036
+ parentURL = parentURLFor(referrer, parentURL)
1037
+
891
1038
  const url = parentURL === referrer.url ? referrer._resolve(specifier, condition) : null
892
1039
 
893
1040
  return url === null ? this._resolveSync(specifier, parentURL, condition) : url
894
1041
  }
895
1042
 
896
- _addon(referrer, specifier, parentURL = referrer.url) {
1043
+ _addon(referrer, specifier, parentURL) {
897
1044
  const url = this._resolveArtifact(referrer, specifier, 'addon', parentURL)
898
1045
 
899
1046
  const record = this._lookup(url) || this._addonRecord(url)
@@ -1181,6 +1328,30 @@ function resolveEntry(specifier, condition) {
1181
1328
  return { type, specifier, names: [], attributes: {}, position: [0, 0, 0] }
1182
1329
  }
1183
1330
 
1331
+ function patching(protocol, patches) {
1332
+ return protocol.extend({
1333
+ exists(context, url) {
1334
+ return patches.has(url.href) || context.exists(url)
1335
+ },
1336
+
1337
+ existsSync(context, url) {
1338
+ return patches.has(url.href) || context.existsSync(url)
1339
+ },
1340
+
1341
+ read(context, url) {
1342
+ const patch = patches.get(url.href)
1343
+
1344
+ return patch === undefined ? context.read(url) : patch.read(url.href)
1345
+ },
1346
+
1347
+ readSync(context, url) {
1348
+ const patch = patches.get(url.href)
1349
+
1350
+ return patch === undefined ? context.readSync(url) : patch.read(url.href)
1351
+ }
1352
+ })
1353
+ }
1354
+
1184
1355
  function toURL(value, base) {
1185
1356
  if (isURL(value)) return value
1186
1357
 
@@ -1197,6 +1368,20 @@ function bundleOwns(bundle, url) {
1197
1368
  return bundle.exists(url.href)
1198
1369
  }
1199
1370
 
1371
+ function parentURLFor(referrer, parentURL) {
1372
+ return parentURL === undefined ? referrer.url : toURL(parentURL, referrer.url)
1373
+ }
1374
+
1375
+ function isOptions(value) {
1376
+ return typeof value === 'object' && value !== null && !URL.isURL(value)
1377
+ }
1378
+
1379
+ function hasImportsAttribute(attributes) {
1380
+ return (
1381
+ typeof attributes === 'object' && attributes !== null && typeof attributes.imports === 'string'
1382
+ )
1383
+ }
1384
+
1200
1385
  function isTerminalResolution(url, condition) {
1201
1386
  const protocol = url.protocol
1202
1387
 
package/lib/module.js CHANGED
@@ -285,7 +285,7 @@ async function ondynamicimport(specifier, attributes, referrerName, id) {
285
285
  url = await loader._resolve(specifier, referrer.url, 'import')
286
286
 
287
287
  record = await loader.link(url, null, {
288
- attributes,
288
+ attributes: await loader._resolveAttributes(attributes, referrer.url),
289
289
  importType: lex.constants.IMPORT | lex.constants.DYNAMIC
290
290
  })
291
291
  } else {
@@ -343,24 +343,20 @@ function onmeta(id, meta) {
343
343
  meta.dirname = record.dirname
344
344
  meta.filename = record.filename
345
345
 
346
- meta.resolve = function (specifier) {
347
- const url = record._resolve(specifier, 'import')
348
-
349
- return url === null
350
- ? record._loader._resolveSync(specifier, record.url, 'import').href
351
- : url.href
346
+ meta.resolve = function (specifier, parentURL, opts) {
347
+ return record._loader._resolveModule(record, specifier, 'import', parentURL, opts).href
352
348
  }
353
349
 
354
- meta.asset = function (specifier) {
355
- return record._loader._resolveArtifact(record, specifier, 'asset').href
350
+ meta.asset = function (specifier, parentURL) {
351
+ return record._loader._resolveArtifact(record, specifier, 'asset', parentURL).href
356
352
  }
357
353
 
358
- meta.addon = function (specifier = '.') {
359
- return record._loader._addon(record, specifier)
354
+ meta.addon = function (specifier = '.', parentURL) {
355
+ return record._loader._addon(record, specifier, parentURL)
360
356
  }
361
357
 
362
- meta.addon.resolve = function (specifier = '.') {
363
- return record._loader._resolveArtifact(record, specifier, 'addon').href
358
+ meta.addon.resolve = function (specifier = '.', parentURL) {
359
+ return record._loader._resolveArtifact(record, specifier, 'addon', parentURL).href
364
360
  }
365
361
 
366
362
  meta.addon.host = Bare.Addon.host
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "description": "The module system that powers Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -47,9 +47,9 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "bare-bundle": "^1.11.0",
50
- "bare-module-lexer": "^1.6.1",
50
+ "bare-module-lexer": "^1.7.0",
51
51
  "bare-module-resolve": "^1.12.3",
52
- "bare-module-traverse": "~2.5.5",
52
+ "bare-module-traverse": "~2.5.7",
53
53
  "bare-path": "^3.0.0",
54
54
  "bare-type-stripper": "^0.1.2",
55
55
  "bare-url": "^2.0.1",