LogoPear Docs

bare-pack

Bundle packing for Bare

stable

bare-pack — Bundle packing for Bare.

npm i bare-pack

Usage

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>
Source

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

ParameterTypeDefaultDescription
entryURLThe root of the module graph to bundle; must be a WHATWG URL instance (typically a file: URL).
optsPackOptionsPacking 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).
readModuleReadModuleCallbackCalled 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.
listPrefixListPrefixCallback | nullCalled 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.
writeFileWriteFileCallbackCalled 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 }
}
Source

ReadModuleCallback

interface ReadModuleCallback {
  (url: URL): Buffer | string | null
}
Source

ListPrefixCallback

interface ListPrefixCallback {
  (url: URL): Iterable<URL>
}
Source

WriteFileCallback

interface WriteFileCallback {
  (url: URL, source: Buffer | string): string | null | void
}
Source

See also

On this page