bare-module
Module support for JavaScript
bare-module — Module support for JavaScript. It is a native addon and requires Bare >=1.29.4.
npm i bare-moduleUsage
const Module = require('bare-module')API
Module
new Module(url: URL)
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | The 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
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | The asset specifier to resolve. |
parentURL | URL | — | The WHATWG URL to resolve specifier relative to. |
opts? | Options | — | Resolution options. |
Returns URL — The WHATWG URL of the resolved asset.
Throws
ASSET_NOT_FOUND— no asset matchingspecifiercould be found relative toparentURL.TypeError—specifieris 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
}
}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
| Parameter | Type | Default | Description |
|---|---|---|---|
parentURL | string | URL | — | The parent URL that the returned require() resolves and loads specifiers relative to. |
opts? | CreateRequireOptions | — | Options 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): ModuleParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | The WHATWG URL of the module to load. |
opts | LoadOptions | — | Load 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 forurlwith a type incompatible with the requestedtype.
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
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | The module specifier to resolve. |
parentURL | URL | — | The WHATWG URL to resolve specifier relative to. |
opts? | ResolveOptions | — | Resolution options. |
Returns URL — The WHATWG URL that specifier resolves to.
Throws
MODULE_NOT_FOUND— no module matchingspecifiercould be found relative toparentURL.TypeError—specifieris 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
| Parameter | Type | Default | Description |
|---|---|---|---|
methods? | Partial<ModuleProtocol> | — | Protocol method overrides; any of preresolve, postresolve, resolve, exists, read, addon, or asset. |
context? | ModuleProtocol | — | An 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
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | The resolved addon URL to post-process. |
asset(url: URL): URL
Source
Post-process URLs for assets before postresolve().
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | The resolved asset URL to post-process. |
exists(url: URL, type: number): boolean
Source
Return whether the URL exists.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | The URL to check for existence. |
type | number | — | The 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
| Parameter | Type | Default | Description |
|---|---|---|---|
methods | Partial<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
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | The 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
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | The module specifier being resolved. |
parentURL | URL | — | The 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
| Parameter | Type | Default | Description |
|---|---|---|---|
url | URL | — | The 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
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | The module specifier to resolve. |
parentURL | URL | — | The URL to resolve specifier relative to. |
imports | ImportsMap | — | The "imports" map to apply during resolution. |
Types
Attributes
interface Attributes {
type: Lowercase<keyof typeof constants.types>
}Import attributes instructing how a module should be loaded.
Cache
interface Cache {
[href: string]: Module
}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
}LoadOptions
interface LoadOptions extends Options {
isDynamicImport?: boolean
isImport?: boolean
}ResolveOptions
interface ResolveOptions extends Options {
isImport?: boolean
}CreateRequireOptions
interface CreateRequireOptions extends Options {
module?: Module
}RequireOptions
interface RequireOptions {
with?: Attributes
}Options for require(); with holds the import attributes.
RequireAddon
interface RequireAddon {
(specifier?: string, parentURL?: URL): string
host: string
resolve: (specifier: string, parentURL?: URL) => unknown
}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
}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
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.