zod-aot 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +82 -35
  2. package/package.json +3 -2
package/README.md CHANGED
@@ -60,11 +60,13 @@ npm install zod@^4
60
60
 
61
61
  ## Quick Start
62
62
 
63
+ ### 1. Define schemas with `compile()`
64
+
63
65
  ```typescript
66
+ // src/schemas.ts
64
67
  import { z } from "zod";
65
- import { extractSchema, generateValidator } from "zod-aot";
68
+ import { compile } from "zod-aot";
66
69
 
67
- // 1. Define your Zod schema as usual
68
70
  const UserSchema = z.object({
69
71
  name: z.string().min(3),
70
72
  age: z.number().int().positive(),
@@ -72,70 +74,115 @@ const UserSchema = z.object({
72
74
  role: z.enum(["admin", "user"]),
73
75
  });
74
76
 
75
- // 2. Extract the schema into an intermediate representation (IR)
76
- const ir = extractSchema(UserSchema);
77
+ // compile() falls back to Zod in dev, uses generated functions after build
78
+ export const validateUser = compile(UserSchema);
79
+ ```
77
80
 
78
- // 3. Generate an optimized validator function
79
- const { code, functionName } = generateValidator(ir, "validateUser");
81
+ ### 2. Use the compiled validator
80
82
 
81
- // 4. The generated code is a self-contained JS function
82
- // In production, write this to a file at build time
83
- console.log(code); // preamble: Set/RegExp declarations
84
- console.log(functionName); // function safeParse_validateUser(input) { ... }
83
+ ```typescript
84
+ // Same interface as Zod works in both dev and production
85
+ const user = validateUser.parse(data); // throws on failure
86
+ const result = validateUser.safeParse(data); // { success, data/error }
87
+ const isUser = validateUser.is(data); // type guard (boolean)
85
88
  ```
86
89
 
87
- ### Using the Generated Code
90
+ ### 3. Generate optimized code
88
91
 
89
- The generated code can be evaluated at build time and written to a file:
92
+ Choose one of these approaches:
93
+
94
+ **Option A: Build plugin (Vite / webpack / esbuild / Rollup)**
90
95
 
91
96
  ```typescript
92
- // Build script example
93
- import { writeFileSync } from "node:fs";
94
- import { extractSchema, generateValidator } from "zod-aot";
97
+ // vite.config.ts
98
+ import zodAot from "zod-aot/vite";
95
99
 
96
- const ir = extractSchema(UserSchema);
97
- const { code, functionName } = generateValidator(ir, "validateUser");
100
+ export default defineConfig({
101
+ plugins: [zodAot()],
102
+ });
103
+ ```
104
+
105
+ Also available: `zod-aot/webpack`, `zod-aot/esbuild`, `zod-aot/rollup`
106
+
107
+ **Option B: CLI**
108
+
109
+ ```bash
110
+ # Generate optimized validators
111
+ npx zod-aot generate src/schemas.ts -o src/schemas.compiled.ts
98
112
 
99
- // Write to a .js file
100
- writeFileSync(
101
- "src/validators/user.generated.js",
102
- `${code}\nexport const validateUser = ${functionName}`,
103
- );
113
+ # Generate from a directory
114
+ npx zod-aot generate src/ -o src/compiled/
115
+
116
+ # Check if schemas are compilable (without generating)
117
+ npx zod-aot check src/schemas.ts
104
118
  ```
105
119
 
106
- Then import and use in your application:
120
+ ## Build Plugin (unplugin)
121
+
122
+ The build plugin automatically replaces `compile()` calls with optimized inline validators during the build step. No code changes needed — your source files stay the same.
107
123
 
108
124
  ```typescript
109
- import { validateUser } from "./validators/user.generated.js";
125
+ // vite.config.ts
126
+ import zodAot from "zod-aot/vite";
127
+ export default defineConfig({ plugins: [zodAot()] });
110
128
 
111
- const result = validateUser(data);
112
- // { success: true, data: { name: "Alice", ... } }
113
- // or { success: false, error: { issues: [...] } }
129
+ // webpack.config.js
130
+ const zodAot = require("zod-aot/webpack");
131
+ module.exports = { plugins: [zodAot()] };
114
132
  ```
115
133
 
116
- ### Runtime Fallback (Development)
134
+ ### Options
117
135
 
118
- During development — before running the build step — use `createFallback` to wrap Zod schemas with the same interface:
136
+ ```typescript
137
+ zodAot({
138
+ include: ["src/schemas"], // only process files matching these substrings
139
+ exclude: ["test", "mock"], // skip files matching these substrings
140
+ })
141
+ ```
142
+
143
+ The plugin:
144
+ - Runs at build time (`enforce: "pre"`)
145
+ - Replaces `compile(Schema)` with optimized IIFE inline validators
146
+ - Adds `/* @__PURE__ */` annotations for tree-shaking
147
+ - Supports HMR in development
148
+
149
+ ## Low-Level API
150
+
151
+ For custom build scripts or advanced use cases, you can use the extractor and code generator directly:
119
152
 
120
153
  ```typescript
121
154
  import { z } from "zod";
122
- import { createFallback } from "zod-aot";
155
+ import { extractSchema, generateValidator } from "zod-aot";
123
156
 
124
157
  const UserSchema = z.object({
125
158
  name: z.string().min(3),
126
159
  age: z.number().int().positive(),
127
160
  });
128
161
 
129
- // Delegates to Zod at runtime — same interface as compiled validators
130
- const validator = createFallback(UserSchema);
162
+ // Extract schema into intermediate representation (IR)
163
+ const ir = extractSchema(UserSchema);
164
+
165
+ // Generate optimized JavaScript validation code
166
+ const { code, functionName } = generateValidator(ir, "validateUser");
167
+
168
+ // Execute the generated code
169
+ const safeParse = new Function(`${code}\nreturn ${functionName};`)();
170
+ const result = safeParse({ name: "Alice", age: 30 });
171
+ ```
172
+
173
+ ### `createFallback(zodSchema)`
174
+
175
+ Wraps a Zod schema to provide the `CompiledSchema` interface, delegating to Zod at runtime. Useful for development or unsupported schema types:
131
176
 
132
- validator.parse(data); // throws ZodError on failure
177
+ ```typescript
178
+ import { createFallback } from "zod-aot";
179
+
180
+ const validator = createFallback(UserSchema);
181
+ validator.parse(data); // throws on failure
133
182
  validator.safeParse(data); // { success, data/error }
134
183
  validator.is(data); // type guard (boolean)
135
184
  ```
136
185
 
137
- This lets you write code against the `CompiledSchema` interface and swap in the generated code at build time without changing any call sites.
138
-
139
186
  ## How It Works
140
187
 
141
188
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-aot",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Compile Zod schemas into zero-overhead validation functions at build time",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -22,7 +22,8 @@
22
22
  "zod-aot": "./dist/cli/index.js"
23
23
  },
24
24
  "files": [
25
- "dist"
25
+ "dist",
26
+ "LICENSE"
26
27
  ],
27
28
  "imports": {
28
29
  "#src/*": "./src/*"