PhoenixAssets.Plugin behaviour (PhoenixAssets v0.1.0)

View Source

The behaviour every phoenix_assets integration implements.

A plugin contributes declarations -- generated files, Vite config patches, dev processes, graph entries, doctor checks -- computed from a PhoenixAssets.Context and its own initialised state. Plugins never mutate files or global state directly; the core engine collects their declarations and acts on them. This keeps the plugin layer pure and the system deterministic.

Defining a plugin

defmodule MyApp.Assets.Storybook do
  use PhoenixAssets.Plugin

  depends_on PhoenixAssets.SvelteKit
  after_plugin PhoenixAssets.Tailwind

  @impl true
  def dev_processes(ctx, _state) do
    [PhoenixAssets.DevProcess.new(id: :storybook, command: ~w(pnpm storybook dev -p 6006), cd: ctx.asset_root)]
  end
end

Every callback has a no-op default injected by use PhoenixAssets.Plugin, so a plugin implements only the callbacks it cares about. depends_on/1 declares a hard ordering edge (the dependency must be present and ordered first); after_plugin/1 declares a soft edge (honoured only if that plugin is present).

use PhoenixAssets.Plugin

Using this module sets the PhoenixAssets.Plugin behaviour, imports depends_on/1 and after_plugin/1, and supplies no-op implementations for callbacks the plugin does not define.

Callbacks

init/2 runs once to build per-plugin state; the collection callbacks (generated_files/2, vite_config/2, dev_processes/2, graph_entries/2, doctor_checks/2) each answer "what does this plugin contribute?" for one kind of declaration. PhoenixAssets.Engine initialises plugins and fans each callback out across them in resolved order.

See also

Summary

Types

Opaque per-plugin state returned by init/2 and passed to other callbacks.

Callbacks

Returns external dev processes this plugin needs supervised.

Returns doctor checks this plugin contributes.

Returns the generated contract files this plugin contributes.

Returns asset-graph entries this plugin contributes.

Initialises plugin state from its options and the context.

A short, unique name for the plugin (defaults to the underscored last module segment).

Returns a Vite config patch, deep-merged (in plugin order, later plugin wins) into the asset graph's "vite" section.

Functions

Declares a soft ordering: if module is present it is ordered before this plugin.

Declares a hard dependency: module must be present and ordered before this plugin.

Types

state()

@type state() :: term()

Opaque per-plugin state returned by init/2 and passed to other callbacks.

Callbacks

dev_processes(ctx, state)

(optional)
@callback dev_processes(ctx :: PhoenixAssets.Context.t(), state()) :: [
  PhoenixAssets.DevProcess.t()
]

Returns external dev processes this plugin needs supervised.

doctor_checks(ctx, state)

(optional)
@callback doctor_checks(ctx :: PhoenixAssets.Context.t(), state()) :: [
  PhoenixAssets.Doctor.Check.t()
]

Returns doctor checks this plugin contributes.

generated_files(ctx, state)

(optional)
@callback generated_files(ctx :: PhoenixAssets.Context.t(), state()) :: [
  PhoenixAssets.GeneratedFile.t()
]

Returns the generated contract files this plugin contributes.

graph_entries(ctx, state)

(optional)
@callback graph_entries(ctx :: PhoenixAssets.Context.t(), state()) :: [
  PhoenixAssets.Graph.Entry.t()
]

Returns asset-graph entries this plugin contributes.

init(opts, ctx)

@callback init(opts :: keyword(), ctx :: PhoenixAssets.Context.t()) ::
  {:ok, state()} | {:error, term()}

Initialises plugin state from its options and the context.

name()

(optional)
@callback name() :: atom()

A short, unique name for the plugin (defaults to the underscored last module segment).

vite_config(ctx, state)

(optional)
@callback vite_config(ctx :: PhoenixAssets.Context.t(), state()) :: map()

Returns a Vite config patch, deep-merged (in plugin order, later plugin wins) into the asset graph's "vite" section.

Functions

after_plugin(module)

(macro)

Declares a soft ordering: if module is present it is ordered before this plugin.

depends_on(module)

(macro)

Declares a hard dependency: module must be present and ordered before this plugin.