LogoPear Docs
ReferencesBareModules

bare-module-traverse

Low-level module graph traversal for Bare

stable

bare-module-traverse — Low-level module graph traversal for Bare.

npm i bare-module-traverse

Usage

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>
Source

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

ParameterTypeDefaultDescription
entryURLThe WHATWG URL of the entry module to root the graph at.
readModule(url: URL) => Buffer | string | nullCalled 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 | undefinedCalled with the URL of each module to probe for existence; returns a boolean, or undefined to fall back to readModule.
resolveModule?(url: URL) => URLCalled 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
}
Source

Artifacts

interface Artifacts {
    addons: URL[] | Set<string>
    assets: URL[] | Set<string>
  }
Source

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

ParameterTypeDefaultDescription
entryImportThe import to resolve, as produced by bare-module-lexer.
parentURLURLThe WHATWG URL to resolve entry relative to.
opts?ResolveOptionsResolve 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

ParameterTypeDefaultDescription
entryImportThe import to resolve, as produced by bare-module-lexer.
parentURLURLThe WHATWG URL to resolve entry relative to.
opts?BareResolveOptionsResolve 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

ParameterTypeDefaultDescription
entryImportThe import to resolve, as produced by bare-module-lexer.
parentURLURLThe WHATWG URL to resolve entry relative to.
opts?NodeResolveOptionsResolve 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

ParameterTypeDefaultDescription
entryImportThe import to resolve, as produced by bare-module-lexer.
parentURLURLThe WHATWG URL to resolve entry relative to.
opts?ResolveOptionsResolve 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

ParameterTypeDefaultDescription
entryImportThe import to resolve, as produced by bare-module-lexer.
parentURLURLThe WHATWG URL to resolve entry relative to.
opts?BareResolveOptionsResolve 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[]
}
Source

bare-module-traverse/resolve/node

Functions

resolve(entry: Import, parentURL: URL, opts?: NodeResolveOptions): Resolver

Source

Parameters

ParameterTypeDefaultDescription
entryImportThe import to resolve, as produced by bare-module-lexer.
parentURLURLThe WHATWG URL to resolve entry relative to.
opts?NodeResolveOptionsResolve 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[]
}
Source

See also

On this page