Skip to content

Ahead-of-time Compiler

Beta

Paseri schemas can be compiled ahead of time into a TypeScript module containing the parser, for faster validation.

There are two ways to do this:

  • Via the Vite plugin, which compiles *.schema.ts files automatically as part of your build. This is the simplest option if you already use Vite.
  • Manually, by calling toSource and writing the generated module yourself. Use this if you don’t build with Vite, or want full control over when and how schemas are compiled.

If you build with Vite, @paseri/vite-plugin 🔗 compiles your schemas automatically, swapping the runtime parser for the ahead-of-time version in production builds. Requires Vite 7+.

Terminal window
deno add jsr:@paseri/paseri jsr:@paseri/vite-plugin

Register the plugin in your Vite config:

vite.config.ts
import { paseri } from '@paseri/vite-plugin';
export default {
plugins: [paseri()],
};

Write your schemas in *.schema.ts files:

greeting.schema.ts
import * as p from '@paseri/paseri';
export const Greeting = p.object({
hello: p.string(),
});

Then import and use them as normal:

app.ts
import { Greeting } from './greeting.schema.ts';
const result = Greeting.safeParse({ hello: 'world' });

The @paseri/compiler package is a companion to Paseri; install both.

Terminal window
deno add jsr:@paseri/paseri jsr:@paseri/compiler

Import @paseri/paseri/introspect, then pass schema.toIR() to toSource, which returns the module source as a string:

build.ts
import * as p from '@paseri/paseri';
import '@paseri/paseri/introspect';
import { toSource } from '@paseri/compiler';
const schema = p.object({
hello: p.string(),
});
const source = toSource(schema.toIR(), { name: 'Greeting' });
// Write `source` to a file (e.g. `greeting.ts`) as part of your build.

The generated module exports a single object named after the schema (here, Greeting) that mirrors the runtime schema’s parsing interface, so it is a drop-in replacement for validation.

app.ts
import { Greeting } from './greeting.ts';
const result = Greeting.safeParse({ hello: 'world' });