vite/src
Vite plugin for schema-driven form generation.
Two modes:
- Query mode (
?z2fimports): import a schema file with a?z2fquery parameter to receive a fully-generated React form component as a virtual module. Zero build step — the plugin compiles on demand. - Generate mode (
options.generate): scan JSX source files for<ZodForm>call sites and replace statically-resolvable ones with generated form components at build time. Opt-in viagenerate: {}.
Config resolution order:
options.configPathif explicitly provided- Auto-discovery of
z2f.config.{ts,mts,js,mjs}in the Vite root DEFAULT_CONFIGmerged withoptions.configOverride
Remarks
Requires Zod v4 and @zod-to-form/core. The plugin uses Vite's
ssrLoadModule to evaluate schema files — the same Vite resolver pipeline
that handles your app code, so aliases and tsconfig paths all work.
HMR contract: schema file changes invalidate only the affected virtual
modules. Config file (z2f.config.ts) changes invalidate all cached
compiled forms and trigger re-compilation on the next import.
Use When
- You want zero-config form generation directly from
import './schema?z2f' - You have a Vite-based app and want to skip the CLI generate step
- You need per-variant forms (mobile/desktop) from the same schema
- You want HMR-aware form recompilation during development
Avoid When
- You are NOT using Vite (use
@zod-to-form/clifor non-Vite builds) - Your schema files have cyclic type references — the
?z2frewriter recurses on Zod's type graph and will hang on cycles - You need SSR-safe form HTML without a client-side React bundle
- You are on Zod v3 — the plugin only supports Zod v4 schemas
Pitfalls
- NEVER use
?z2fon schemas with cyclic type references — the schema walker recurses on Zod's type graph and hits infinite recursion on cycles - NEVER enable
rewrite(generate mode) with HMR simultaneously without testing — the in-memory compilation cache has invalidation gaps when source files are changed mid-session and the module graph is stale - NEVER assume Zod schema objects survive Vite's module graph isolation
intact — always re-export schemas from a dedicated file so
ssrLoadModulecan evaluate them without pulling in unrelated runtime dependencies - NEVER put your
z2f.config.tsoutside the Vite root — auto-discovery only searchesresolvedConfig.root; files outside that boundary are silently skipped and the plugin falls back to defaults - NEVER mix
configOverridewith az2f.config.tsthat overlaps the same keys —configOverridewins unconditionally (shallow merge), so config-file fields with the same name are silently dropped
Errors
Other
default
Renames and re-exports z2fVite