@anhur/vite
v0.3.0
Published
Vite plugin for @anhur/core: builds content, rebuilds on change, error overlay, serves assets
Readme
@anhur/vite
⚡ Vite plugin for Anhur.
It builds your content before the app, makes anhur/generated importable, rebuilds when content changes, shows build errors in the browser overlay, and serves copied assets.
📦 Install
bun add @anhur/core @anhur/vite🚀 Setup
import { defineConfig } from "vite";
import anhur from "@anhur/vite";
export default defineConfig({
plugins: [anhur()],
});Put anhur.config.ts in the Vite root (or pass configPath), and point TypeScript at the output:
// tsconfig.json
{
"compilerOptions": {
"paths": { "anhur/generated": ["./.anhur/generated"] },
// With scopes: one entry per scope, for example
// "anhur/scopes/public": ["./.anhur/public"]
},
}import { allPosts, getPost } from "anhur/generated";With scopes in the config, anhur/scopes/<name> resolves to the output of that scope (import { allPosts } from "anhur/scopes/public").
No separate anhur build step is needed — the plugin builds on vite dev and vite build (including vite build --watch).
🔁 Dev
- Uses Vite's file watcher. Changes to content folders, the config, and files the config imports trigger a rebuild (debounced); unrelated files do not.
- Only changed generated modules are rewritten, so Vite's normal HMR picks them up.
- SSR stays in step with the browser. For a content change, hot updates wait until the rebuild is done and runnable server environments (the SSR module runner) dropped the old content. This also holds for other plugins that reload the page for files they scan, such as
@tailwindcss/vite. The page reload then renders the new content on the server too, with no hydration mismatch. - While a build fails, a page that loads or reloads gets the error overlay again.
- A failed build keeps the dev server running: the error (with file and field) shows in the overlay and the terminal, and the last good output stays in place. Fixing the file reloads the page.
- Changes made while the first build runs are picked up by a rebuild right after it.
server.restart()keeps rebuilding, also with an inline plugin instance.- Copied assets are served under their URL prefix, with single
Rangerequests (video seeking),ETag/If-None-Match, correct content types andX-Content-Type-Options: nosniff. SVGs also getContent-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; img-src data: 'self'; sandbox; send the same headers from your production server or CDN. Dotfiles and paths outside the assets folder are never served.
Each build logs one line:
[anhur] rebuilt 12 document(s) in 48ms → .anhur/generated (2 written, 0 removed; posts 8, authors 4)📦 Build
A failed content build fails vite build with the diagnostics. The assets of this build (the files listed in .anhur-assets.json, never leftovers of earlier builds) are added to the client output (for example dist/anhur-assets/) when they are served locally; with a CDN base and storage, @anhur/assets uploads them instead. When the assets base equals the Vite base, the files go to the root of the output.
With vite build --watch, a content edit runs one content build; the Rollup rebuild caused by the rewritten generated modules reuses it.
Do not run vite build while vite dev uses the same assets folder: both remove files the other one does not use.
⚙️ Options
anhur({
// Config file relative to the Vite root. Default: anhur.config.{ts,mts,js,mjs}
configPath: "./content/anhur.config.ts",
});The Vite base is applied to local asset URLs (base: "/app/" → /app/anhur-assets/…).
Without Vite, use the anhur CLI from @anhur/core (anhur build, anhur check, anhur watch).
License
MIT
