zod-compiler 1.0.1 → 1.0.2
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.
- package/README.md +52 -22
- package/dist/unplugin/hoist.d.ts +19 -0
- package/dist/unplugin/hoist.d.ts.map +1 -0
- package/dist/unplugin/hoist.js +648 -0
- package/dist/unplugin/hoist.js.map +1 -0
- package/dist/unplugin/index.d.ts.map +1 -1
- package/dist/unplugin/index.js +12 -5
- package/dist/unplugin/index.js.map +1 -1
- package/dist/unplugin/transform.d.ts.map +1 -1
- package/dist/unplugin/transform.js +29 -12
- package/dist/unplugin/transform.js.map +1 -1
- package/dist/unplugin/types.d.ts +32 -7
- package/dist/unplugin/types.d.ts.map +1 -1
- package/dist/unplugin/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -90,9 +90,10 @@ const result = CreateUserSchema.safeParse(data); // { success, data/error }
|
|
|
90
90
|
At build time, the plugin:
|
|
91
91
|
|
|
92
92
|
1. Finds every file with `import ... from "zod"` (skips type-only imports)
|
|
93
|
-
2.
|
|
94
|
-
3.
|
|
95
|
-
4.
|
|
93
|
+
2. Statically pre-filters: files whose exports provably can't be schemas (functions, components, constants) are skipped without ever being executed
|
|
94
|
+
3. Executes the remaining candidates and detects exported Zod schemas
|
|
95
|
+
4. Compiles each schema into an optimized validator
|
|
96
|
+
5. Replaces the export with a tree-shakeable IIFE that preserves the full Zod API
|
|
96
97
|
|
|
97
98
|
**What "preserves the full Zod API" means:** The compiled schema inherits from the original Zod schema via `Object.create()`. So `._zod`, `.shape`, Standard Schema (`~standard`), `instanceof` checks — all still work. Libraries that accept Zod schemas (tRPC, Hono, React Hook Form) work without changes.
|
|
98
99
|
|
|
@@ -151,14 +152,15 @@ npx zod-compiler generate src/ --watch
|
|
|
151
152
|
|
|
152
153
|
### Options
|
|
153
154
|
|
|
154
|
-
| Option | Type | Default
|
|
155
|
-
| -------------- | ----------------------------- |
|
|
156
|
-
| `autoDiscover` | `boolean` | `false`
|
|
157
|
-
| `include` | `string[]` | —
|
|
158
|
-
| `exclude` | `string[]` | —
|
|
159
|
-
| `zodCompat` | `boolean` | `true`
|
|
160
|
-
| `verbose` | `boolean` | `false`
|
|
161
|
-
| `
|
|
155
|
+
| Option | Type | Default | Description |
|
|
156
|
+
| -------------- | ----------------------------- | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
157
|
+
| `autoDiscover` | `boolean` | `false` | Auto-detect all exported Zod schemas without `compile()` |
|
|
158
|
+
| `include` | `string[]` | — | Only process files matching these substrings |
|
|
159
|
+
| `exclude` | `string[]` | — | Skip files matching these substrings |
|
|
160
|
+
| `zodCompat` | `boolean` | `true` | Preserve Zod API via `Object.create()`. Set `false` for smaller output |
|
|
161
|
+
| `verbose` | `boolean` | `false` | Log per-schema compilation status during build |
|
|
162
|
+
| `hoist` | `boolean` | `true` | Hoist Zod schemas defined inside function bodies to module scope so they're constructed once instead of per call (babel-plugin-zod-hoist equivalent). Only expressions built purely from imports and literals are hoisted |
|
|
163
|
+
| `apply` | `"build" \| "serve" \| "all"` | builds + Vitest | **Vite only**: when the plugin runs. By default, production builds and test runs are compiled (so tests exercise what ships); plain dev servers use the Zod fallback. `"all"` also compiles the dev server; `"build"` also skips tests |
|
|
162
164
|
|
|
163
165
|
```typescript
|
|
164
166
|
zodCompiler({
|
|
@@ -168,9 +170,37 @@ zodCompiler({
|
|
|
168
170
|
});
|
|
169
171
|
```
|
|
170
172
|
|
|
171
|
-
> **Note:** Vitest
|
|
172
|
-
>
|
|
173
|
-
> `apply: "
|
|
173
|
+
> **Note:** Vitest is detected automatically (via the `VITEST` env var), so
|
|
174
|
+
> tests compile and exercise the same validators that ship to production —
|
|
175
|
+
> including their performance. Pass `apply: "build"` if you want tests to use
|
|
176
|
+
> the plain Zod fallback instead.
|
|
177
|
+
|
|
178
|
+
### Schema Hoisting
|
|
179
|
+
|
|
180
|
+
Schemas defined inside functions are rebuilt on every call — a hidden cost in
|
|
181
|
+
React components, request handlers, and helpers. With `hoist` (on by default),
|
|
182
|
+
the plugin moves them to module scope:
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
// before
|
|
186
|
+
function getSchema() {
|
|
187
|
+
return z.object({ name: z.string() }); // rebuilt per call
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// after (build output)
|
|
191
|
+
const _zh_94b7f5c1 = z.object({ name: z.string() });
|
|
192
|
+
function getSchema() {
|
|
193
|
+
return _zh_94b7f5c1; // built once per module
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Hoisting is conservative: only expressions built purely from **imported
|
|
198
|
+
bindings and literals** move. Anything referencing local variables,
|
|
199
|
+
module-level bindings, `this`, or globals (`new Date()`, `Math.random()`)
|
|
200
|
+
stays where it is, and inline `.parse(...)` calls are peeled so evaluation
|
|
201
|
+
stays at the call site (`z.string().parse(x)` → `_zh_….parse(x)`). Identical
|
|
202
|
+
schemas dedupe to a single binding. Combinator chains on imported schemas
|
|
203
|
+
also qualify (`BaseZodSchema.partial()`, `Base.extend({ a: z.string() })`).
|
|
174
204
|
|
|
175
205
|
### Bundle Size & Cross-File Dedup
|
|
176
206
|
|
|
@@ -192,7 +222,7 @@ when you don't need `instanceof` / `.shape` access on the compiled output.
|
|
|
192
222
|
|
|
193
223
|
### autoDiscover: Side Effects Warning
|
|
194
224
|
|
|
195
|
-
With `autoDiscover`, the plugin executes files to inspect their exports.
|
|
225
|
+
With `autoDiscover`, the plugin executes files to inspect their exports. A static pre-filter skips files whose exports provably can't be schemas without executing them — but if a file has schema-shaped exports AND side effects (starts a server, connects to a database), those side effects run at build time.
|
|
196
226
|
|
|
197
227
|
**Fix:** Use `include` to limit which files are scanned:
|
|
198
228
|
|
|
@@ -205,13 +235,13 @@ zodCompiler({
|
|
|
205
235
|
|
|
206
236
|
### autoDiscover vs compile()
|
|
207
237
|
|
|
208
|
-
| | autoDiscover
|
|
209
|
-
| ---------------------------- |
|
|
210
|
-
| Source code changes | None
|
|
211
|
-
| `zod-compiler` import needed | No
|
|
212
|
-
| What gets compiled | All exported Zod schemas
|
|
213
|
-
| Build-time file execution |
|
|
214
|
-
| Best for | New projects, framework integration
|
|
238
|
+
| | autoDiscover | compile() |
|
|
239
|
+
| ---------------------------- | ---------------------------------------------------------- | ------------------------------------------- |
|
|
240
|
+
| Source code changes | None | Wrap each schema |
|
|
241
|
+
| `zod-compiler` import needed | No | Yes |
|
|
242
|
+
| What gets compiled | All exported Zod schemas | Only wrapped schemas |
|
|
243
|
+
| Build-time file execution | Zod-importing files that may export schemas (pre-filtered) | Files with `import ... from "zod-compiler"` |
|
|
244
|
+
| Best for | New projects, framework integration | Gradual adoption, selective optimization |
|
|
215
245
|
|
|
216
246
|
## Framework Examples
|
|
217
247
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface ImportBindings {
|
|
2
|
+
/** Every runtime (non-type) imported binding name. */
|
|
3
|
+
all: Set<string>;
|
|
4
|
+
/** Bindings imported from a zod module (usually just `z`). */
|
|
5
|
+
zod: Set<string>;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Collect runtime import bindings with a regex over import statements.
|
|
9
|
+
* Type-only imports and `type` specifiers are excluded — they cannot be
|
|
10
|
+
* referenced at runtime, so excluding them keeps the capture rule sound.
|
|
11
|
+
*/
|
|
12
|
+
export declare function collectImportBindings(code: string): ImportBindings;
|
|
13
|
+
/**
|
|
14
|
+
* Hoist eligible Zod schema expressions to module scope.
|
|
15
|
+
* Returns the rewritten source, or null when nothing was hoisted.
|
|
16
|
+
*/
|
|
17
|
+
export declare function hoistZodSchemas(code: string): string | null;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=hoist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hoist.d.ts","sourceRoot":"","sources":["../../src/unplugin/hoist.ts"],"names":[],"mappings":"AA8CA,UAAU,cAAc;IACtB,sDAAsD;IACtD,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACjB,8DAA8D;IAC9D,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAClB;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAelE;AAsdD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CA+H3D"}
|
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
import { parseExpressionAt } from "acorn";
|
|
2
|
+
/**
|
|
3
|
+
* Hoist Zod schema construction out of functions to module scope —
|
|
4
|
+
* equivalent of babel-plugin-zod-hoist.
|
|
5
|
+
*
|
|
6
|
+
* Schemas built inside function bodies are re-constructed on every call:
|
|
7
|
+
*
|
|
8
|
+
* function getSchema() {
|
|
9
|
+
* return z.object({ name: z.string() }); // rebuilt per call
|
|
10
|
+
* }
|
|
11
|
+
*
|
|
12
|
+
* becomes
|
|
13
|
+
*
|
|
14
|
+
* const _zh_94b7f5c1 = z.object({ name: z.string() });
|
|
15
|
+
* function getSchema() {
|
|
16
|
+
* return _zh_94b7f5c1; // built once
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* Safety rules (mirroring babel-plugin-zod-hoist):
|
|
20
|
+
* - Only expressions built solely from **imported bindings and literals**
|
|
21
|
+
* are hoisted. Local variables, module-level const/let/var, `this`,
|
|
22
|
+
* `await`/`yield`, and globals (`new Date()`, `Math.random()`) all
|
|
23
|
+
* disqualify — hoisting would freeze their evaluation at module load.
|
|
24
|
+
* - Eligible roots: any binding imported from zod, an imported identifier
|
|
25
|
+
* matching /ZodSchema$/, or an imported identifier whose chain contains
|
|
26
|
+
* an inline z.* reference (e.g. `Base.extend({ a: z.string() })`).
|
|
27
|
+
* - Declarations are inserted at the top of the module (after shebang and
|
|
28
|
+
* directive prologue). Imports are initialized before module code runs,
|
|
29
|
+
* so referencing them from above their textual position is safe.
|
|
30
|
+
* - Names are content-hashed, so identical schemas dedupe to one binding.
|
|
31
|
+
*
|
|
32
|
+
* The source is TypeScript, which acorn cannot fully parse — candidates are
|
|
33
|
+
* located with a string/comment/depth-aware scanner and extracted with
|
|
34
|
+
* parseExpressionAt (the same technique as the autoDiscover rewrite).
|
|
35
|
+
* Anything unparseable (TS generics, `as` casts) is skipped: a miss leaves
|
|
36
|
+
* the schema unhoisted, never breaks the code.
|
|
37
|
+
*/
|
|
38
|
+
/** Imported identifiers matching this pattern are treated as schema roots. */
|
|
39
|
+
const SCHEMA_NAME_PATTERN = /ZodSchema$/;
|
|
40
|
+
/** Module specifiers whose bindings count as the zod namespace. */
|
|
41
|
+
const ZOD_MODULES = new Set(["zod", "zod/v4", "zod/mini", "zod/v4/mini"]);
|
|
42
|
+
/**
|
|
43
|
+
* Collect runtime import bindings with a regex over import statements.
|
|
44
|
+
* Type-only imports and `type` specifiers are excluded — they cannot be
|
|
45
|
+
* referenced at runtime, so excluding them keeps the capture rule sound.
|
|
46
|
+
*/
|
|
47
|
+
export function collectImportBindings(code) {
|
|
48
|
+
const all = new Set();
|
|
49
|
+
const zod = new Set();
|
|
50
|
+
const importPattern = /import\s+(type\s+)?([^'";]+?)\s+from\s*["']([^"']+)["']/g;
|
|
51
|
+
for (const match of code.matchAll(importPattern)) {
|
|
52
|
+
const [, typeOnly, clause, specifier] = match;
|
|
53
|
+
if (typeOnly || clause === undefined || specifier === undefined)
|
|
54
|
+
continue;
|
|
55
|
+
const isZod = ZOD_MODULES.has(specifier);
|
|
56
|
+
for (const name of parseImportClause(clause)) {
|
|
57
|
+
all.add(name);
|
|
58
|
+
if (isZod)
|
|
59
|
+
zod.add(name);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return { all, zod };
|
|
63
|
+
}
|
|
64
|
+
/** Extract local binding names from an import clause. */
|
|
65
|
+
function parseImportClause(clause) {
|
|
66
|
+
const names = [];
|
|
67
|
+
const namedStart = clause.indexOf("{");
|
|
68
|
+
// Default import and/or namespace import before the named group
|
|
69
|
+
const head = namedStart === -1 ? clause : clause.slice(0, namedStart);
|
|
70
|
+
for (const part of head.split(",")) {
|
|
71
|
+
const trimmed = part.trim();
|
|
72
|
+
if (!trimmed)
|
|
73
|
+
continue;
|
|
74
|
+
const ns = trimmed.match(/^\*\s*as\s+([A-Za-z_$][\w$]*)$/);
|
|
75
|
+
if (ns?.[1]) {
|
|
76
|
+
names.push(ns[1]);
|
|
77
|
+
}
|
|
78
|
+
else if (/^[A-Za-z_$][\w$]*$/.test(trimmed)) {
|
|
79
|
+
names.push(trimmed);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (namedStart !== -1) {
|
|
83
|
+
const namedEnd = clause.indexOf("}", namedStart);
|
|
84
|
+
const inner = clause.slice(namedStart + 1, namedEnd === -1 ? undefined : namedEnd);
|
|
85
|
+
for (const part of inner.split(",")) {
|
|
86
|
+
const spec = part.trim();
|
|
87
|
+
if (!spec || spec.startsWith("type "))
|
|
88
|
+
continue;
|
|
89
|
+
// `a as b` binds b; plain `a` binds a
|
|
90
|
+
const asMatch = spec.match(/^[A-Za-z_$][\w$]*\s+as\s+([A-Za-z_$][\w$]*)$/);
|
|
91
|
+
const name = asMatch?.[1] ?? (/^[A-Za-z_$][\w$]*$/.test(spec) ? spec : null);
|
|
92
|
+
if (name)
|
|
93
|
+
names.push(name);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return names;
|
|
97
|
+
}
|
|
98
|
+
/** Deterministic FNV-1a 32-bit hash, hex-encoded. */
|
|
99
|
+
function fnv1a(text) {
|
|
100
|
+
let hash = 0x811c9dc5;
|
|
101
|
+
for (let i = 0; i < text.length; i++) {
|
|
102
|
+
hash ^= text.charCodeAt(i);
|
|
103
|
+
hash = Math.imul(hash, 0x01000193);
|
|
104
|
+
}
|
|
105
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
106
|
+
}
|
|
107
|
+
const IDENT_START = /[A-Za-z_$]/;
|
|
108
|
+
const IDENT_PART = /[\w$]/;
|
|
109
|
+
/** After these tokens a `/` starts a regex literal, not division. */
|
|
110
|
+
const REGEX_PRECEDING_KEYWORDS = new Set([
|
|
111
|
+
"return",
|
|
112
|
+
"typeof",
|
|
113
|
+
"instanceof",
|
|
114
|
+
"in",
|
|
115
|
+
"of",
|
|
116
|
+
"new",
|
|
117
|
+
"delete",
|
|
118
|
+
"void",
|
|
119
|
+
"throw",
|
|
120
|
+
"do",
|
|
121
|
+
"else",
|
|
122
|
+
"case",
|
|
123
|
+
"yield",
|
|
124
|
+
"await",
|
|
125
|
+
]);
|
|
126
|
+
/** Is `/` after this token division (true) or a regex literal (false)? */
|
|
127
|
+
function isDivisionContext(lastToken) {
|
|
128
|
+
if (lastToken === ")" || lastToken === "]")
|
|
129
|
+
return true;
|
|
130
|
+
if (/^["'`]$/.test(lastToken))
|
|
131
|
+
return true;
|
|
132
|
+
if (/^[\w$]+$/.test(lastToken))
|
|
133
|
+
return !REGEX_PRECEDING_KEYWORDS.has(lastToken);
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Scan the source for candidate root identifiers (`z`, `FooZodSchema`, ...)
|
|
138
|
+
* followed by a `.`, tracking string/template/comment state and nesting
|
|
139
|
+
* depth. Depth 0 candidates are top-level initializers — already evaluated
|
|
140
|
+
* once — and are recorded only so their extents mask nested candidates.
|
|
141
|
+
*/
|
|
142
|
+
function scanCandidates(code, roots) {
|
|
143
|
+
const candidates = [];
|
|
144
|
+
let depth = 0;
|
|
145
|
+
// Template literals interleave string and expression states; the stack
|
|
146
|
+
// records the brace depth at which each `${` opened so the matching `}`
|
|
147
|
+
// resumes string state.
|
|
148
|
+
const templateStack = [];
|
|
149
|
+
let lastToken = "";
|
|
150
|
+
let i = 0;
|
|
151
|
+
while (i < code.length) {
|
|
152
|
+
const ch = code[i];
|
|
153
|
+
const next = code[i + 1];
|
|
154
|
+
// Comments
|
|
155
|
+
if (ch === "/" && next === "/") {
|
|
156
|
+
const nl = code.indexOf("\n", i);
|
|
157
|
+
i = nl === -1 ? code.length : nl + 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (ch === "/" && next === "*") {
|
|
161
|
+
const end = code.indexOf("*/", i + 2);
|
|
162
|
+
i = end === -1 ? code.length : end + 2;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (ch === "/") {
|
|
166
|
+
if (!isDivisionContext(lastToken)) {
|
|
167
|
+
i = skipRegexLiteral(code, i);
|
|
168
|
+
lastToken = ")"; // a regex literal is an operand
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
lastToken = ch;
|
|
172
|
+
i++;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
// Strings
|
|
176
|
+
if (ch === '"' || ch === "'") {
|
|
177
|
+
i = skipString(code, i, ch);
|
|
178
|
+
lastToken = ch;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
// Template literals
|
|
182
|
+
if (ch === "`") {
|
|
183
|
+
i = skipTemplateChunk(code, i + 1, templateStack, depth);
|
|
184
|
+
lastToken = "`";
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (templateStack.length > 0 &&
|
|
188
|
+
ch === "}" &&
|
|
189
|
+
depth === templateStack[templateStack.length - 1]) {
|
|
190
|
+
// End of a ${ } expression — back into template string state
|
|
191
|
+
templateStack.pop();
|
|
192
|
+
i = skipTemplateChunk(code, i + 1, templateStack, depth);
|
|
193
|
+
lastToken = "`";
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (ch === "(" || ch === "[" || ch === "{") {
|
|
197
|
+
depth++;
|
|
198
|
+
lastToken = ch;
|
|
199
|
+
i++;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (ch === ")" || ch === "]" || ch === "}") {
|
|
203
|
+
depth--;
|
|
204
|
+
lastToken = ch;
|
|
205
|
+
i++;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (IDENT_START.test(ch) && !IDENT_PART.test(code[i - 1] ?? "")) {
|
|
209
|
+
let j = i + 1;
|
|
210
|
+
while (j < code.length && IDENT_PART.test(code[j]))
|
|
211
|
+
j++;
|
|
212
|
+
const word = code.slice(i, j);
|
|
213
|
+
// Skip whitespace to check for the member access
|
|
214
|
+
let k = j;
|
|
215
|
+
while (k < code.length && /\s/.test(code[k]))
|
|
216
|
+
k++;
|
|
217
|
+
if (roots.has(word) && code[k] === "." && lastToken !== ".") {
|
|
218
|
+
candidates.push({ index: i, depth, name: word });
|
|
219
|
+
}
|
|
220
|
+
lastToken = word;
|
|
221
|
+
i = j;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (!/\s/.test(ch))
|
|
225
|
+
lastToken = ch;
|
|
226
|
+
i++;
|
|
227
|
+
}
|
|
228
|
+
return candidates;
|
|
229
|
+
}
|
|
230
|
+
function skipString(code, start, quote) {
|
|
231
|
+
for (let i = start + 1; i < code.length; i++) {
|
|
232
|
+
if (code[i] === "\\") {
|
|
233
|
+
i++;
|
|
234
|
+
}
|
|
235
|
+
else if (code[i] === quote || code[i] === "\n") {
|
|
236
|
+
return i + 1;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return code.length;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Skip a template string chunk; returns the offset after the closing
|
|
243
|
+
* `` ` `` or after a `${`, recording the current depth so the scanner can
|
|
244
|
+
* recognize the matching `}` later.
|
|
245
|
+
*/
|
|
246
|
+
function skipTemplateChunk(code, start, templateStack, depth) {
|
|
247
|
+
for (let i = start; i < code.length; i++) {
|
|
248
|
+
if (code[i] === "\\") {
|
|
249
|
+
i++;
|
|
250
|
+
}
|
|
251
|
+
else if (code[i] === "`") {
|
|
252
|
+
return i + 1;
|
|
253
|
+
}
|
|
254
|
+
else if (code[i] === "$" && code[i + 1] === "{") {
|
|
255
|
+
templateStack.push(depth);
|
|
256
|
+
return i + 2;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return code.length;
|
|
260
|
+
}
|
|
261
|
+
function skipRegexLiteral(code, start) {
|
|
262
|
+
let inClass = false;
|
|
263
|
+
for (let i = start + 1; i < code.length; i++) {
|
|
264
|
+
const ch = code[i];
|
|
265
|
+
if (ch === "\\") {
|
|
266
|
+
i++;
|
|
267
|
+
}
|
|
268
|
+
else if (ch === "[") {
|
|
269
|
+
inClass = true;
|
|
270
|
+
}
|
|
271
|
+
else if (ch === "]") {
|
|
272
|
+
inClass = false;
|
|
273
|
+
}
|
|
274
|
+
else if (ch === "/" && !inClass) {
|
|
275
|
+
return i + 1;
|
|
276
|
+
}
|
|
277
|
+
else if (ch === "\n") {
|
|
278
|
+
// Not a regex after all (unterminated) — treat as division
|
|
279
|
+
return start + 1;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return code.length;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Narrow a parsed expression to the largest call chain starting at `start`.
|
|
286
|
+
* parseExpressionAt can overshoot into surrounding operators
|
|
287
|
+
* (`z.string().min(1) || fallback` parses as a LogicalExpression) — descend
|
|
288
|
+
* through same-start children until a CallExpression is found.
|
|
289
|
+
*/
|
|
290
|
+
function narrowToCallChain(node, start) {
|
|
291
|
+
let current = node;
|
|
292
|
+
while (current.start === start) {
|
|
293
|
+
if (current.type === "CallExpression")
|
|
294
|
+
return current;
|
|
295
|
+
const child = sameStartChild(current, start);
|
|
296
|
+
if (!child)
|
|
297
|
+
return null;
|
|
298
|
+
current = child;
|
|
299
|
+
}
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
function sameStartChild(node, start) {
|
|
303
|
+
const record = node;
|
|
304
|
+
for (const key of ["left", "object", "callee", "test", "tag", "expression", "expressions"]) {
|
|
305
|
+
const value = record[key];
|
|
306
|
+
const child = Array.isArray(value) ? value[0] : value;
|
|
307
|
+
if (typeof child === "object" &&
|
|
308
|
+
child !== null &&
|
|
309
|
+
child.start === start &&
|
|
310
|
+
typeof child.type === "string") {
|
|
311
|
+
return child;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Walk a member/call chain down to its base identifier, collecting the
|
|
318
|
+
* non-computed method names along the spine. Computed members
|
|
319
|
+
* (`base[name]()`) are unanalyzable — the chain is rejected.
|
|
320
|
+
*/
|
|
321
|
+
function describeChain(node) {
|
|
322
|
+
const methods = [];
|
|
323
|
+
let current = node;
|
|
324
|
+
while (true) {
|
|
325
|
+
if (current.type === "CallExpression") {
|
|
326
|
+
current = current.callee;
|
|
327
|
+
}
|
|
328
|
+
else if (current.type === "MemberExpression") {
|
|
329
|
+
if (current.computed || current.property.type !== "Identifier")
|
|
330
|
+
return null;
|
|
331
|
+
methods.push(current.property.name);
|
|
332
|
+
current = current.object;
|
|
333
|
+
}
|
|
334
|
+
else if (current.type === "Identifier") {
|
|
335
|
+
return { root: current.name, methods };
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Combinators eligible on non-z chain bases (babel-plugin-zod-hoist's
|
|
344
|
+
* documented set) — `Base.extend({...})`, `Base.pick({...})`, ...
|
|
345
|
+
*/
|
|
346
|
+
const COMBINATOR_METHODS = new Set(["extend", "pick", "omit", "merge", "partial"]);
|
|
347
|
+
/**
|
|
348
|
+
* Methods that evaluate data rather than construct schemas. Hoisting one
|
|
349
|
+
* would move the evaluation (and any throw) to module load.
|
|
350
|
+
*/
|
|
351
|
+
const PARSE_METHODS = new Set([
|
|
352
|
+
"parse",
|
|
353
|
+
"safeParse",
|
|
354
|
+
"parseAsync",
|
|
355
|
+
"safeParseAsync",
|
|
356
|
+
"decode",
|
|
357
|
+
"encode",
|
|
358
|
+
"decodeAsync",
|
|
359
|
+
"encodeAsync",
|
|
360
|
+
]);
|
|
361
|
+
/**
|
|
362
|
+
* Scope-aware free-variable analysis of an expression. Nested function
|
|
363
|
+
* params and local declarations are bound names, not captures — so
|
|
364
|
+
* `z.string().refine((v) => v.length > 0)` has no free variables beyond `z`.
|
|
365
|
+
*/
|
|
366
|
+
function analyzeCaptures(expr) {
|
|
367
|
+
const free = new Set();
|
|
368
|
+
let impure = false;
|
|
369
|
+
function patternNames(node, into) {
|
|
370
|
+
switch (node.type) {
|
|
371
|
+
case "Identifier":
|
|
372
|
+
into.add(node.name);
|
|
373
|
+
return;
|
|
374
|
+
case "ObjectPattern":
|
|
375
|
+
for (const prop of node.properties) {
|
|
376
|
+
if (prop.type === "Property")
|
|
377
|
+
patternNames(prop.value, into);
|
|
378
|
+
else
|
|
379
|
+
patternNames(prop.argument, into);
|
|
380
|
+
}
|
|
381
|
+
return;
|
|
382
|
+
case "ArrayPattern":
|
|
383
|
+
for (const el of node.elements) {
|
|
384
|
+
if (el)
|
|
385
|
+
patternNames(el, into);
|
|
386
|
+
}
|
|
387
|
+
return;
|
|
388
|
+
case "AssignmentPattern":
|
|
389
|
+
patternNames(node.left, into);
|
|
390
|
+
return;
|
|
391
|
+
case "RestElement":
|
|
392
|
+
patternNames(node.argument, into);
|
|
393
|
+
return;
|
|
394
|
+
default:
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
function collectFunctionScope(body, into) {
|
|
399
|
+
// Flatten block scoping into the function scope — over-approximating
|
|
400
|
+
// bound names can only suppress a hoist's free set, and the failure
|
|
401
|
+
// mode is a loud ReferenceError, never silent misvalidation.
|
|
402
|
+
const stack = [body];
|
|
403
|
+
while (stack.length > 0) {
|
|
404
|
+
const node = stack.pop();
|
|
405
|
+
if (!node || typeof node !== "object")
|
|
406
|
+
continue;
|
|
407
|
+
if (node.type === "VariableDeclaration") {
|
|
408
|
+
for (const decl of node.declarations)
|
|
409
|
+
patternNames(decl.id, into);
|
|
410
|
+
}
|
|
411
|
+
else if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") {
|
|
412
|
+
if (node.id)
|
|
413
|
+
into.add(node.id.name);
|
|
414
|
+
continue; // do not descend into nested declarations' bodies here
|
|
415
|
+
}
|
|
416
|
+
else if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
417
|
+
continue; // nested functions get their own scope in visit()
|
|
418
|
+
}
|
|
419
|
+
for (const value of Object.values(node)) {
|
|
420
|
+
if (Array.isArray(value)) {
|
|
421
|
+
for (const item of value) {
|
|
422
|
+
if (typeof item === "object" && item !== null && "type" in item) {
|
|
423
|
+
stack.push(item);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
else if (typeof value === "object" && value !== null && "type" in value) {
|
|
428
|
+
stack.push(value);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
function visit(node, scopes) {
|
|
434
|
+
switch (node.type) {
|
|
435
|
+
case "Identifier": {
|
|
436
|
+
const name = node.name;
|
|
437
|
+
if (name !== "undefined" && !scopes.some((s) => s.has(name)))
|
|
438
|
+
free.add(name);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
case "ThisExpression":
|
|
442
|
+
case "Super":
|
|
443
|
+
case "AwaitExpression":
|
|
444
|
+
case "YieldExpression":
|
|
445
|
+
case "NewExpression":
|
|
446
|
+
case "TaggedTemplateExpression":
|
|
447
|
+
case "MetaProperty":
|
|
448
|
+
impure = true;
|
|
449
|
+
return;
|
|
450
|
+
case "MemberExpression":
|
|
451
|
+
visit(node.object, scopes);
|
|
452
|
+
if (node.computed)
|
|
453
|
+
visit(node.property, scopes);
|
|
454
|
+
return;
|
|
455
|
+
case "Property":
|
|
456
|
+
if (node.computed)
|
|
457
|
+
visit(node.key, scopes);
|
|
458
|
+
visit(node.value, scopes);
|
|
459
|
+
return;
|
|
460
|
+
case "FunctionExpression":
|
|
461
|
+
case "ArrowFunctionExpression": {
|
|
462
|
+
const scope = new Set();
|
|
463
|
+
for (const param of node.params)
|
|
464
|
+
patternNames(param, scope);
|
|
465
|
+
if (node.type === "FunctionExpression" && node.id)
|
|
466
|
+
scope.add(node.id.name);
|
|
467
|
+
collectFunctionScope(node.body, scope);
|
|
468
|
+
visit(node.body, [...scopes, scope]);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
default: {
|
|
472
|
+
for (const value of Object.values(node)) {
|
|
473
|
+
if (Array.isArray(value)) {
|
|
474
|
+
for (const item of value) {
|
|
475
|
+
if (typeof item === "object" && item !== null && "type" in item) {
|
|
476
|
+
visit(item, scopes);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
else if (typeof value === "object" && value !== null && "type" in value) {
|
|
481
|
+
visit(value, scopes);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
visit(expr, []);
|
|
488
|
+
return { free, impure };
|
|
489
|
+
}
|
|
490
|
+
/** Offset after the shebang and directive prologue ("use client", ...). */
|
|
491
|
+
function insertionOffset(code) {
|
|
492
|
+
let i = 0;
|
|
493
|
+
if (code.startsWith("#!")) {
|
|
494
|
+
const nl = code.indexOf("\n");
|
|
495
|
+
i = nl === -1 ? code.length : nl + 1;
|
|
496
|
+
}
|
|
497
|
+
while (true) {
|
|
498
|
+
// Skip whitespace and comments between directives
|
|
499
|
+
while (i < code.length) {
|
|
500
|
+
const ch = code[i];
|
|
501
|
+
if (/\s/.test(ch)) {
|
|
502
|
+
i++;
|
|
503
|
+
}
|
|
504
|
+
else if (ch === "/" && code[i + 1] === "/") {
|
|
505
|
+
const nl = code.indexOf("\n", i);
|
|
506
|
+
i = nl === -1 ? code.length : nl + 1;
|
|
507
|
+
}
|
|
508
|
+
else if (ch === "/" && code[i + 1] === "*") {
|
|
509
|
+
const end = code.indexOf("*/", i + 2);
|
|
510
|
+
i = end === -1 ? code.length : end + 2;
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
break;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
const directive = code.slice(i).match(/^(["'])use [^"'\n]*\1\s*;?[^\S\n]*\n?/);
|
|
517
|
+
if (!directive)
|
|
518
|
+
return i;
|
|
519
|
+
i += directive[0].length;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Hoist eligible Zod schema expressions to module scope.
|
|
524
|
+
* Returns the rewritten source, or null when nothing was hoisted.
|
|
525
|
+
*/
|
|
526
|
+
export function hoistZodSchemas(code) {
|
|
527
|
+
const imports = collectImportBindings(code);
|
|
528
|
+
if (imports.all.size === 0)
|
|
529
|
+
return null;
|
|
530
|
+
const roots = new Set(imports.zod);
|
|
531
|
+
for (const name of imports.all) {
|
|
532
|
+
if (SCHEMA_NAME_PATTERN.test(name))
|
|
533
|
+
roots.add(name);
|
|
534
|
+
}
|
|
535
|
+
// Imported roots that only qualify via an inline z.* reference in the
|
|
536
|
+
// chain are validated per-expression below; scan them as candidates too.
|
|
537
|
+
if (imports.zod.size > 0) {
|
|
538
|
+
for (const name of imports.all)
|
|
539
|
+
roots.add(name);
|
|
540
|
+
}
|
|
541
|
+
if (roots.size === 0)
|
|
542
|
+
return null;
|
|
543
|
+
const candidates = scanCandidates(code, roots);
|
|
544
|
+
if (candidates.length === 0)
|
|
545
|
+
return null;
|
|
546
|
+
const hoists = [];
|
|
547
|
+
const declByText = new Map();
|
|
548
|
+
let consumedUntil = 0;
|
|
549
|
+
for (const candidate of candidates) {
|
|
550
|
+
if (candidate.index < consumedUntil)
|
|
551
|
+
continue;
|
|
552
|
+
let parsed;
|
|
553
|
+
try {
|
|
554
|
+
parsed = parseExpressionAt(code, candidate.index, {
|
|
555
|
+
ecmaVersion: "latest",
|
|
556
|
+
sourceType: "module",
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
catch {
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
let chain = narrowToCallChain(parsed, candidate.index);
|
|
563
|
+
if (!chain)
|
|
564
|
+
continue;
|
|
565
|
+
// Mask nested candidates inside this extent regardless of eligibility —
|
|
566
|
+
// an inner z.string() of a top-level schema must not hoist on its own.
|
|
567
|
+
consumedUntil = chain.end;
|
|
568
|
+
// Depth 0 expressions are top-level statements/initializers: already
|
|
569
|
+
// evaluated once per module load, nothing to gain.
|
|
570
|
+
if (candidate.depth === 0)
|
|
571
|
+
continue;
|
|
572
|
+
// Peel trailing parse calls: for `z.object({...}).safeParse(input)`,
|
|
573
|
+
// hoist the construction and leave `.safeParse(input)` — with its
|
|
574
|
+
// local-variable arguments — at the call site.
|
|
575
|
+
let described = describeChain(chain);
|
|
576
|
+
while (chain !== null &&
|
|
577
|
+
described !== null &&
|
|
578
|
+
described.methods.length > 0 &&
|
|
579
|
+
PARSE_METHODS.has(described.methods[0])) {
|
|
580
|
+
const callee = chain.type === "CallExpression" ? chain.callee : null;
|
|
581
|
+
const inner = callee !== null && callee.type === "MemberExpression" ? callee.object : null;
|
|
582
|
+
chain = inner !== null && inner.type === "CallExpression" ? inner : null;
|
|
583
|
+
described = chain === null ? null : describeChain(chain);
|
|
584
|
+
}
|
|
585
|
+
if (chain === null || described === null)
|
|
586
|
+
continue;
|
|
587
|
+
if (described.root !== candidate.name)
|
|
588
|
+
continue;
|
|
589
|
+
if (described.methods.some((m) => PARSE_METHODS.has(m)))
|
|
590
|
+
continue;
|
|
591
|
+
const rootIsZod = imports.zod.has(candidate.name);
|
|
592
|
+
const rootMatchesPattern = SCHEMA_NAME_PATTERN.test(candidate.name);
|
|
593
|
+
// Non-z bases must look like schema derivation: only the documented
|
|
594
|
+
// combinator methods qualify (`Base.extend({...}).partial()`), so an
|
|
595
|
+
// arbitrary imported object with a z-mentioning argument is not hoisted.
|
|
596
|
+
if (!rootIsZod && !described.methods.every((m) => COMBINATOR_METHODS.has(m)))
|
|
597
|
+
continue;
|
|
598
|
+
const { free, impure } = analyzeCaptures(chain);
|
|
599
|
+
if (impure)
|
|
600
|
+
continue;
|
|
601
|
+
// Imports-and-literals only (babel-plugin-zod-hoist rule): local
|
|
602
|
+
// variables AND module-level bindings disqualify; globals too, since
|
|
603
|
+
// `new Date()` / `Math.random()` would freeze at module load.
|
|
604
|
+
let eligible = true;
|
|
605
|
+
for (const name of free) {
|
|
606
|
+
if (!imports.all.has(name)) {
|
|
607
|
+
eligible = false;
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
if (!eligible)
|
|
612
|
+
continue;
|
|
613
|
+
// Non-zod, non-pattern roots qualify only when the chain itself
|
|
614
|
+
// references a zod binding (`Base.extend({ a: z.string() })`).
|
|
615
|
+
if (!rootIsZod && !rootMatchesPattern) {
|
|
616
|
+
let referencesZod = false;
|
|
617
|
+
for (const name of free) {
|
|
618
|
+
if (imports.zod.has(name)) {
|
|
619
|
+
referencesZod = true;
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (!referencesZod)
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
const text = code.slice(candidate.index, chain.end);
|
|
627
|
+
let declName = declByText.get(text);
|
|
628
|
+
if (!declName) {
|
|
629
|
+
declName = `_zh_${fnv1a(text)}`;
|
|
630
|
+
declByText.set(text, declName);
|
|
631
|
+
}
|
|
632
|
+
hoists.push({ start: candidate.index, end: chain.end, name: declName });
|
|
633
|
+
}
|
|
634
|
+
if (hoists.length === 0)
|
|
635
|
+
return null;
|
|
636
|
+
// Apply replacements from the end so earlier offsets stay valid.
|
|
637
|
+
let result = code;
|
|
638
|
+
for (let i = hoists.length - 1; i >= 0; i--) {
|
|
639
|
+
const hoist = hoists[i];
|
|
640
|
+
result = result.slice(0, hoist.start) + hoist.name + result.slice(hoist.end);
|
|
641
|
+
}
|
|
642
|
+
const decls = [...declByText.entries()]
|
|
643
|
+
.map(([text, name]) => `const ${name} = ${text};`)
|
|
644
|
+
.join("\n");
|
|
645
|
+
const offset = insertionOffset(result);
|
|
646
|
+
return `${result.slice(0, offset)}${decls}\n${result.slice(offset)}`;
|
|
647
|
+
}
|
|
648
|
+
//# sourceMappingURL=hoist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hoist.js","sourceRoot":"","sources":["../../src/unplugin/hoist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,8EAA8E;AAC9E,MAAM,mBAAmB,GAAG,YAAY,CAAC;AAEzC,mEAAmE;AACnE,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC,CAAC;AAS1E;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAC;IAC9B,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAC;IAC9B,MAAM,aAAa,GAAG,0DAA0D,CAAC;IAEjF,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;QACjD,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,GAAG,KAAK,CAAC;QAC9C,IAAI,QAAQ,IAAI,MAAM,KAAK,SAAS,IAAI,SAAS,KAAK,SAAS;YAAE,SAAS;QAC1E,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACzC,KAAK,MAAM,IAAI,IAAI,iBAAiB,CAAC,MAAM,CAAC,EAAE,CAAC;YAC7C,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACd,IAAI,KAAK;gBAAE,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IACD,OA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AAC;IAErC,iEAAiE;IACjE,IAAI,MAAM,GAAG,IAAI,CAAC;IAClB,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAU,CAAC;QACjC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC/E,CAAC;IAED,MAAM,KAAK,GAAG,CAAC,GAAG,UAAU,CAAC,OAAO,EAAE,CAAC;SACpC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,SAAS,IAAI,MAAM,IAAI,GAAG,CAAC;SACjD,IAAI,CAAC,IAAI,CAAC,CAAC;IACd,MAAM,MAAM,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;IACvC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;AACvE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/unplugin/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/unplugin/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAiC3D,eAAO,MAAM,QAAQ,oFA4FpB,CAAC;AAEF,YAAY,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/unplugin/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import process from "node:process";
|
|
1
2
|
import { createUnplugin } from "unplugin";
|
|
2
3
|
import { invalidateModuleCache } from "#src/loader.js";
|
|
3
4
|
import { log, shouldTransform, transformCode } from "./transform.js";
|
|
@@ -32,14 +33,19 @@ export const unplugin = createUnplugin((options, meta) => {
|
|
|
32
33
|
const verbose = options?.verbose === true;
|
|
33
34
|
const mode = VIRTUAL_MODULE_FRAMEWORKS.has(meta.framework) ? "lean" : "inline";
|
|
34
35
|
const runtimeId = WP_FRAMEWORKS.has(meta.framework) ? WP_RUNTIME_ID : VIRTUAL_RUNTIME_ID;
|
|
35
|
-
|
|
36
|
+
// Vite only (other bundlers ignore the field): when the plugin runs.
|
|
37
|
+
// The default compiles production builds AND test runs — tests should
|
|
38
|
+
// exercise (and benefit from) the validators that ship — while plain dev
|
|
39
|
+
// servers skip AOT cost and use the Zod fallback. Vitest runs Vite in
|
|
40
|
+
// serve mode but is detectable via the VITEST env var / "test" mode.
|
|
41
|
+
const viteApply = options?.apply === "all"
|
|
42
|
+
? undefined
|
|
43
|
+
: (options?.apply ??
|
|
44
|
+
((_config, env) => env.command === "build" || env.mode === "test" || process.env["VITEST"] !== undefined));
|
|
36
45
|
return {
|
|
37
46
|
name: "zod-compiler",
|
|
38
47
|
enforce: "pre",
|
|
39
|
-
|
|
40
|
-
// build-time AOT cost — compile() falls back to Zod at dev time.
|
|
41
|
-
// Other bundlers ignore this field.
|
|
42
|
-
vite: apply === "all" ? {} : { apply },
|
|
48
|
+
vite: viteApply === undefined ? {} : { apply: viteApply },
|
|
43
49
|
resolveId(id) {
|
|
44
50
|
return resolveVirtualId(id);
|
|
45
51
|
},
|
|
@@ -68,6 +74,7 @@ export const unplugin = createUnplugin((options, meta) => {
|
|
|
68
74
|
verbose,
|
|
69
75
|
zodCompat: options?.zodCompat,
|
|
70
76
|
autoDiscover: options?.autoDiscover,
|
|
77
|
+
hoist: options?.hoist,
|
|
71
78
|
onBuildStats(s) {
|
|
72
79
|
stats.add(s);
|
|
73
80
|
},
|
|
@@ -1 +1 @@
|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B,KAAK,CAAC,KAAK,UAAU;gBAClG,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,MAAM,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CACvD,CAAC;YACF,KAAK,CAAC,KAAK,EAAE,CAAC;QAChB,CAAC;KACF,CAAC;AACJ,CAAC,CACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/unplugin/transform.ts"],"names":[],"mappings":"AAUA,OAAO,EAEL,KAAK,kBAAkB,EAExB,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/unplugin/transform.ts"],"names":[],"mappings":"AAUA,OAAO,EAEL,KAAK,kBAAkB,EAExB,MAAM,uBAAuB,CAAC;AAK/B,OAAO,KAAK,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAM7E;;GAEG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,wBAAwB,GAAG,OAAO,CAevF;AAED,wBAAgB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAGrC;AAOD;;;GAGG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA2IxB;AA8DD;;GAEG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,kBAAkB,EAAE,EAC7B,OAAO,CAAC,EAAE;IAAE,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,GAC5C,MAAM,CA2BR;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAUrE;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CACvC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,kBAAkB,EAAE,EAC7B,OAAO,CAAC,EAAE;IAAE,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,GAC5C,MAAM,CAuBR;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAaxD"}
|
|
@@ -4,6 +4,7 @@ import { FIN_DECL, generateIIFE, MK_VALIDATOR_DECL, ZOD_CONFIG_IMPORT, ZOD_MSG_D
|
|
|
4
4
|
import { aggregateUsedHelpers, compileSchemas, } from "#src/core/pipeline.js";
|
|
5
5
|
import { discoverSchemas } from "#src/discovery.js";
|
|
6
6
|
import { mayExportSchemas } from "#src/static-filter.js";
|
|
7
|
+
import { hoistZodSchemas } from "./hoist.js";
|
|
7
8
|
import { VIRTUAL_RUNTIME_ID } from "./virtual.js";
|
|
8
9
|
/** Matches a runtime (non-type-only) import from "zod". */
|
|
9
10
|
const HAS_RUNTIME_ZOD_IMPORT = /import\s+(?!type\s)[^;]*from\s+["']zod["']/;
|
|
@@ -42,12 +43,28 @@ export async function transformCode(code, id, options) {
|
|
|
42
43
|
const verbose = options.verbose === true;
|
|
43
44
|
const autoDiscover = options.autoDiscover === true;
|
|
44
45
|
const mode = options.mode;
|
|
46
|
+
// Hoist Zod schema construction out of function bodies to module scope
|
|
47
|
+
// (babel-plugin-zod-hoist equivalent). Mode-independent: inline schemas
|
|
48
|
+
// live exactly in the files that export none.
|
|
49
|
+
let working = code;
|
|
50
|
+
if (options.hoist !== false &&
|
|
51
|
+
(HAS_RUNTIME_ZOD_IMPORT.test(code) || code.includes("ZodSchema"))) {
|
|
52
|
+
const hoisted = hoistZodSchemas(code);
|
|
53
|
+
if (hoisted !== null) {
|
|
54
|
+
working = hoisted;
|
|
55
|
+
if (verbose) {
|
|
56
|
+
log(`Hoisted inline Zod schemas in ${id}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// When only hoisting changed the file, that is still a transform result.
|
|
61
|
+
const fallback = working === code ? null : working;
|
|
45
62
|
// Quick bail-out check
|
|
46
63
|
if (autoDiscover) {
|
|
47
64
|
// autoDiscover: any file with a runtime Zod import is a candidate.
|
|
48
65
|
// Skip `import type` — these files have no runtime schemas.
|
|
49
|
-
if (!HAS_RUNTIME_ZOD_IMPORT.test(
|
|
50
|
-
return
|
|
66
|
+
if (!HAS_RUNTIME_ZOD_IMPORT.test(working))
|
|
67
|
+
return fallback;
|
|
51
68
|
}
|
|
52
69
|
else {
|
|
53
70
|
// Legacy mode: require compile() from zod-compiler. The word-boundary
|
|
@@ -55,14 +72,14 @@ export async function transformCode(code, id, options) {
|
|
|
55
72
|
// "compile", so a plain includes("compile") would match every import of
|
|
56
73
|
// "zod-compiler" — \bcompile\b does not match inside "zod-compiler"
|
|
57
74
|
// (no boundary between "e" and "r") but matches compile( / { compile }.
|
|
58
|
-
if (!
|
|
59
|
-
return
|
|
75
|
+
if (!working.includes("zod-compiler") || !/\bcompile\b/.test(working))
|
|
76
|
+
return fallback;
|
|
60
77
|
}
|
|
61
78
|
// Static pre-filter: skip files whose exports provably cannot be schemas
|
|
62
79
|
// (functions, components, constants, type-only modules) without executing
|
|
63
80
|
// them. Conservative — anything ambiguous stays a candidate.
|
|
64
|
-
if (!(await mayExportSchemas(
|
|
65
|
-
return
|
|
81
|
+
if (!(await mayExportSchemas(working, id)))
|
|
82
|
+
return fallback;
|
|
66
83
|
// Discover schemas by executing the file. Module executions are cached in
|
|
67
84
|
// the shared loader; watch/HMR changes invalidate via invalidateModuleCache().
|
|
68
85
|
let schemas;
|
|
@@ -77,12 +94,12 @@ export async function transformCode(code, id, options) {
|
|
|
77
94
|
if (verbose) {
|
|
78
95
|
warn(`Skipping ${id}: ${msg}`);
|
|
79
96
|
}
|
|
80
|
-
return
|
|
97
|
+
return fallback;
|
|
81
98
|
}
|
|
82
99
|
throw new Error(`[zod-compiler] Failed to load schemas from ${id}: ${msg}`);
|
|
83
100
|
}
|
|
84
101
|
if (schemas.length === 0)
|
|
85
|
-
return
|
|
102
|
+
return fallback;
|
|
86
103
|
// Lean mode (Vite/Rollup/etc.) uses virtual:zod-compiler/runtime imports for cross-file dedup.
|
|
87
104
|
// Inline mode (webpack/rspack) emits self-contained file-level helpers.
|
|
88
105
|
let failedCount = 0;
|
|
@@ -107,7 +124,7 @@ export async function transformCode(code, id, options) {
|
|
|
107
124
|
}
|
|
108
125
|
}
|
|
109
126
|
if (compiled.length === 0)
|
|
110
|
-
return
|
|
127
|
+
return fallback;
|
|
111
128
|
// Report build stats only when at least one schema was compiled
|
|
112
129
|
options.onBuildStats?.({
|
|
113
130
|
files: 1,
|
|
@@ -125,13 +142,13 @@ export async function transformCode(code, id, options) {
|
|
|
125
142
|
const compileExportNames = new Set();
|
|
126
143
|
for (const s of compiled) {
|
|
127
144
|
const pattern = new RegExp(`\\b${s.exportName}\\s*=\\s*compile[\\s<(]`);
|
|
128
|
-
if (pattern.test(
|
|
145
|
+
if (pattern.test(working)) {
|
|
129
146
|
compileExportNames.add(s.exportName);
|
|
130
147
|
}
|
|
131
148
|
}
|
|
132
149
|
const compileSchemaInfos = compiled.filter((s) => compileExportNames.has(s.exportName));
|
|
133
150
|
const autoDiscoverSchemaInfos = compiled.filter((s) => !compileExportNames.has(s.exportName));
|
|
134
|
-
let result =
|
|
151
|
+
let result = working;
|
|
135
152
|
// Pass 1: rewrite compile() schemas with existing function
|
|
136
153
|
if (compileSchemaInfos.length > 0) {
|
|
137
154
|
result = rewriteSource(result, compileSchemaInfos, { zodCompat: options.zodCompat });
|
|
@@ -144,7 +161,7 @@ export async function transformCode(code, id, options) {
|
|
|
144
161
|
}
|
|
145
162
|
return injectRuntime(result, usedHelpers, mode, options.runtimeId);
|
|
146
163
|
}
|
|
147
|
-
return injectRuntime(rewriteSource(
|
|
164
|
+
return injectRuntime(rewriteSource(working, compiled, { zodCompat: options.zodCompat }), usedHelpers, mode, options.runtimeId);
|
|
148
165
|
}
|
|
149
166
|
/**
|
|
150
167
|
* Prepend the runtime helpers required by the rewritten source.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.js","sourceRoot":"","sources":["../../src/unplugin/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,SAAS,MAAM,WAAW,CAAC;AAElC,OAAO,EACL,QAAQ,EACR,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,oBAAoB,EAEpB,cAAc,GACf,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"transform.js","sourceRoot":"","sources":["../../src/unplugin/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,SAAS,MAAM,WAAW,CAAC;AAElC,OAAO,EACL,QAAQ,EACR,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,oBAAoB,EAEpB,cAAc,GACf,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,2DAA2D;AAC3D,MAAM,sBAAsB,GAAG,4CAA4C,CAAC;AAE5E;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,EAAU,EAAE,OAAkC;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scover?: boolean | undefined;
|
|
8
|
+
hoist?: boolean | undefined;
|
|
8
9
|
onBuildStats?: (stats: BuildStats) => void;
|
|
9
10
|
}
|
|
10
11
|
export interface BuildStats {
|
|
@@ -50,17 +51,41 @@ export interface ZodCompilerPluginOptions {
|
|
|
50
51
|
* @default false
|
|
51
52
|
*/
|
|
52
53
|
autoDiscover?: boolean | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Hoist Zod schema construction out of function bodies to module scope
|
|
56
|
+
* (the babel-plugin-zod-hoist optimization). A schema defined inside a
|
|
57
|
+
* function — a React component, a request handler — is otherwise rebuilt
|
|
58
|
+
* on every call:
|
|
59
|
+
*
|
|
60
|
+
* ```typescript
|
|
61
|
+
* function getSchema() {
|
|
62
|
+
* return z.object({ name: z.string() }); // rebuilt per call
|
|
63
|
+
* }
|
|
64
|
+
* // becomes
|
|
65
|
+
* const _zh_94b7f5c1 = z.object({ name: z.string() });
|
|
66
|
+
* function getSchema() {
|
|
67
|
+
* return _zh_94b7f5c1; // built once
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* Only expressions built purely from imported bindings and literals are
|
|
72
|
+
* hoisted — anything referencing local variables, module-level bindings,
|
|
73
|
+
* `this`, or globals stays put. Identical schemas dedupe to one binding.
|
|
74
|
+
* @default true
|
|
75
|
+
*/
|
|
76
|
+
hoist?: boolean | undefined;
|
|
53
77
|
/**
|
|
54
78
|
* **Vite only** (other bundlers ignore this option): when the plugin runs.
|
|
55
79
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
80
|
+
* By default the plugin compiles production builds **and test runs**
|
|
81
|
+
* (Vitest is detected via the `VITEST` env var / `"test"` mode), so tests
|
|
82
|
+
* exercise — and benefit from — the same compiled validators that ship.
|
|
83
|
+
* Plain dev servers skip AOT compilation cost; `compile()` transparently
|
|
84
|
+
* falls back to Zod's runtime validation there, so behavior stays correct.
|
|
59
85
|
*
|
|
60
|
-
* Set `"
|
|
61
|
-
*
|
|
62
|
-
* default
|
|
63
|
-
* @default "build"
|
|
86
|
+
* Set `"build"` to also skip tests, `"serve"` for dev/test-only, or
|
|
87
|
+
* `"all"` to compile everywhere including the dev server.
|
|
88
|
+
* @default builds + Vitest
|
|
64
89
|
*/
|
|
65
90
|
apply?: "build" | "serve" | "all" | undefined;
|
|
66
91
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/unplugin/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAEhE,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,WAAW,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAChC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC9B,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACnC,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;CAC5C;AAED,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,qBAAsB,YAAW,UAAU;IACtD,KAAK,SAAK;IACV,OAAO,SAAK;IACZ,SAAS,SAAK;IACd,MAAM,SAAK;IAEX,GAAG,CAAC,CAAC,EAAE,UAAU,GAAG,IAAI;IAOxB,KAAK,IAAI,IAAI;CAMd;AAED,MAAM,WAAW,wBAAwB;IACvC,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,4EAA4E;IAC5E,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CA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AAC;IAC9B;;;;;;;;OAQG;IACH,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACnC;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC5B;;;;;;;;;;;;OAYG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,KAAK,GAAG,SAAS,CAAC;CAC/C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/unplugin/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/unplugin/types.ts"],"names":[],"mappings":"AAmBA,MAAM,OAAO,qBAAqB;IAChC,KAAK,GAAG,CAAC,CAAC;IACV,OAAO,GAAG,CAAC,CAAC;IACZ,SAAS,GAAG,CAAC,CAAC;IACd,MAAM,GAAG,CAAC,CAAC;IAEX,GAAG,CAAC,CAAa;QACf,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,KAAK,CAAC;QACtB,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC,OAAO,CAAC;QAC1B,IAAI,CAAC,SAAS,IAAI,CAAC,CAAC,SAAS,CAAC;QAC9B,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC;IAC1B,CAAC;IAED,KAAK;QACH,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QACf,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;CACF"}
|