Build Hooks

Build hooks allow you to run custom shell commands before and after the build process. This is useful for tasks like installing dependencies, preprocessing data, optimizing assets, or triggering deployments.

Configuration

Define hooks in config.toml:

[build]
hooks.pre = ["npm install", "npx tsc"]
hooks.post = ["npm run minify", "npx pagefind --site public"]
Key Type Default Description
hooks.pre array [] Commands to run **before** building
hooks.post array [] Commands to run **after** building

How It Works

Pre-Build Hooks

Pre-build hooks run before any content processing begins. They are ideal for:

[build]
hooks.pre = [
  "npm ci",
  "npx tailwindcss -i src/input.css -o static/assets/css/main.css",
  "python scripts/fetch-data.py"
]

If a pre-build hook fails (exits with a non-zero status), the build process is aborted. This prevents building with missing dependencies or broken assets.

Post-Build Hooks

Post-build hooks run after the site has been generated in the output directory. They are ideal for:

[build]
hooks.post = [
  "npx imagemin public/images/* --out-dir=public/images",
  "npx pagefind --site public",
  "./scripts/deploy.sh"
]

If a post-build hook fails, a warning is shown but the build is not considered failed. The generated site remains intact.

Execution Order

Commands are executed sequentially in the order they are defined:

[build]
hooks.pre = ["echo Step 1", "echo Step 2", "echo Step 3"]

Output:

Running pre-build hook: echo Step 1
Step 1
Running pre-build hook: echo Step 2
Step 2
Running pre-build hook: echo Step 3
Step 3

Serve Mode

Build hooks also run during hwaro serve:

Use Cases

TypeScript Compilation

[build]
hooks.pre = ["npx tsc --outDir static/assets/js"]

Tailwind CSS

[build]
hooks.pre = [
  "npx tailwindcss -i src/styles.css -o static/assets/css/styles.css --minify"
]

Generate a client-side search index after build:

[build]
hooks.post = ["npx pagefind --site public"]

Image Optimization

[build]
hooks.post = [
  "npx imagemin public/**/*.{jpg,png} --out-dir=public"
]

Custom Deploy Script

[build]
hooks.post = ["./scripts/deploy.sh"]

Full Pipeline Example

[build]
hooks.pre = [
  "npm ci",
  "npx tsc",
  "npx tailwindcss -i src/input.css -o static/assets/css/main.css --minify"
]
hooks.post = [
  "npx pagefind --site public",
  "npx imagemin public/images/* --out-dir=public/images",
  "echo 'Build complete!'"
]

Error Handling

Hook Type On Failure
Pre-build ❌ Build **aborted** — no content is processed
Post-build ⚠️ Warning shown — generated site is preserved

This design ensures that critical setup tasks (pre-build) must succeed, while optional optimization tasks (post-build) don't block the build output.

Tips

See Also