Volt.Assets (Volt v0.13.1)

Copy Markdown View Source

Static asset handling — images, fonts, SVGs, and other non-code files.

Small assets (below the inline threshold) are inlined as base64 data URIs. Larger assets are copied with content-hashed filenames.

Import patterns

// Inlined as data URI when small enough
import icon from './icon.svg'
// icon = "data:image/svg+xml;base64,..."

// Forced public URL
import photo from './photo.jpg?url'
// photo = "/assets/photo-a1b2c3d4.jpg"

// Raw file contents
import text from './message.txt?raw'

JavaScript new URL("./asset.ext", import.meta.url) references and CSS url("./asset.ext") references are also routed through this asset pipeline in production builds.

Summary

Functions

Check if a path is a known asset type.

Copy an asset to the output directory with a content-hashed filename.

Generate a JS module for an asset and return emitted asset filenames.

Get MIME type for a file extension.

Generate a JS module that exports asset content or a URL.

Functions

asset?(path)

@spec asset?(String.t()) :: boolean()

Check if a path is a known asset type.

copy_hashed(source_path, outdir)

@spec copy_hashed(String.t(), String.t()) :: {:ok, String.t()}

Copy an asset to the output directory with a content-hashed filename.

Returns {:ok, hashed_filename}.

emit_js_module(path, opts \\ [])

@spec emit_js_module(
  String.t(),
  keyword()
) :: {:ok, %{code: String.t(), assets: [String.t()]}} | {:error, term()}

Generate a JS module for an asset and return emitted asset filenames.

mime_type(path)

@spec mime_type(String.t()) :: String.t()

Get MIME type for a file extension.

to_js_module(path, opts \\ [])

@spec to_js_module(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Generate a JS module that exports asset content or a URL.

Options

  • :raw — export the file contents as a string
  • :url — force a public URL instead of inlining
  • :inline — force a data URI
  • :no_inline — force a public URL even for small assets
  • :inline_limit — byte threshold for default inlining (default: 4096)
  • :prefix — URL prefix for referenced assets (default: "/assets")
  • :outdir — output directory for copied assets (production only)
  • :url_path — dev-server URL to export when no :outdir is provided