Volt.Plugin behaviour (Volt v0.4.3)

Copy Markdown View Source

Behaviour for Volt build plugins.

Plugins hook into the compilation pipeline at four stages:

  1. resolve — remap import specifiers before file lookup
  2. load — provide virtual module content for a specifier
  3. transform — modify compiled output before it's served/bundled
  4. 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(path)

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

Load content for a resolved module path.

Return {:ok, code, content_type} to provide content, {:ok, code} (defaults to JS), or nil to pass.

name()

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

Plugin name for identification and error messages.

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.

Return {:ok, code} with modified code, or nil to pass.

resolve(specifier, importer)

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

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(code, path)

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

Transform compiled code.

Return {:ok, code} with modified code, or nil to pass.