Bare Docs

bare-module-traverse

Low-level module graph traversal for Bare

Documented against v2.5.9
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>

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
entryURL—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.

traverse.addons

traverse.addons(parentURL: URL, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): Traversal

Parameters

ParameterTypeDefaultDescription
parentURLURL——
artifactsArtifacts——
visitedSet<string>——
opts?TraverseOptions——

traverse.assets

traverse.assets(patterns: ConditionalSpecifier, parentURL: URL, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): Traversal

Parameters

ParameterTypeDefaultDescription
patternsConditionalSpecifier——
parentURLURL——
artifactsArtifacts——
visitedSet<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): Traversal

Parameters

ParameterTypeDefaultDescription
parentURLURL——
sourcestring | Buffer——
importsImportsMap——
artifactsArtifacts——
lexer{ imports: Import[] exports: Export[] }——
visitedSet<string>——
opts?TraverseOptions——
traverse.link(entry: Import, specifier: string, condition: string, parentURL: URL, imports: ImportsMap, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): Traversal

Parameters

ParameterTypeDefaultDescription
entryImport——
specifierstring——
conditionstring——
parentURLURL——
importsImportsMap——
artifactsArtifacts——
visitedSet<string>——
opts?TraverseOptions——

traverse.module

traverse.module(url: URL, source: string | Buffer, attributes: Record<string, string> | null, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): Traversal

Parameters

ParameterTypeDefaultDescription
urlURL——
sourcestring | Buffer——
attributesRecord<string, string> | null——
artifactsArtifacts——
visitedSet<string>——
opts?TraverseOptions——

traverse.package

traverse.package(url: URL, source: string | Buffer, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): Traversal

Parameters

ParameterTypeDefaultDescription
urlURL——
sourcestring | Buffer——
artifactsArtifacts——
visitedSet<string>——
opts?TraverseOptions——

traverse.preresolved

traverse.preresolved(url: URL, source: string | Buffer, resolution: ResolutionsMap, artifacts: Artifacts, visited: Set<string>, opts?: TraverseOptions): Traversal

Parameters

ParameterTypeDefaultDescription
urlURL——
sourcestring | Buffer——
resolutionResolutionsMap——
artifactsArtifacts——
visitedSet<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
  builtinProtocol?: string
  builtins?: Builtins
  conditions?: Conditions
  extensions?: string[]
  host?: string
  hosts?: string[]
  linked?: boolean
  linkedProtocol?: string
  matchedConditions?: string[]
  resolutions?: ResolutionsMap
}

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[]
  }
}

bare-module-traverse/resolve

Functions

module(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver

Parameters

ParameterTypeDefaultDescription
specifierstring——
parentURLURL——
opts?ResolveOptions——

addon(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver

Parameters

ParameterTypeDefaultDescription
specifierstring——
parentURLURL——
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

ParameterTypeDefaultDescription
entryImport—The import to resolve, as produced by bare-module-lexer.
parentURLURL—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.

Parameters

ParameterTypeDefaultDescription
entryImport—The import to resolve, as produced by bare-module-lexer.
parentURLURL—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.

Parameters

ParameterTypeDefaultDescription
entryImport—The import to resolve, as produced by bare-module-lexer.
parentURLURL—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[]
  builtinProtocol?: string
  builtins?: Builtins
  conditions?: Conditions
  extensions?: string[]
  linkedProtocol?: string
  matchedConditions?: string[]
  resolutions?: ResolutionsMap
}

node.NodeResolveOptions

interface NodeResolveOptions {
  host?: string
  hosts?: string[]
  builtinProtocol?: string
  builtins?: Builtins
  conditions?: Conditions
  extensions?: string[]
  linked?: boolean
  linkedProtocol?: string
  matchedConditions?: string[]
  resolutions?: ResolutionsMap
}

bare-module-traverse/resolve/default

Functions

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

Parameters

ParameterTypeDefaultDescription
entryImport—The import to resolve, as produced by bare-module-lexer.
parentURLURL—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

Parameters

ParameterTypeDefaultDescription
entryImport—The import to resolve, as produced by bare-module-lexer.
parentURLURL—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[]
  builtinProtocol?: string
  builtins?: Builtins
  conditions?: Conditions
  extensions?: string[]
  linkedProtocol?: string
  matchedConditions?: string[]
  resolutions?: ResolutionsMap
}

bare-module-traverse/resolve/node

Functions

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

Parameters

ParameterTypeDefaultDescription
entryImport—The import to resolve, as produced by bare-module-lexer.
parentURLURL—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[]
  builtinProtocol?: string
  builtins?: Builtins
  conditions?: Conditions
  extensions?: string[]
  linked?: boolean
  linkedProtocol?: string
  matchedConditions?: string[]
  resolutions?: ResolutionsMap
}

See also

  • Builds on bare-addon-resolve, bare-mime, bare-module-lexer, and bare-module-resolve.
  • It's the engine behind bare-pack — a low-level building block; use bare-pack to 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.addons, and traverse.assets) are subject to change between minor releases; if using them directly, specify a tilde range (for example ~1.2.3) when declaring the dependency. 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

Was this helpful?

On this page