Volt.Priv (Volt v0.14.12)

Copy Markdown View Source

Accesses browser support JavaScript and other runtime files from an OTP application's priv directory.

priv is the OTP convention for application-specific runtime files. Volt uses this module for its own bundled browser support code, and plugin authors can use it to keep JavaScript and TypeScript in their application instead of embedding source in Elixir strings.

The source may be either an OTP application atom or an {app, root} tuple. The tuple form keeps repeated calls concise when several files live under the same priv subdirectory:

assets = {:my_app, "islands"}

Volt.Priv.path(assets, "runtime.ts")
Volt.Priv.read!(assets, "runtime.ts")
Volt.Priv.js!(assets, "runtime.ts")
Volt.Priv.js!(assets, "entry.ts", id: "counter", props: %{count: 1})

js!/3 binds $placeholder JavaScript literals with OXC before emitting browser JavaScript. That keeps templates valid TypeScript or JavaScript files instead of requiring EEx for ordinary data injection.

Summary

Functions

Emits browser JavaScript for a support file stored under priv.

Returns the absolute path to a file under an OTP application's priv directory.

Reads a file from an OTP application's priv directory.

Types

bindings()

@type bindings() :: keyword() | map()

source()

@type source() :: atom() | {atom(), String.t()}

Functions

js!(source, relative, bindings \\ [], opts \\ [])

@spec js!(source(), String.t(), bindings(), keyword()) :: String.t()

Emits browser JavaScript for a support file stored under priv.

TypeScript and JavaScript files are both accepted. When bindings are provided, the source is parsed and $placeholder literals are replaced with JSON-compatible JavaScript literals using OXC.bind/2 before the result is emitted.

Options:

  • :rewrite_specifiers - a map or keyword list of import specifiers to rewrite after code generation.

path(app, relative)

@spec path(source(), String.t()) :: String.t()

Returns the absolute path to a file under an OTP application's priv directory.

read!(source, relative)

@spec read!(source(), String.t()) :: String.t()

Reads a file from an OTP application's priv directory.