Function: z2fVite()
z2fVite(
options?):Plugin
Defined in: packages/vite/src/plugin.ts:147
Vite plugin factory for @zod-to-form/vite.
Registers Vite hooks for:
- Query mode (
resolveId+load): intercepts*.ts?z2f[=variant]imports, evaluates the schema viassrLoadModule, and returns a virtual module containing the generated React form component. - Generate mode (
transform): whenoptions.generateis set, scans JSX source files for<ZodForm schema={X}>and rewrites resolvable call sites with generated components at build time. - Resolver tree-shake (
transform): removeszodResolvercalls fromuseZodFormat build time whenvalidationLevelis set, allowing bundlers to drop the@hookform/resolversdependency. - HMR (
handleHotUpdate): invalidates cached compiled forms when their schema or thez2f.config.tschanges.
Parameters
options?
PluginOptions = {}
Optional plugin configuration. All fields are optional; z2fVite()
with no arguments produces a working plugin using auto-discovered config.
Returns
Plugin
A Vite Plugin object to include in vite.config.ts.
Example
// vite.config.ts
import { defineConfig } from 'vite';
import { z2fVite } from '@zod-to-form/vite';
export default defineConfig({
plugins: [z2fVite()],
});
Use When
- You want
import SignupForm from './signup.schema?z2f'to Just Work in a Vite app - You want HMR-aware form recompilation when schemas change in development
- You want to run generate mode to pre-compile forms from
<ZodForm>call sites
Avoid When
- You are building with webpack, esbuild, Rollup, or any non-Vite bundler
- Your schemas have cyclic references — the walker will recurse infinitely on them
- You need server-side form rendering without a React runtime
Pitfalls
- NEVER use
?z2fon schemas with cyclic type references — the schema walker recurses on Zod's internal type graph and hangs on cycles - NEVER enable
generatemode and then rely on HMR without testing — the generate-mode transform cache does not integrate with Vite's standard HMR module invalidation for rewritten JSX files - NEVER assume Zod types survive Vite's module graph isolation — always export
schemas from a dedicated
.schema.tsfile; importing from a module that re-exports through complex chains can fail underssrLoadModule - NEVER configure
configPathto point outside the Viteroot— the plugin usesssrLoadModulewith a dev server scoped toroot, so files outside that boundary may fail to resolve their own imports