bare-pack
Bundle packing for Bare
bare-pack — Bundle packing for Bare.
npm i bare-packUsage
const pack = require('bare-pack')
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.
}
const bundle = await pack(new URL('file:///directory/file.js'), readModule, listPrefix)API
Functions
pack
pack(entry: URL, opts: PackOptions, readModule: ReadModuleCallback, listPrefix: ListPrefixCallback | null, writeFile: WriteFileCallback): Promise<Bundle>Bundle the module graph rooted at url, 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 bundled. writeFile is called for every addon or asset that should be offloaded rather than embedded; see the Offloading section of the README. When writeFile is provided, listPrefix must be passed positionally (or as null).
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
entry | URL | — | The root of the module graph to bundle; must be a WHATWG URL instance (typically a file: URL). |
opts | PackOptions | — | Packing options, extending TraverseOptions from bare-module-traverse. Adds concurrency, base (the URL that offloaded file paths are made relative to), and offload (whether to write addons and/or assets to disk instead of embedding them). |
readModule | ReadModuleCallback | — | Called with a URL for every module in the graph; returns the module source as a Buffer or string, or null if it does not exist. |
listPrefix | ListPrefixCallback | null | — | Called with a URL for every prefix to list; yields the URLs that have it as a prefix. Pass null (or omit) to skip prefix bundling. |
writeFile | WriteFileCallback | — | Called for each addon or asset to offload rather than embed, receiving the file URL and its source. See the Offloading section of the README. |
Returns Promise<Bundle> — a promise that resolves to the packed bare-bundle Bundle, with all statically resolvable imports preresolved.
Types
PackOptions
interface PackOptions extends TraverseOptions {
concurrency?: number
base?: URL | string
offload?: boolean | { addons?: boolean; assets?: boolean }
}ReadModuleCallback
interface ReadModuleCallback {
(url: URL): Buffer | string | null
}ListPrefixCallback
interface ListPrefixCallback {
(url: URL): Iterable<URL>
}WriteFileCallback
interface WriteFileCallback {
(url: URL, source: Buffer | string): string | null | void
}See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.