bare-module-traverse
Low-level module graph traversal for Bare
v2.7.6bare-module-traverse — Low-level module graph traversal for Bare.
npm i bare-module-traverseUsage
For synchronous traversal:
const traverse = require('bare-module-traverse')
function readModule(url) {
// Read `url` if it exists, otherwise `null`
}
function* listPrefix(url) {
// Yield URLs that have `url` as a prefix. The list may be empty.
}
for (const dependency of traverse(new URL('file:///directory/file.js'), readModule, listPrefix)) {
console.log(dependency)
}For asynchronous traversal:
const traverse = require('bare-module-traverse')
async function readModule(url) {
// Read `url` if it exists, otherwise `null`
}
async function* listPrefix(url) {
// Yield URLs that have `url` as a prefix. The list may be empty.
}
for await (const dependency of traverse(
new URL('file:///directory/file.js'),
readModule,
listPrefix
)) {
console.log(dependency)
}API
Functions
traverse
traverse(entry: URL, readModule: (url: URL) => Buffer | string | null, listPrefix?: (url: URL) => Iterable<URL>, probeModule?: (url: URL) => boolean | undefined, resolveModule?: (url: URL) => URL): Iterable<Dependency>Traverse the module graph rooted at entry, which must be a WHATWG URL instance. readModule is called with a URL instance for every module to be read and must either return the module source, if it exists, or null. listPrefix is called with a URL instance of every prefix to be listed and must yield URL instances that have the specified URL as a prefix. If not provided, prefixes won't be traversed. If readModule returns a promise or listPrefix returns a promise generator, synchronous iteration is not supported.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | URL | — | The WHATWG URL of the entry module to root the graph at. |
readModule | (url: URL) => Buffer | string | null | — | Called with the URL of each module to read; returns its source as a Buffer or string, or null if it does not exist. Returning a promise disables synchronous iteration. |
listPrefix? | (url: URL) => Iterable<URL> | — | Called with the URL of each prefix to list; must yield the URLs that have it as a prefix. If omitted, prefixes are not traversed. |
probeModule? | (url: URL) => boolean | undefined | — | Called with the URL of each module to probe for existence; returns a boolean, or undefined to fall back to readModule. |
resolveModule? | (url: URL) => URL | — | Called with each resolution URL to transform; returns the URL to use in its place. Defaults to the identity function. |
Returns Iterable<Dependency> — An iterable of resolved Dependency records for the module graph; asynchronous when any callback returns a promise.
Changed in v2.7.0: the traversal walks queued subgraphs first in, first out, where before it took the most recently queued one first. The same graph now yields its Dependency records in a different order. For example, the package.json that a module reaches is yielded ahead of the modules it imports, where in v2.6.0 it came after them.
traverse.addons
traverse.addons(parentURL: URL, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): TraversalParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
parentURL | URL | — | — |
artifacts | Artifacts | — | — |
visited | Set<string> | — | — |
opts? | TraverseOptions | — | — |
traverse.assertAddonContext
traverse.assertAddonContext(url: URL, scope: string, context: Partial<AddonContext>, addonContexts: Map<string, AddonContext>): voidRecord in addonContexts that the package at scope loads its addons in context, and throw if that conflicts with what is already recorded or with url. The traversal calls this for every addon it resolves, so a package's addons load in one context for the whole graph. A driver that loads addons on its own can call it to apply the same rule. See WebAssembly addons. Added in v2.6.0.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | The addon being loaded. A builtin: URL is accepted without being recorded. |
scope | string | — | The key to record the context under. The traversal uses the URL of the importing package's package.json, or the URL of the importing module when it has no package. |
context | Partial<AddonContext> | — | The host and the wasi provider the addon loads with. wasi is the URL of the provider module, as a string. A value left out is null, which means the defaults. |
addonContexts | Map<string, AddonContext> | — | The contexts recorded so far, by scope. A scope with no entry gets one for context. |
Throws
ADDON_HOST_INCOMPATIBLE—context.hostis set andurlis not a WebAssembly addon, which is a URL with a path that ends in.wasm, orscopewas recorded with a differenthost.ADDON_WASI_INCOMPATIBLE—scopewas recorded with a differentwasiprovider. Only a WebAssembly addon has awasiprovider, socontext.wasiis ignored for any otherurl.
traverse.assets
traverse.assets(patterns: ConditionalSpecifier, parentURL: URL, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): TraversalParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
patterns | ConditionalSpecifier | — | — |
parentURL | URL | — | — |
artifacts | Artifacts | — | — |
visited | Set<string> | — | — |
opts? | TraverseOptions | — | — |
traverse.imports
traverse.imports(parentURL: URL, source: string | Buffer, imports: ImportsMap, artifacts: Artifacts, lexer: {
imports: Import[]
exports: Export[]
}, visited: Set<string>, opts?: TraverseOptions): TraversalParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
parentURL | URL | — | — |
source | string | Buffer | — | — |
imports | ImportsMap | — | — |
artifacts | Artifacts | — | — |
lexer | { imports: Import[]; exports: Export[] } | — | — |
visited | Set<string> | — | — |
opts? | TraverseOptions | — | — |
traverse.link
traverse.link(entry: Import, specifier: string, condition: string, parentURL: URL, imports: ImportsMap, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): TraversalResolve one import and record the result in imports. Before that, it resolves the specifier of each of the imports and wasi attributes of entry as a module and records it in imports too. The wasi attribute is new in v2.6.0. See The host and wasi import attributes.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | Import | — | — |
specifier | string | — | — |
condition | string | — | — |
parentURL | URL | — | — |
imports | ImportsMap | — | — |
artifacts | Artifacts | — | — |
visited | Set<string> | — | — |
opts? | TraverseOptions | — | — |
traverse.module
traverse.module(url: URL, source: string | Buffer, attributes: Record<string, string> | null, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): TraversalParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | — |
source | string | Buffer | — | — |
attributes | Record<string, string> | null | — | The import attributes of the import that reached url. As of v2.6.0 they include host and wasi; see WebAssembly addons. |
artifacts | Artifacts | — | — |
visited | Set<string> | — | — |
opts? | TraverseOptions | — | — |
traverse.package
traverse.package(url: URL, source: string | Buffer, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): TraversalParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | — |
source | string | Buffer | — | — |
artifacts | Artifacts | — | — |
visited | Set<string> | — | — |
opts? | TraverseOptions | — | — |
traverse.preresolved
traverse.preresolved(url: URL, source: string | Buffer, attributes: Record<string, string> | null, resolution: ResolutionsMap, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): TraversalTraverse url using the imports recorded for it in resolution instead of resolving its specifiers again. The generator returns false when resolution has no imports map for url, and traverse.module then resolves the module as usual.
As of v2.7.0, traverse.module also calls it for a module that the same traversal already resolved in another context, with the imports recorded then in place of the resolutions option. See The host and wasi import attributes.
A resolutions map doesn't hold import attributes, so they are read from source. As of v2.6.0 the type, host, and wasi attributes of each import are applied this way. In v2.5.9 the type attribute was ignored for an import that the map resolved, so a .txt file imported with type: 'binary' was traversed as text. The naturalType of its dependency is now the type the module has without the attribute, constants.TEXT for that file, where in v2.5.9 it was always equal to type.
An entry that the map lists under the addon condition is traversed as an addon whatever its file extension, so its dependency has type constants.ADDON. In v2.5.9 its type came from its extension, and a .wasm file was a script.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | — |
source | string | Buffer | — | — |
attributes | Record<string, string> | null | — | The import attributes that url was imported with. They set the type of the module as they do for traverse.module. Added in v2.6.0, ahead of resolution, so a call written for v2.5.9 passes resolution in the wrong position. |
resolution | ResolutionsMap | — | — |
artifacts | Artifacts | — | — |
visited | Set<string> | — | — |
opts? | TraverseOptions | — | — |
Constants and variables
traverse.constants
traverse.constants: {
SCRIPT: number
MODULE: number
JSON: number
BUNDLE: number
ADDON: number
BINARY: number
TEXT: number
}Types
traverse.Traversal
type Traversal = Generator<
| { module: URL; artifact: boolean }
| { probe: URL }
| { resolution: URL }
| { prefix: URL }
| { links: Traversal[] }
| { children: Traversal; deferred: boolean }
| { dependency: Dependency },
boolean,
void | URL | URL[] | Buffer | string | boolean | null
>TraverseOptions
interface TraverseOptions {
defaultType?: number
importType?: number
artifacts?: boolean
aliases?: Record<string, AliasableExtension>
resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
deferUnresolved?: boolean
mount?: URL | string
builtinProtocol?: string
builtins?: Builtins
conditions?: Conditions
defer?: string[]
deferredProtocol?: string
directories?: boolean
engines?: Engines
extensions?: string[]
imports?: ImportsMap
matchedConditions?: string[]
resolutions?: ResolutionsMap
host?: string
hosts?: string[]
linked?: boolean
linkedProtocol?: string
}mount is the URL that the entry is mounted at when it is a bundle, for a consumer that loads the bundle with an explicit mount. The addons and assets that the bundle declares resolve relative to it. It defaults to the URL of the entry itself, and it applies only to the entry, not to bundles that the entry imports. Added in v2.7.1. See Bundled addons and assets.
conditions is passed to the resolver. As of v2.7.0, the traversal first adds the conditions of the host under a host attribute; see Conditions under a host attribute. The default resolver forwards conditions to bare-module-resolve and bare-addon-resolve unchanged. As of v2.7.0, resolve.bare and resolve.node add it to the conditions of each host, where before they ignored it.
As of v2.7.0, TraverseOptions extends ResolveOptions, so it also lists the options of bare-module-resolve, such as defer and imports. Before, it listed only the options of bare-addon-resolve, although the traversal already passed options such as defer and imports on to the resolver.
Artifacts
interface Artifacts {
addons: URL[] | Set<string>
assets: URL[] | Set<string>
}Dependency
interface Dependency {
url: URL
source: string | Buffer
type: number
naturalType: number
imports: ImportsMap
lexer: {
imports: Import[]
exports: Export[]
}
wasi?: string
}wasi is the URL of the WASI provider of a WebAssembly addon, when its importer named one with the wasi import attribute. The traversal has set it since v2.6.0, and the type declares it as of v2.7.2. A module imported under several host or wasi attributes is walked once for each, so the same url can be yielded in more than one Dependency. See WebAssembly addons.
ResolveOptions
interface ResolveOptions {
builtinProtocol?: string
builtins?: Builtins
conditions?: Conditions
defer?: string[]
deferredProtocol?: string
directories?: boolean
engines?: Engines
extensions?: string[]
imports?: ImportsMap
matchedConditions?: string[]
resolutions?: ResolutionsMap
host?: string
hosts?: string[]
linked?: boolean
linkedProtocol?: string
}The options of bare-module-resolve and bare-addon-resolve together, which the default resolver forwards to them. TraverseOptions extends it, and BareResolveOptions and NodeResolveOptions extend the same two. Added in v2.7.0.
AddonContext
interface AddonContext {
host: string | null
wasi: string | null
}The context that the addons of one package load in. host is the host asked for with the host import attribute, or null for the default hosts. wasi is the URL of the module that provides the WASI capabilities of a WebAssembly addon, or null for the default capabilities. Added in v2.6.0.
WebAssembly addons
Added in v2.6.0. An addon package can ship a WebAssembly build as prebuilds/wasi-wasm32/<name>.wasm, or <name>@<version>.wasm for a package with a version. A traversal falls back to that build when a package has no native build, and an import can ask for it with the host attribute.
This section covers what a traversal does with these builds. For how Bare loads and sandboxes them, see WebAssembly addons.
Fallback resolution
bare-addon-resolve treats a host that starts with wasi- as a WebAssembly host. It tries the native prebuilds first, then linked addons, then the .wasm file of the WebAssembly hosts, so a WebAssembly build is used only when no native build or linked addon was found. See resolve.directory.
traverse.resolve.bare adds wasi-wasm32 to its default hosts. When hosts is not set, it is [host, 'wasi-wasm32'], where host is the host option, or the host of the running platform when host is not set. Pass hosts to leave wasi-wasm32 out. The default resolver and traverse.resolve.node do not add it. Before v2.6.0 the default was [host]. Changed in v2.7.6: on a freertos- host, where bare-module doesn't load WebAssembly addons, the default is [host].
const traverse = require('bare-module-traverse')
// Falls back to prebuilds/wasi-wasm32/<name>.wasm
traverse(entry, { resolve: traverse.resolve.bare }, readModule)
// Native addons only
traverse(entry, { resolve: traverse.resolve.bare, hosts: [Bare.Addon.host] }, readModule)The lookup for .wasm files is in bare-addon-resolve 1.11.0 and later. bare-module-traverse 2.7.2 still declares ^1.5.0 for that dependency, so an install that keeps an older bare-addon-resolve looks for the wasi-wasm32 host like a native one, with .bare and .node files, and a package that has only a .wasm build throws ADDON_NOT_FOUND.
A host also supplies import conditions, so wasi-wasm32 adds wasi and wasm32. With resolve.bare and no hosts, a package whose exports or imports has a wasi or wasm32 condition has that branch traversed and recorded next to its default branch. Before v2.6.0 only the default branch was. Under a host attribute only one branch is; see Conditions under a host attribute.
What a driver sees
A WebAssembly addon is not a lazy artifact. The traversal does not call probeModule for it. It yields { module: url, artifact: false } and treats the addon as present only if the driver returns its source, so the driver must read the file. A native addon that probeModule reports as existing is yielded with artifact: true, and the driver may leave it unread, in which case its dependency.source is null. The URL is added to artifacts.addons either way.
The dependency for a .wasm addon has type set to constants.ADDON. Its naturalType is the defaultType option, constants.SCRIPT unless set otherwise, because the .wasm extension has no type of its own. When the wasi attribute applies, the dependency also has a wasi property holding the URL of the provider module as a string. The Dependency type declares wasi as of v2.7.2.
The host and wasi import attributes
Two import attributes set the context that the addons of a module, and of everything it imports, load in:
const addon = require('addon', { with: { host: 'wasi-wasm32', wasi: './wasi.js' } })import addon from 'addon' with { host: 'wasi-wasm32', wasi: './wasi.js' }hostasks for the WebAssembly build of the addons in the graph below the import.wasi-wasm32is the only value it accepts. It overrideshostsfor those addons, so it applies even whenhostshas only native hosts. Addons of packages outside that graph keep the default hosts. As of v2.7.0 it also adds thewasiandwasm32conditions to those targeted in that graph; see Conditions under ahostattribute.wasiis a module specifier, resolved from the importing module. The traversal adds the module it names to the graph and records it in the imports map of the importing module. Each WebAssembly addon loaded in that context gets that URL aswasion itsdependency.wasiworks withouthost, for example for a package that only ships a.wasmbuild. A native addon never gets awasiproperty.
A module is traversed again for each context it is imported under, so the same URL can be yielded more than once. As of v2.7.0 it resolves its imports only once, in the first context that walks it. A context that reaches it later walks the imports recorded then, rather than resolving them again with its own conditions, and checks the addons it reaches against its own context. The type, host, and wasi attributes are also applied when a resolutions map is in use.
The addons of one package load in one context for the whole graph. Importing the same package with the default hosts and with host throws an error rather than letting one package load in two ways. So does importing a package with two different wasi modules when its addon is WebAssembly. A package without addons can be imported under any context.
| Error code | Thrown when |
|---|---|
UNKNOWN_ADDON_HOST | A host attribute is not wasi-wasm32. |
ADDON_HOST_INCOMPATIBLE | A module in the graph below an import with a host attribute loads an addon that is not WebAssembly, such as a .bare file that it requires by path, or the addons of one package are reached with different hosts. |
ADDON_WASI_INCOMPATIBLE | The WebAssembly addons of one package are reached with different wasi modules. |
A host attribute for a package with no .wasm build throws ADDON_NOT_FOUND, as any missing addon does. traverse.assertAddonContext() applies the same rules to a driver that loads addons itself.
Conditions under a host attribute
Added in v2.7.0. A host attribute adds the conditions of the host it asks for, wasi and wasm32 for wasi-wasm32, to the conditions targeted for the modules, assets, and addons in the graph below the import. A package can then send that graph to a WebAssembly build through its exports or imports:
{
"name": "split",
"imports": {
"#binding": {
"wasi": { "wasm32": "split-wasi-wasm32" },
"default": "split-native"
}
}
}Imported with host: 'wasi-wasm32', #binding resolves to split-wasi-wasm32 alone. Imported without the attribute, with resolve.bare and no hosts, both branches are traversed and recorded, as described under Fallback resolution.
With resolve.bare and resolve.node, the added conditions join those of the native host in a single set rather than forming a set of their own, so the order of the conditions in the package decides between the branches. A package that lists wasi ahead of its native conditions, as above, resolves to its WebAssembly branch. One that lists a native condition first, such as the platform of the host, resolves to that branch, and an addon there with no .wasm build throws ADDON_NOT_FOUND. With the default resolver, wasi and wasm32 are added to the conditions option.
Before v2.7.0, a host attribute did not change the conditions. With resolve.bare, both branches of a package like split were traversed under the attribute, and a native branch with no .wasm build threw ADDON_NOT_FOUND.
Bundled addons and assets
Added in v2.7.1. When the traversal reaches a bundle, a module with the .bundle extension or one imported with type: 'bundle', it adds the addons and assets that the bundle header declares to artifacts.addons and artifacts.assets. It does this whether the imports of the bundle are resolved or come from a resolutions map. The traversal doesn't read, probe, or yield the declared files themselves. A bundle reached as an asset is not parsed.
Each declared path resolves against the URL that the bundle is mounted at, which is the URL of the bundle followed by /. For the entry, the mount option sets that URL instead, and a / is appended to it if missing. A bundle that the entry imports always uses its own URL.
For example, an entry bundle at file:///app/foo.bundle that declares the asset /asset.txt and the addon /../prebuilds/darwin-arm64/foo.bare adds file:///app/foo.bundle/asset.txt and file:///app/prebuilds/darwin-arm64/foo.bare. With mount: 'file:///root/', it adds file:///root/asset.txt and file:///prebuilds/darwin-arm64/foo.bare.
The header is parsed only while artifacts are collected. With artifacts: false, a .bundle file that is not a valid bundle doesn't throw. Otherwise the error from parsing it with bare-bundle is thrown, such as INVALID_BUNDLE_HEADER. Before v2.7.1, the traversal did not parse bundles.
bare-module-traverse/resolve
Functions
module(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | — |
parentURL | URL | — | — |
opts? | ResolveOptions | — | — |
addon(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | — |
parentURL | URL | — | — |
opts? | ResolveOptions | — | — |
default(entry: Import, parentURL: URL, opts?: ResolveOptions): Resolver
The default resolver, which simply forwards to <https://github.com/holepunchto/bare-module-resolve> and <https://github.com/holepunchto/bare-addon-resolve> with the literal options passed by the caller.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | Import | — | The import to resolve, as produced by bare-module-lexer. |
parentURL | URL | — | The WHATWG URL to resolve entry relative to. |
opts? | ResolveOptions | — | Resolve options forwarded to the underlying resolution algorithm. |
Returns Resolver — A Resolver that yields the candidate resolutions for entry.
bare(entry: Import, parentURL: URL, opts?: BareResolveOptions): Resolver
The Bare resolver, which matches the options used by the Bare module system. When hosts is not set, it defaults to [host, 'wasi-wasm32'], so a package with no native build for host resolves to its prebuilds/wasi-wasm32/<name>.wasm file. Changed in v2.6.0: before, hosts defaulted to [host]. Changed in v2.7.6: on a freertos- host, where bare-module doesn't load WebAssembly addons, the default is [host]. See WebAssembly addons. As of v2.7.0 it adds the conditions option to the conditions of each host; see bare-module-traverse/resolve/bare.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | Import | — | The import to resolve, as produced by bare-module-lexer. |
parentURL | URL | — | The WHATWG URL to resolve entry relative to. |
opts? | BareResolveOptions | — | Resolve options forwarded to the underlying resolution algorithm. |
Returns Resolver — A Resolver that yields the candidate resolutions for entry.
node(entry: Import, parentURL: URL, opts?: NodeResolveOptions): Resolver
The Node.js resolver, which matches the options used by the Node.js module system. As of v2.7.0 it adds the conditions option to the conditions of each host; see bare-module-traverse/resolve/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | Import | — | The import to resolve, as produced by bare-module-lexer. |
parentURL | URL | — | The WHATWG URL to resolve entry relative to. |
opts? | NodeResolveOptions | — | Resolve options forwarded to the underlying resolution algorithm. |
Returns Resolver — A Resolver that yields the candidate resolutions for entry.
Types
bare.BareResolveOptions
interface BareResolveOptions {
linked?: boolean
host?: string
hosts?: string[]
conditions?: Conditions
builtinProtocol?: string
builtins?: Builtins
defer?: string[]
deferredProtocol?: string
directories?: boolean
engines?: Engines
extensions?: string[]
imports?: ImportsMap
matchedConditions?: string[]
resolutions?: ResolutionsMap
linkedProtocol?: string
}node.NodeResolveOptions
interface NodeResolveOptions {
host?: string
hosts?: string[]
conditions?: Conditions
builtinProtocol?: string
builtins?: Builtins
defer?: string[]
deferredProtocol?: string
directories?: boolean
engines?: Engines
extensions?: string[]
imports?: ImportsMap
matchedConditions?: string[]
resolutions?: ResolutionsMap
linked?: boolean
linkedProtocol?: string
}bare-module-traverse/resolve/default
Functions
resolve(entry: Import, parentURL: URL, opts?: ResolveOptions): Resolver
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | Import | — | The import to resolve, as produced by bare-module-lexer. |
parentURL | URL | — | The WHATWG URL to resolve entry relative to. |
opts? | ResolveOptions | — | Resolve options forwarded to the underlying resolution algorithm. |
Returns Resolver — A Resolver that yields the candidate resolutions for entry.
Types
default.ResolveOptions
interface ResolveOptions {
builtinProtocol?: string
builtins?: Builtins
conditions?: Conditions
defer?: string[]
deferredProtocol?: string
directories?: boolean
engines?: Engines
extensions?: string[]
imports?: ImportsMap
matchedConditions?: string[]
resolutions?: ResolutionsMap
host?: string
hosts?: string[]
linked?: boolean
linkedProtocol?: string
}bare-module-traverse/resolve/bare
Functions
resolve(entry: Import, parentURL: URL, opts?: BareResolveOptions): Resolver
The Bare resolver. When hosts is not set, it defaults to [host, 'wasi-wasm32']. Changed in v2.6.0: before, hosts defaulted to [host]. Changed in v2.7.6: on a freertos- host, where bare-module doesn't load WebAssembly addons, the default is [host]. See WebAssembly addons.
Each host gives a set of conditions to target: the condition for the kind of import, such as require or addon, then bare, node, and the parts of the host name, such as darwin and arm64. Changed in v2.7.5: the host name is split with bare-host-parser, so an architecture that contains a - stays whole, such as arm64-32 in watchos-arm64-32. Before, it was split at every -. As of v2.7.0 the conditions option lists more conditions to target, and it's added to the set of each host. It can also be nested lists, in which case each innermost list is added to the set of each host in turn, giving one set for each, and a package is resolved for every set. A host whose conditions are in every innermost list gets no set of its own, since the other sets already target it. Before v2.7.0, conditions was ignored.
const traverse = require('bare-module-traverse')
// Also target the `custom` condition
traverse(entry, { resolve: traverse.resolve.bare, conditions: ['custom'] }, readModule)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | Import | — | The import to resolve, as produced by bare-module-lexer. |
parentURL | URL | — | The WHATWG URL to resolve entry relative to. |
opts? | BareResolveOptions | — | Resolve options forwarded to the underlying resolution algorithm. |
Returns Resolver — A Resolver that yields the candidate resolutions for entry.
Types
BareResolveOptions
interface BareResolveOptions {
linked?: boolean
host?: string
hosts?: string[]
conditions?: Conditions
builtinProtocol?: string
builtins?: Builtins
defer?: string[]
deferredProtocol?: string
directories?: boolean
engines?: Engines
extensions?: string[]
imports?: ImportsMap
matchedConditions?: string[]
resolutions?: ResolutionsMap
linkedProtocol?: string
}bare-module-traverse/resolve/node
Functions
resolve(entry: Import, parentURL: URL, opts?: NodeResolveOptions): Resolver
The Node.js resolver. Each host gives a set of conditions to target: the condition for the kind of import, then node and the parts of the host name. As of v2.7.0 the conditions option is added to the set of each host, as it is for resolve.bare. Before v2.7.0, conditions was ignored.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | Import | — | The import to resolve, as produced by bare-module-lexer. |
parentURL | URL | — | The WHATWG URL to resolve entry relative to. |
opts? | NodeResolveOptions | — | Resolve options forwarded to the underlying resolution algorithm. |
Returns Resolver — A Resolver that yields the candidate resolutions for entry.
Types
NodeResolveOptions
interface NodeResolveOptions {
host?: string
hosts?: string[]
conditions?: Conditions
builtinProtocol?: string
builtins?: Builtins
defer?: string[]
deferredProtocol?: string
directories?: boolean
engines?: Engines
extensions?: string[]
imports?: ImportsMap
matchedConditions?: string[]
resolutions?: ResolutionsMap
linked?: boolean
linkedProtocol?: string
}See also
- Builds on
bare-addon-resolve,bare-bundle,bare-host-parser,bare-mime,bare-module-lexer, andbare-module-resolve. Addon lookup, including the WebAssembly fallback, is inbare-addon-resolve. The bundle format, including the addons and assets a bundle declares, is inbare-bundle. - It's the engine behind
bare-pack— a low-level building block; usebare-packto produce a bundle, and reach for this directly only when building tooling on top of the graph. - The step generators exposed on
traverse(traverse.module,traverse.package,traverse.preresolved,traverse.imports,traverse.link,traverse.addons, andtraverse.assets), andtraverse.assertAddonContext, are subject to change between minor releases; if using them directly, specify a tilde range (for example~2.7.0) when declaring the dependency. v2.6.0 added a parameter totraverse.preresolved.bare-pack2.2.5 and later andbare-module7.5.0 declare~2.7.0. As of v2.7.0, the drive loop in the README takes queued subgraphs first in, first out, withqueue.shift(), matching the traversal itself. See the repository README for each step generator. - Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.
Last updated on