PhoenixAssets (PhoenixAssets v0.1.0)
View SourceRuntime and generated contracts for Phoenix applications built with Vite.
phoenix_assets supervises Vite and Storybook in development, generates typed
frontend contracts from Phoenix and Ash, links routes, pages, stories, sync
shapes, PubSub topics, and locales in an asset graph, and validates the
production manifest. It does not bundle assets itself or require LiveView as
the application's UI layer.
This module provides the runtime API. Related modules include:
PhoenixAssets.Config-- normalized, validated configuration.PhoenixAssets.Preset/PhoenixAssets.Plugin-- the integration engine.PhoenixAssets.Generated-- the contract generators (routes, types, ...).PhoenixAssets.Manifest/PhoenixAssets.ManifestServer-- Vite manifest.PhoenixAssets.Graph-- the asset graph.PhoenixAssets.DevSupervisor-- supervised dev processes.PhoenixAssets.Doctor-- configuration and production validation.
Platform support
Development supervision -- Vite and Storybook as OS children with process-group teardown -- requires a POSIX platform (macOS, Linux, or WSL2), because it relies on MuonTrap, which is POSIX-only. Production manifest serving and contract generation are platform-independent.
Summary
Functions
Returns the served path for an asset key.
The supervised children a host application adds to its own tree.
Whether development features (supervised Vite/Storybook, the dev overlay, the generated-file watcher) are active.
Resolves an entry to its production assets, or :dev in development.
Loads the asset graph (graph.json), or an empty map if none has been built.
Fetches a page from the asset graph by name, raising if absent.
Fetches a route from the asset graph by name, raising if absent.
The installed version of phoenix_assets.
Builds a Vite dev-server URL for path.
Functions
Returns the served path for an asset key.
In production resolves to the hashed file from the manifest (prefixed with
the configured :asset_url, if any). In development returns the key as a
root-relative path (the Vite dev server serves it). Raises if a manifest is
expected but unavailable.
@spec child_specs(keyword()) :: [Supervisor.child_spec() | {module(), term()}]
The supervised children a host application adds to its own tree.
Always includes PhoenixAssets.ManifestServer; in development also includes
PhoenixAssets.DevSupervisor (Vite, Storybook, the generated-file watcher).
Add them in your Application.start/2:
children = [
# ... your children ...
] ++ PhoenixAssets.child_specs()Options:
:config-- aPhoenixAssets.Config(defaults toPhoenixAssets.Config.load!/1).:ctx-- a prebuiltPhoenixAssets.Context(defaults to one derived from the config and the configured preset).
@spec dev?() :: boolean()
Whether development features (supervised Vite/Storybook, the dev overlay, the generated-file watcher) are active.
Defaults to false so production never spawns dev processes. Enable with:
config :phoenix_assets, :dev, enabled: true
@spec entry!(String.t()) :: %{ file: String.t(), css: [String.t()], imports: [String.t()], integrity: %{required(String.t()) => String.t()} } | :dev
Resolves an entry to its production assets, or :dev in development.
In production returns %{file:, css:, imports:, integrity:} from the Vite
manifest (integrity is a possibly-empty href => hash map, populated only
when the manifest carries SRI metadata). When
config :phoenix_assets, :build, asset_url: "https://cdn.example.com" is
set, every href (including the integrity keys) is prefixed with it. In
development returns :dev so callers emit dev-server URLs. Raises if a
manifest is expected but unavailable.
@spec graph() :: map()
Loads the asset graph (graph.json), or an empty map if none has been built.
For hot paths, prefer a module built with PhoenixAssets.Graph.Compiled.
Fetches a page from the asset graph by name, raising if absent.
Fetches a route from the asset graph by name, raising if absent.
@spec version() :: String.t()
The installed version of phoenix_assets.
Builds a Vite dev-server URL for path.
Prefers the hot file the Vite plugin writes (.phoenix-assets/hot under the
asset root, carrying the server's actual resolved URL -- scheme, host, and
port included, so HTTPS dev servers work); falls back to the configured
dev: [vite: [host:, port:]].