Volt.JS.Vendor (Volt v0.22.1)

Copy Markdown View Source

Pre-bundle vendor (node_modules) dependencies for dev mode.

Scans source files with OXC.select/3, identifies bare specifiers (non-relative, non-URL), resolves them through module directories, and bundles each into a single ESM file with OXC.bundle/2.

CJS packages (e.g. React) are automatically converted to ESM during bundling. process.env.NODE_ENV is replaced with "development" so conditional CJS branches resolve correctly.

Bundled files are cached on disk in _build/volt/vendor/.

Vendor URLs carry a version, ?v=, that browsers cache them under for good. It changes with the module directories, lockfiles and plugins, and with the generation of the pre-bundle: a hash over the signature of every package bundled, written beside the bundles. A package that changed in place, such as a linked one, is bundled again on the next pre-bundle, which gives every vendor module a new URL. One URL for all of them is deliberate: the bundles share chunks, and a browser mixing old and new ones would hold two instances of a chunk.

Summary

Functions

Return the current browser hash for optimized dependency requests.

Bundle a single vendor specifier on demand.

Return whether a request browser hash matches the current optimized dependency state.

Decode a URL-safe specifier back to its original form.

Encode a specifier for use in URLs (escaping @ and /).

Return the generation of the pre-bundle for opts: a hash over the cache signatures of the packages bundled, or nil before the first pre-bundle.

Scan source files and pre-bundle any bare npm imports.

Read a pre-bundled vendor file by specifier.

Read a pre-bundled vendor file when its cache signature matches the current options.

Get the URL path for a vendor module.

Get the URL path for a vendor module with a cache-busting browser hash.

Functions

browser_hash(opts)

@spec browser_hash(keyword()) :: String.t()

Return the current browser hash for optimized dependency requests.

The hash covers the browser signature and the generation of the pre-bundle, so it changes when a package is bundled again.

bundle_on_demand(specifier, node_modules, opts \\ [])

@spec bundle_on_demand(String.t(), String.t() | nil, keyword()) ::
  {:ok, String.t()} | {:error, term()}

Bundle a single vendor specifier on demand.

Used by the dev server when a /@vendor/ request arrives for a specifier that wasn't caught by prebundle/1 (e.g. transitive dependency, or newly added import).

current_browser_hash?(hash, opts)

@spec current_browser_hash?(
  String.t() | nil,
  keyword()
) :: boolean()

Return whether a request browser hash matches the current optimized dependency state.

decode_specifier(encoded)

Decode a URL-safe specifier back to its original form.

encode_specifier(specifier)

Encode a specifier for use in URLs (escaping @ and /).

generation(opts)

@spec generation(keyword()) :: String.t() | nil

Return the generation of the pre-bundle for opts: a hash over the cache signatures of the packages bundled, or nil before the first pre-bundle.

prebundle(opts)

@spec prebundle(keyword()) ::
  {:ok, %{required(String.t()) => String.t()}} | {:error, term()}

Scan source files and pre-bundle any bare npm imports.

Returns a map of specifier → vendor_path for import rewriting.

Options

  • :root — source directory to scan
  • :node_modules — path to node_modules (default: auto-detect)
  • :resolve_dirs — additional package directories to resolve from
  • :force — rebuild even if cached (default: false)

read(specifier)

@spec read(String.t()) :: {:ok, String.t()} | {:error, :not_found}

Read a pre-bundled vendor file by specifier.

read(specifier, opts)

@spec read(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, :not_found}

Read a pre-bundled vendor file when its cache signature matches the current options.

vendor_url(specifier)

@spec vendor_url(String.t()) :: String.t()

Get the URL path for a vendor module.

vendor_url(specifier, opts)

@spec vendor_url(
  String.t(),
  keyword()
) :: String.t()

Get the URL path for a vendor module with a cache-busting browser hash.