PhoenixAssets.Manifest (PhoenixAssets v0.1.0)

View Source

Parses and queries a Vite build manifest.

Vite writes a manifest.json keyed by source path; each entry records its hashed output file, its css, and the chunk imports it depends on (by manifest key). This module resolves those relationships -- recursively following imports and de-duplicating -- so a caller can ask "for this entry, what stylesheet links, script src, and module preloads do I emit?".

Root-relative paths are prefixed with / (Vite emits them relative to the served static root); absolute http(s):// URLs -- an entry Vite resolves to a CDN origin -- pass through unchanged.

The query functions (file/2, css/2, imports/2, subresource_integrity/2) all raise KeyError when the requested top-level key is absent; transitive imports missing from the manifest are skipped.

Summary

Functions

All stylesheet hrefs for key, including those of its transitive imports.

Fetches the raw chunk for key, raising KeyError if it is absent.

The hashed output file for key (prefixed with /).

Transitively imported chunk files for key, excluding its own file.

Loads and decodes a Vite manifest from path.

Module preload targets for key (its transitively imported chunk files).

Subresource-integrity hashes for key, keyed by served href.

Types

t()

@type t() :: %{optional(String.t()) => map()}

Functions

css(manifest, key)

@spec css(t(), String.t()) :: [String.t()]

All stylesheet hrefs for key, including those of its transitive imports.

Raises KeyError when key itself is absent (like file/2 and imports/2); transitive imports that are missing from the manifest are skipped.

iex> manifest = %{"src/app.ts" => %{"file" => "assets/app.js", "css" => ["assets/app.css"]}}
iex> PhoenixAssets.Manifest.css(manifest, "src/app.ts")
["/assets/app.css"]

entry!(manifest, key)

@spec entry!(t(), String.t()) :: map()

Fetches the raw chunk for key, raising KeyError if it is absent.

A missing key also emits [:phoenix_assets, :manifest, :missing_entry], so production observability catches requests for entries the build no longer produces.

file(manifest, key)

@spec file(t(), String.t()) :: String.t()

The hashed output file for key (prefixed with /).

iex> manifest = %{"src/app.ts" => %{"file" => "assets/app-Cw3Qz1.js"}}
iex> PhoenixAssets.Manifest.file(manifest, "src/app.ts")
"/assets/app-Cw3Qz1.js"

imports(manifest, key)

@spec imports(t(), String.t()) :: [String.t()]

Transitively imported chunk files for key, excluding its own file.

load(path)

@spec load(Path.t()) :: {:ok, t()} | {:error, term()}

Loads and decodes a Vite manifest from path.

preload_links(manifest, key)

@spec preload_links(t(), String.t()) :: [String.t()]

Module preload targets for key (its transitively imported chunk files).

subresource_integrity(manifest, key)

@spec subresource_integrity(t(), String.t()) :: %{required(String.t()) => String.t()}

Subresource-integrity hashes for key, keyed by served href.

Returns a map of href => integrity for the entry's own file and its transitively imported chunk files, for every chunk that carries an integrity field. Vite emits that field only when an SRI plugin is configured, so the map is empty otherwise -- callers render integrity/crossorigin only when a hash is present.

Raises KeyError when key itself is absent (like file/2, css/2, and imports/2).

iex> manifest = %{"src/app.ts" => %{"file" => "assets/app.js", "integrity" => "sha384-abc"}}
iex> PhoenixAssets.Manifest.subresource_integrity(manifest, "src/app.ts")
%{"/assets/app.js" => "sha384-abc"}