LogoPear Docs
ReferencesBareModules

bare-module

Module support for JavaScript

stable

bare-module — Module support for JavaScript. It is a native addon and requires Bare >=1.29.4.

npm i bare-module

Usage

const Module = require('bare-module')

API

Module

new Module(url: URL)

Source

Parameters

ParameterTypeDefaultDescription
urlURLThe WHATWG URL identifying the module.

builtins: Builtins

Source

A map of builtin module specifiers mapped to the loaded module.

cache: Cache

Source

A cache of loaded modules for this module. Defaults to Module.cache.

conditions: Conditions

Source

An array of conditions used to resolve dependencies while loading the module. See Conditional exports for possible values.

defaultType: number

Source

The assumed type of a module without a type using an ambiguous extension, such as .js. See Module.constants.types for possible values.

dirname: string

Source

The directory portion of module.url.

exports: unknown

Source

The exports from the module.

filename: string

Source

The file portion of module.url.

id: string

Source

imports: ImportsMap

Source

The import map when the module was loaded.

main: Module

Source

The module representing the entry script where the program was launched.

Module.asset(specifier: string, parentURL: URL, opts?: Options): URL

Source

Get the asset URL by resolving specifier relative to parentURL. specifier is a string and parentURL is a WHATWG URL.

Parameters

ParameterTypeDefaultDescription
specifierstringThe asset specifier to resolve.
parentURLURLThe WHATWG URL to resolve specifier relative to.
opts?OptionsResolution options.

Returns URL — The WHATWG URL of the resolved asset.

Throws

  • ASSET_NOT_FOUND — no asset matching specifier could be found relative to parentURL.
  • TypeErrorspecifier is not a string.

Module.builtinModules: Module[]

Source

Always an empty array; provided for Node.js compatibility.

Module.cache: Cache

Source

Module.constants

Module.constants: {
  states: {
    EVALUATED: number
    SYNTHESIZED: number
    RUN: number
  }
  types: {
    SCRIPT: number
    MODULE: number
    JSON: number
    BUNDLE: number
    ADDON: number
    BINARY: number
    TEXT: number
    ASSET: number
  }
}
Source

Constants describing module states (EVALUATED, SYNTHESIZED, RUN) and module types (SCRIPT, MODULE, JSON, BUNDLE, ADDON, BINARY, TEXT, ASSET).

Module.createRequire(parentURL: string | URL, opts?: CreateRequireOptions): Require

Source

Create a preconfigured require() bound to parentURL, so specifiers resolve and load relative to it.

Parameters

ParameterTypeDefaultDescription
parentURLstring | URLThe parent URL that the returned require() resolves and loads specifiers relative to.
opts?CreateRequireOptionsOptions for the created require(), such as its protocol and cache.

Returns Require — A require() bound to parentURL, with main, cache, resolve, addon, and asset attached.

Module.isBuiltin(): boolean

Source

Always returns false; provided for Node.js compatibility.

Module.load(url: URL, opts: LoadOptions): Module

Source

Load a module with the provided url. url is a WHATWG URL. If provided, the source will be passed to the matching extension for the url.

Overloads:

Module.load(url: URL, opts: LoadOptions): Module
Module.load(url: URL, source?: Buffer | string | Bundle | null, opts?: LoadOptions): Module

Parameters

ParameterTypeDefaultDescription
urlURLThe WHATWG URL of the module to load.
optsLoadOptionsLoad options; may carry a source to load directly instead of reading it through the protocol.

Returns Module — The loaded Module, reusing the cached instance if url was already loaded.

Throws

  • TYPE_INCOMPATIBLE — a module is already cached for url with a type incompatible with the requested type.

Module.protocol: Protocol

Source

The ModuleProtocol class for resolving, reading and loading modules. See Protocols for usage.

Module.resolve(specifier: string, parentURL: URL, opts?: ResolveOptions): URL

Source

Resolve the module specifier relative to the parentURL. specifier is a string and parentURL is a WHATWG URL.

Parameters

ParameterTypeDefaultDescription
specifierstringThe module specifier to resolve.
parentURLURLThe WHATWG URL to resolve specifier relative to.
opts?ResolveOptionsResolution options.

Returns URL — The WHATWG URL that specifier resolves to.

Throws

  • MODULE_NOT_FOUND — no module matching specifier could be found relative to parentURL.
  • TypeErrorspecifier is not a string.

path: string

Source

protocol: Protocol

Source

The ModuleProtocol class for resolving, reading and loading modules. See Protocols for usage.

resolutions: ResolutionsMap

Source

A map of preresolved imports with keys being serialized parent URLs and values being "imports" maps.

type: number

Source

The type of the module. See Module.constants.types for possible values.

url: URL

Source

The WHATWG URL identifier of the module.

ModuleProtocol

new ModuleProtocol(methods?: Partial<ModuleProtocol>, context?: ModuleProtocol)

Source

Defines how modules are resolved, read and loaded; custom protocols can serve modules from outside the file system, such as a Hyperdrive or a bare-bundle.

Parameters

ParameterTypeDefaultDescription
methods?Partial<ModuleProtocol>Protocol method overrides; any of preresolve, postresolve, resolve, exists, read, addon, or asset.
context?ModuleProtocolAn existing protocol to fall back to for any method not provided in methods.

addon(url: URL): URL

Source

Post-process URLs for addons before postresolve().

Parameters

ParameterTypeDefaultDescription
urlURLThe resolved addon URL to post-process.

asset(url: URL): URL

Source

Post-process URLs for assets before postresolve().

Parameters

ParameterTypeDefaultDescription
urlURLThe resolved asset URL to post-process.

exists(url: URL, type: number): boolean

Source

Return whether the URL exists.

Parameters

ParameterTypeDefaultDescription
urlURLThe URL to check for existence.
typenumberThe module type being probed (see Module.constants.types).

extend(methods: Partial<ModuleProtocol>): ModuleProtocol

Source

Create a new protocol that uses this protocol as its context, overriding the given methods.

Parameters

ParameterTypeDefaultDescription
methodsPartial<ModuleProtocol>Protocol method overrides for the new protocol.

Returns ModuleProtocol — A new ModuleProtocol that uses this protocol as its context, with methods overriding.

postresolve(url: URL): URL

Source

Process the resolved URL; can be used to convert file paths, etc.

Parameters

ParameterTypeDefaultDescription
urlURLThe resolved URL to post-process.

Returns URL — The (possibly transformed) resolved URL.

preresolve(specifier: string, parentURL: URL): string

Source

Preprocess the specifier and parentURL before the resolve algorithm is called.

Parameters

ParameterTypeDefaultDescription
specifierstringThe module specifier being resolved.
parentURLURLThe URL the specifier is resolved relative to.

Returns string — The (possibly rewritten) specifier to pass into the resolve algorithm.

read(url: URL): Buffer | string | null

Source

Return the source code of a URL, represented as a string or buffer.

Parameters

ParameterTypeDefaultDescription
urlURLThe URL to read.

Returns Buffer | string | null — The source of url as a Buffer or string, or null if it does not exist.

resolve(specifier: string, parentURL: URL, imports: ImportsMap): URL

Source

Resolve the specifier to a URL.

Parameters

ParameterTypeDefaultDescription
specifierstringThe module specifier to resolve.
parentURLURLThe URL to resolve specifier relative to.
importsImportsMapThe "imports" map to apply during resolution.

Types

Attributes

interface Attributes {
  type: Lowercase<keyof typeof constants.types>
}
Source

Import attributes instructing how a module should be loaded.

Cache

interface Cache {
  [href: string]: Module
}
Source

A map of module URL hrefs to loaded modules.

Options

interface Options {
  attributes?: Attributes
  builtins?: Builtins
  cache?: Cache
  conditions?: Conditions
  defaultType?: number
  imports?: ImportsMap
  main?: Module
  protocol?: Protocol
  referrer?: Module
  resolutions?: ResolutionsMap
  type?: number
}
Source

LoadOptions

interface LoadOptions extends Options {
  isDynamicImport?: boolean
  isImport?: boolean
}
Source

ResolveOptions

interface ResolveOptions extends Options {
  isImport?: boolean
}
Source

CreateRequireOptions

interface CreateRequireOptions extends Options {
    module?: Module
  }
Source

RequireOptions

interface RequireOptions {
    with?: Attributes
  }
Source

Options for require(); with holds the import attributes.

RequireAddon

interface RequireAddon {
    (specifier?: string, parentURL?: URL): string
    host: string
    resolve: (specifier: string, parentURL?: URL) => unknown
  }
Source

The require.addon function: imports addon modules, with host and resolve attached.

Require

interface Require {
    (parentURL: string | URL, opts?: RequireOptions): unknown
    main: Module
    cache: Cache
    resolve: (specifier: string, parentURL?: URL) => string
    addon: RequireAddon
    asset: (specifier: string, parentURL?: URL) => string
  }
Source

The function returned by Module.createRequire(): resolves and loads modules relative to its parent URL, with main, cache, resolve, addon, and asset attached.

See also

On this page