bare-module-traverse
Low-level module graph traversal for Bare
bare-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.
Types
TraverseOptions
interface TraverseOptions extends ResolveOptions {
defaultType?: number
aliases?: Record<string, AliasableExtension>
resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
}Artifacts
interface Artifacts {
addons: URL[] | Set<string>
assets: URL[] | Set<string>
}bare-module-traverse/resolve
Functions
default(entry: Import, parentURL: URL, opts?: ResolveOptions): Resolver
Source
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
Source
The Bare resolver, which matches the options used by the Bare module system.
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
Source
The Node.js resolver, which matches the options used by the Node.js module system.
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.
bare-module-traverse/resolve/default
Functions
resolve(entry: Import, parentURL: URL, opts?: ResolveOptions): Resolver
Source
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-module-traverse/resolve/bare
Functions
resolve(entry: Import, parentURL: URL, opts?: BareResolveOptions): Resolver
Source
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 extends ResolveOptions {
linked?: boolean
host?: string
hosts?: string[]
}bare-module-traverse/resolve/node
Functions
resolve(entry: Import, parentURL: URL, opts?: NodeResolveOptions): Resolver
Source
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 extends ResolveOptions {
host?: string
hosts?: string[]
}See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.