Vite plugin
A Vite plugin. It adds import queries (like Vite's ?raw) that load Nix files into cursery's in-memory /nix/store. It can also render pages from Nix.
import cursery from "cursery/vite-plugin"; import { defineConfig } from "vite"; export default defineConfig({ plugins: [cursery()], });
To declare server and client entrypoints, see Pages.
For TypeScript, add the queries' types:
{ "compilerOptions": { // ... "types": ["vite/client", "cursery/vite-plugin/client"] } }
Imports#
Each query takes a directory with a default.nix, or a .nix file, as the entry. The files are the entry, and every .nix file beside it and below. Edits to any of them update the import.
Relative paths resolve from the importing module. Paths starting with / resolve from Vite's root, then from the file system.
?cursery:app#
app.mount(container, options?)
Loads the files, and renders the app into the element container. options are those of mount from the API. Returns a function that unmounts it. Throws if the app fails to mount.
import app from "./app?cursery:app"; const unmount = app.mount(document.getElementById("app"));
Edits hot reload every container the app is mounted into, keeping state. If the edited app fails to mount, the error is logged, and the last working version stays until the next edit.
?cursery:store-path#
Loads the files, and returns the entry's store path, for mount and evaluate from the API. Edits load a new store path, and unload the old one.
import path from "./app?cursery:store-path"; // "/nix/store/…-source/default.nix" evaluate(`(import ${path}).init`);
?cursery:files#
Returns the files by path, for load from the API. Nothing is loaded automatically.
import files from "./app?cursery:files"; // { "./default.nix": "…", "./view.nix": "…" } const path = load(files); evaluate(`import ${path}/view.nix`);