Generating Schemas
.compile() builds fast functions for each schema with new Function. A Content-Security-Policy (CSP) without unsafe-eval blocks new Function. The Routier CLI builds those functions ahead of time, so your app runs under that policy. Your schema files stay as they are.
Quick Navigation
When you need it
You need it when your app sends a CSP header and the script-src list has no unsafe-eval. Browser extensions and many company sites use a policy like this:
Content-Security-Policy: script-src 'self'Without that policy, the CLI is optional. .compile() works the same either way.
Generate the file
Run this from your project folder. It needs Node 22 or later.
npx @routier/cli generateIt writes src/routier.generated.js, and prints the names of the schemas it found and the file's path.
Use the full name @routier/cli. The npm package named routier is a different tool.
To pin the version, install it as a dev dependency. Then call routier generate from a script:
npm install --save-dev @routier/cli{
"scripts": {
"routier:generate": "routier generate"
}
}Import it first
Import the generated file in your app's entry file, before any file that compiles a schema:
import "./routier.generated.js";
import { todos } from "./schemas/todos";The file registers the functions. When .compile() runs, it uses them in place of new Function.
The generated file holds only functions. Your defaults, computed properties and other code stay in your schema files.
Keep it up to date
Run the command again after you:
- change a schema
- update
@routier/core
A changed schema has new functions. If you forget to run the command, the app still works where eval is allowed. Under the strict policy, .compile() throws an error that names the schema.
You can run it before each build:
{
"scripts": {
"build": "routier generate && vite build"
}
}Choose the files
With no arguments, the CLI loads each file in src that imports @routier/core and calls .compile(). It skips test files.
The CLI runs these files in Node. A file that needs window or the DOM when it loads fails with the reason. To pick the files yourself, name them or use a glob:
npx @routier/cli generate "src/schemas/*.ts"To write the file to another folder, use --out:
npx @routier/cli generate --out src/generatedTypes
Your types do not change. InferType and InferCreateType read the schema in your own files, so they work the same with or without the generated file. See InferType.
Errors
If a schema has no generated function and the policy blocks eval, .compile() throws:
The schema 'todos' has no generated function. Run "npx @routier/cli generate", and import the generated file before your schemas.To fix it, run the command and check that the import comes first.
If the CLI finds no schemas, it prints Found no compiled schemas in N files. Check that your schema files are in src, or name them.