Behaviour for Volt build plugins.
Plugins hook into the compilation pipeline at four stages:
- resolve — remap import specifiers before file lookup
- load — provide virtual module content for a specifier
- transform — modify compiled output before it's served/bundled
- render_chunk — modify final bundled chunks before writing
Each callback is optional. Return nil or :pass to skip.
Example
defmodule MyApp.SvgPlugin do
@behaviour Volt.Plugin
@impl true
def name, do: "svg"
@impl true
def resolve(specifier, _importer) do
if String.ends_with?(specifier, ".svg") do
{:ok, specifier}
end
end
@impl true
def load(path) do
if String.ends_with?(path, ".svg") do
svg = File.read!(path)
{:ok, "export default " <> Jason.encode!(svg), "application/javascript"}
end
end
@impl true
def transform(_code, _path), do: nil
@impl true
def render_chunk(_code, _chunk_info), do: nil
end
Summary
Callbacks
Load content for a resolved module path.
Plugin name for identification and error messages.
Transform a final output chunk before writing.
Resolve an import specifier to a file path or virtual module ID.
Transform compiled code.
Callbacks
Load content for a resolved module path.
Return {:ok, code, content_type} to provide content,
{:ok, code} (defaults to JS), or nil to pass.
@callback name() :: String.t()
Plugin name for identification and error messages.
Transform a final output chunk before writing.
Return {:ok, code} with modified code, or nil to pass.
Resolve an import specifier to a file path or virtual module ID.
Return {:ok, resolved_path} to handle, nil to pass to the next plugin.
Transform compiled code.
Return {:ok, code} with modified code, or nil to pass.