bare-module-traverse 2.7.0 → 2.7.2

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
@@ -120,7 +120,7 @@ next.value = {
120
120
 
121
121
  **Dependency node**
122
122
 
123
- A fully resolved node of the module graph and the traversal's output. This is what the iterable forms yield to the caller.
123
+ A fully resolved node of the module graph and the traversal's output. This is what the iterable forms yield to the caller. A module imported under several `host` or `wasi` attributes is walked once for each, so the same URL may be yielded more than once.
124
124
 
125
125
  ```js
126
126
  next.value = {
@@ -141,7 +141,10 @@ next.value = {
141
141
  exports: [
142
142
  // See https://github.com/holepunchto/bare-module-lexer#api
143
143
  ]
144
- }
144
+ },
145
+ // The URL of the WASI provider of a WebAssembly addon, if its importer
146
+ // named one with the `wasi` attribute.
147
+ wasi: 'string'
145
148
  }
146
149
  }
147
150
  ```
@@ -206,7 +209,7 @@ function drive(generator) {
206
209
  }
207
210
 
208
211
  while (queue.length > 0 || deferred.length > 0) {
209
- drive(queue.length > 0 ? queue.pop() : deferred.shift())
212
+ drive(queue.length > 0 ? queue.shift() : deferred.shift())
210
213
  }
211
214
  ```
212
215
 
package/index.d.ts CHANGED
@@ -26,6 +26,11 @@ interface Dependency {
26
26
  imports: Import[]
27
27
  exports: Export[]
28
28
  }
29
+ /**
30
+ * The URL of the WASI provider of a WebAssembly addon, if its importer named
31
+ * one with the `wasi` attribute.
32
+ */
33
+ wasi?: string
29
34
  }
30
35
 
31
36
  type AliasableExtension =
@@ -74,6 +79,12 @@ interface TraverseOptions extends ResolveOptions {
74
79
  * system; a tool that only links wants the error.
75
80
  */
76
81
  deferUnresolved?: boolean
82
+ /**
83
+ * The URL the entry is mounted at when it is a bundle, for consumers that
84
+ * load it with an explicit mount. The addons and assets it declares resolve
85
+ * relative to this. Defaults to the URL of the entry itself.
86
+ */
87
+ mount?: URL | string
77
88
  }
78
89
 
79
90
  /**
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ const Bundle = require('bare-bundle')
1
2
  const { lookupPackageScope, conditionMatches } = require('bare-module-resolve')
2
3
  const lex = require('bare-module-lexer')
3
4
  const MIME = require('bare-mime')
@@ -194,9 +195,15 @@ exports.alias = function alias(url, opts = {}) {
194
195
  exports.module = function* (url, source, attributes, artifacts, visited, opts = {}) {
195
196
  attributes = attributes || {}
196
197
 
198
+ // The mount only applies to the module the traversal starts from.
199
+ const { mount } = opts
200
+
201
+ if (mount !== undefined) opts = { ...opts, mount: undefined }
202
+
197
203
  if (opts.contexts === undefined) opts = { ...opts, contexts: new Map() }
198
204
  if (opts.addonContexts === undefined) opts = { ...opts, addonContexts: new Map() }
199
205
  if (opts.resolvedImports === undefined) opts = { ...opts, resolvedImports: new Map() }
206
+ if (opts.lexers === undefined) opts = { ...opts, lexers: new Map() }
200
207
 
201
208
  // The host and WASI attributes set the context that the addons of the module
202
209
  // and everything it imports load in. A graph is walked again for each context
@@ -262,6 +269,16 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
262
269
  }
263
270
  }
264
271
 
272
+ // A bundle declares its own artifacts, whether or not its imports have been
273
+ // resolved before.
274
+ if (
275
+ asset === false &&
276
+ artifacts !== null &&
277
+ moduleType(url, attributes, null, opts) === constants.BUNDLE
278
+ ) {
279
+ addBundleArtifacts(source, mountFor(url, mount), artifacts)
280
+ }
281
+
265
282
  // A module resolves its imports once, however many contexts it is loaded in,
266
283
  // so a context that reaches it after another walks what the other resolved.
267
284
  // The addons that are reached are then checked against the context.
@@ -322,6 +339,8 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
322
339
  ...opts,
323
340
  importType: 0
324
341
  })
342
+
343
+ opts.lexers.set(url.href, lexer)
325
344
  } else if (type === constants.ADDON && artifacts !== null) {
326
345
  yield* exports.addons(url, artifacts, visited, opts)
327
346
  }
@@ -409,11 +428,13 @@ exports.preresolved = function* (
409
428
 
410
429
  const parentURL = url
411
430
 
431
+ const { lexers = null } = opts
432
+
412
433
  // The resolutions map leaves out the import attributes, so they are read
413
- // from the source instead.
434
+ // from the source instead, unless another context already lexed it.
414
435
  const lexed =
415
436
  opts.asset !== true && isJavaScript(moduleType(url, attributes, null, opts))
416
- ? lex(source)
437
+ ? (lexers !== null && lexers.get(url.href)) || lex(source)
417
438
  : null
418
439
 
419
440
  let info = null
@@ -853,6 +874,19 @@ exports.assertAddonContext = function assertAddonContext(url, scope, context, ad
853
874
  }
854
875
  }
855
876
 
877
+ function addBundleArtifacts(source, mount, artifacts) {
878
+ const bundle = Bundle.from(source).mount(mount)
879
+
880
+ for (const href of bundle.addons) addURL(artifacts.addons, new URL(href))
881
+ for (const href of bundle.assets) addURL(artifacts.assets, new URL(href))
882
+ }
883
+
884
+ function mountFor(url, mount) {
885
+ const href = mount === undefined ? url.href : new URL(mount).href
886
+
887
+ return href.endsWith('/') ? href : href + '/'
888
+ }
889
+
856
890
  function isJavaScript(type) {
857
891
  return type === constants.SCRIPT || type === constants.MODULE
858
892
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.7.0",
3
+ "version": "2.7.2",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {
@@ -55,6 +55,7 @@
55
55
  "homepage": "https://github.com/holepunchto/bare-module-traverse#readme",
56
56
  "dependencies": {
57
57
  "bare-addon-resolve": "^1.5.0",
58
+ "bare-bundle": "^1.11.0",
58
59
  "bare-mime": "^1.0.0",
59
60
  "bare-module-lexer": "^1.6.0",
60
61
  "bare-module-resolve": "^1.7.0"