Hooks
To compile CSS/JS, prepare external data, build search indexes, or upload build outputs, attach tasks under [[hooks.<stage>]]. Hooks run once per stage invocation and exit; Tola handles file watching and scheduling.
Stages
| Stage | Purpose | build | check/preview | dev default |
|---|---|---|---|---|
before-build | Emit .typ sources, data files, or asset inputs before source discovery | Runs | Runs | "run" |
generate-outputs | Inspect compiled pages and assets to emit derived published files | Runs | Runs | "run" |
after-publish | Inspect the written publish directory (deploy, verify, notify) | Runs | Skipped | "skip" |
Module packaging adds a separate release-time before-pack stage (see Authoring modules). tola query, tola nu, tola inspect, and tola vendor do not run build hooks.
Execution order
Each hook requires name and exactly one of nu = "path/to/script.nu" (with optional args) or argv = ["tool", ...]. argv spawns directly without a shell, globs, or env expansion. The working directory is the site root, while nu paths resolve relative to the declaring site or module.
Hooks are enabled by default; disable with enable = false or bind to a boolean option with enabled-by = "search.enabled" (site hooks read top-level [options]; module hooks read merged module options; quote dotted keys such as '"search.enable"').
Within one site or module instance, hooks run in ascending order (default 0). Hooks sharing an order run concurrently unless [build].par-hooks = false, capped by [build].hook-jobs. Run tola inspect config --mode dev to verify order and reuse state.
before-build
Use before-build to produce files outside publish-dir for Typst or [assets], listing every emitted path in generates. For instance, aggregate JSON under data/ and read it with json(...) in Typst:
[[hooks.before-build]]
name = "site-stats"
nu = "tools/stats.nu"
dev = "reuse"
rerun-on = { include = ["data/**/*.json"] }
generates = ["generated/stats.json"]def main [] {
let count = (glob "data/**/*.json" | length)
mkdir generated
{ files: $count } | to json | save --raw --force generated/stats.json
}#let stats = json("/generated/stats.json")
Found #stats.files data files.Paths in generates resolve relative to the site root and must all exist when the hook exits. Generated files are not published automatically—declare them in [assets] if clients need them, or write .typ files into content-dir for source discovery.
generate-outputs
During generate-outputs, (get-hook).input-dir is a read-only snapshot of compiled pages and assets, and (get-hook).output-dir is a fresh staging directory for writing every path declared in outputs:
# Module tola-module.toml
[[hooks.generate-outputs]]
name = "search-index"
nu = "tools/search.nu"
dev = "skip"
outputs = [{ file = "_tola-modules/{name}/search.json" }]use tola [get-hook get-module query]
def main [] {
let mod = get-module
let dest = (get-hook).output-dir | path join "_tola-modules" $mod.name "search.json"
mkdir ($dest | path dirname)
query $"($mod.name).pages" | to json | save --raw $dest
}outputs accepts { file = "..." } or { tree = "..." } (relative to the publish root, without base-path). Every declared target must be written, undeclared files are rejected, and outputs cannot collide with pages or other hooks. Link hook outputs in Typst with output-url(...); unlike [assets], they are not minified or cache-busted.
All generate-outputs hooks in one build read the same upstream snapshot; combine sequential steps into one script. Call use tola get-assets in Nu (--module, --include, --exclude) to inspect declared static assets.
after-publish
(get-hook).input-dir points to the written publish directory and cannot add files to the site. tola build waits for completion; if an after-publish hook fails, the command exits non-zero while leaving the written publish-dir in place. When explicitly set to dev = "run", it runs asynchronously without delaying browser reloads.
Reuse & caching
Production builds always run enabled hooks; during tola dev, each hook obeys dev ("run", "reuse", or "skip"):
- Tola watches
nuentry scripts automatically; list additional inputs inrerun-on = { include = [...], exclude = [...] }. Module hooks targeting site files use{ project = "content/**/*.typ" }(globs skip.git/.jjunless included explicitly). - Both
before-build(with non-emptygenerates) andgenerate-outputssupportdev = "reuse". Within a singletola devsession, Tola skips reruns and replays prior diagnostics while watched inputs, config, and outputs remain unchanged. Keep time-, network-, or env-dependent hooks on"run", or passtola dev --no-reuseto force reruns. - Heavy whole-site post-processors (such as Pagefind) re-trigger on every page edit; set
dev = "skip"(as in[options.search]in this repository) sotola buildandtola previewstill run them. - Persist cross-build caches in
(get-hook).cache-dir(disposable scratch intemp-dir) keyed byget-hook --rerun-on-digest(nullif an input path cannot be hashed).
External programs
argv executables can read call context (paths, stage, module options, and reportPath) by running "$TOLA_EXECUTABLE" nu @tola-scripts/context --json.
Nu hooks and module commands submit warnings or errors via use tola diagnostic and diagnostic submit (one batch per invocation; omit location in hooks and commands; any error fails the build after the script exits):
use tola diagnostic
def main [] {
[
(diagnostic make "Some entries lack a summary field"
--severity warning
--code missing-summary
--hints ["Add `summary` to those entries."])
] | diagnostic submit
}External argv executables write a version-1 JSON diagnostic report to reportPath before exiting.