Volt.Plugin behaviour (Volt v0.14.0)

Copy Markdown View Source

Behaviour for Volt build plugins.

Plugins can participate in resolution, loading, compilation, import extraction, and final chunk rendering. All callbacks except name/0 are optional.

Plugins may be configured as modules or {module, opts} tuples. When a plugin defines a callback with one extra arity, Volt passes the tuple opts as the final argument.

Plugins can opt into Vite-style ordering with enforce/0 or enforce/1: :pre plugins run before normal plugins, and :post plugins run after them.

Summary

Callbacks

Compile a source file into browser-ready JavaScript plus optional CSS.

Return compile-time replacements for a build mode.

Return virtual JavaScript-like modules embedded in a plugin-owned source file.

Return :pre, :post, or nil to control plugin ordering.

Return extensions owned by this plugin for :compile, :resolve, :watch, or :scan.

Extract dependency metadata from source handled by this plugin.

Load content for a resolved module path.

Plugin name for identification and error messages.

Return the canonical dev prebundle specifier for an import.

Return a generated dev prebundle entry for a canonical specifier.

Transform a final output chunk before writing.

Resolve an import specifier to a file path, :skip, or pass with nil.

Transform compiled JavaScript before serving or bundling.

Types

compiled()

@type compiled() :: Volt.Pipeline.Result.t()

prebundle_export()

@type prebundle_export() :: Volt.JS.PrebundleEntry.Export.t()

prebundle_import()

@type prebundle_import() :: Volt.JS.PrebundleEntry.Import.t()

Callbacks

compile(path, source, opts)

(optional)
@callback compile(path :: String.t(), source :: String.t(), opts :: keyword()) ::
  {:ok, compiled()} | {:error, term()} | nil

Compile a source file into browser-ready JavaScript plus optional CSS.

define(mode)

(optional)
@callback define(mode :: String.t()) :: %{required(String.t()) => String.t()}

Return compile-time replacements for a build mode.

embedded_modules(path, source, opts)

(optional)
@callback embedded_modules(path :: String.t(), source :: String.t(), opts :: keyword()) ::
  [{extension :: String.t(), source :: String.t()}] | nil

Return virtual JavaScript-like modules embedded in a plugin-owned source file.

enforce()

(optional)
@callback enforce() :: :pre | :post | nil

Return :pre, :post, or nil to control plugin ordering.

extensions(kind)

(optional)
@callback extensions(kind :: atom()) :: [String.t()]

Return extensions owned by this plugin for :compile, :resolve, :watch, or :scan.

extract_imports(path, source, opts)

(optional)
@callback extract_imports(path :: String.t(), source :: String.t(), opts :: keyword()) ::
  {:ok, Volt.JS.ImportExtractor.Result.t()} | {:error, term()} | nil

Extract dependency metadata from source handled by this plugin.

Use this when a source format can contain imports that are not visible to Volt's normal JavaScript parser before plugin processing, such as component files or custom markup formats. Return nil to let Volt use the default JavaScript import extractor.

load(path)

(optional)
@callback load(path :: String.t()) ::
  {:ok, String.t(), String.t()} | {:ok, String.t()} | nil

Load content for a resolved module path.

name()

@callback name() :: String.t()

Plugin name for identification and error messages.

prebundle_alias(specifier)

(optional)
@callback prebundle_alias(specifier :: String.t()) :: String.t() | nil

Return the canonical dev prebundle specifier for an import.

Use this advanced framework-integration hook when several package entrypoints should share one dev vendor module. Return nil to leave the specifier unchanged.

This is Volt-specific. Vite framework plugins achieve similar results through dependency optimization, resolution, and virtual modules rather than a direct hook with this name.

prebundle_entry(specifier)

(optional)
@callback prebundle_entry(specifier :: String.t()) ::
  {:source, filename :: String.t(), source :: String.t()}
  | {:proxy, filename :: String.t(),
     imports: [prebundle_import()], exports: [prebundle_export()]}
  | nil

Return a generated dev prebundle entry for a canonical specifier.

Use this advanced framework-integration hook when a framework needs a synthetic vendor module that re-exports multiple package entrypoints or adapts package exports for browser dev mode. Return nil to let Volt generate a normal package prebundle.

This hook affects dev dependency prebundling only; production output is built from the application module graph.

render_chunk(code, chunk_info)

(optional)
@callback render_chunk(code :: String.t(), chunk_info :: map()) :: {:ok, String.t()} | nil

Transform a final output chunk before writing.

resolve(specifier, importer)

(optional)
@callback resolve(specifier :: String.t(), importer :: String.t() | nil) ::
  {:ok, String.t()} | :skip | nil

Resolve an import specifier to a file path, :skip, or pass with nil.

transform(code, path)

(optional)
@callback transform(code :: String.t(), path :: String.t()) :: {:ok, String.t()} | nil

Transform compiled JavaScript before serving or bundling.