LogoPear Docs
ReferencesBareModules

bare-module-resolve

Reference for bare-module-resolve: the low-level module resolution algorithm for Bare, exposed as a synchronous or asynchronous generator.

bare-module-resolve implements Bare's low-level module resolution algorithm. It's a pure-JavaScript generator that yields candidate URLs for a specifier; the caller supplies package reads and decides which candidate exists.

This is a low-level building block. Most applications resolve modules implicitly through the runtime or bundle with bare-pack; reach for this directly only when implementing tooling.

npm i bare-module-resolve

Usage

const resolve = require('bare-module-resolve')

function readPackage (url) {
  // Read and parse `url` if it exists, otherwise return null
}

for (const resolution of resolve('./file.js', new URL('file:///directory/'), readPackage)) {
  console.log(resolution)
}

An asynchronous form is also supported by iterating the resolver with for await.

API

const resolver = resolve(specifier, parentURL[, options][, readPackage])

Create a resolver for specifier relative to parentURL. Iterate it synchronously (for (const resolution of resolver)) or asynchronously (for await); each yielded value is a candidate URL to test.

Sub-generators

The algorithm's steps are exposed for fine-grained use: resolve.module, resolve.url, resolve.preresolved, resolve.deferred, resolve.package, resolve.packageSelf, resolve.packageExports, resolve.packageImports, resolve.packageImportsExports, resolve.packageTarget, resolve.builtinTarget, resolve.file, and resolve.directory. See the repository README for each step's signature and options.

Builds on bare-semver. Paired with bare-addon-resolve and bare-module-traverse.

See also

On this page