zod-compiler 2.0.1 → 2.0.3

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 (71) hide show
  1. package/README.md +62 -58
  2. package/dist/cli/commands/check.d.ts.map +1 -1
  3. package/dist/cli/commands/check.js +5 -0
  4. package/dist/cli/commands/check.js.map +1 -1
  5. package/dist/core/codegen/build-path.d.ts.map +1 -1
  6. package/dist/core/codegen/build-path.js +107 -28
  7. package/dist/core/codegen/build-path.js.map +1 -1
  8. package/dist/core/codegen/context.d.ts +21 -11
  9. package/dist/core/codegen/context.d.ts.map +1 -1
  10. package/dist/core/codegen/context.js +15 -1
  11. package/dist/core/codegen/context.js.map +1 -1
  12. package/dist/core/codegen/fast-path.d.ts +3 -1
  13. package/dist/core/codegen/fast-path.d.ts.map +1 -1
  14. package/dist/core/codegen/fast-path.js +7 -4
  15. package/dist/core/codegen/fast-path.js.map +1 -1
  16. package/dist/core/codegen/index.js +4 -3
  17. package/dist/core/codegen/index.js.map +1 -1
  18. package/dist/core/codegen/issue-decls.d.ts +67 -5
  19. package/dist/core/codegen/issue-decls.d.ts.map +1 -1
  20. package/dist/core/codegen/issue-decls.js +72 -7
  21. package/dist/core/codegen/issue-decls.js.map +1 -1
  22. package/dist/core/codegen/schemas/array.d.ts.map +1 -1
  23. package/dist/core/codegen/schemas/array.js +3 -3
  24. package/dist/core/codegen/schemas/array.js.map +1 -1
  25. package/dist/core/codegen/schemas/default.d.ts +10 -0
  26. package/dist/core/codegen/schemas/default.d.ts.map +1 -1
  27. package/dist/core/codegen/schemas/default.js +11 -0
  28. package/dist/core/codegen/schemas/default.js.map +1 -1
  29. package/dist/core/codegen/schemas/effect.d.ts +13 -1
  30. package/dist/core/codegen/schemas/effect.d.ts.map +1 -1
  31. package/dist/core/codegen/schemas/effect.js +26 -3
  32. package/dist/core/codegen/schemas/effect.js.map +1 -1
  33. package/dist/core/codegen/schemas/fallback.d.ts +17 -1
  34. package/dist/core/codegen/schemas/fallback.d.ts.map +1 -1
  35. package/dist/core/codegen/schemas/fallback.js +31 -15
  36. package/dist/core/codegen/schemas/fallback.js.map +1 -1
  37. package/dist/core/codegen/schemas/number.d.ts.map +1 -1
  38. package/dist/core/codegen/schemas/number.js +35 -35
  39. package/dist/core/codegen/schemas/number.js.map +1 -1
  40. package/dist/core/codegen/schemas/object.d.ts.map +1 -1
  41. package/dist/core/codegen/schemas/object.js +64 -29
  42. package/dist/core/codegen/schemas/object.js.map +1 -1
  43. package/dist/core/codegen/schemas/string.d.ts.map +1 -1
  44. package/dist/core/codegen/schemas/string.js +3 -3
  45. package/dist/core/codegen/schemas/string.js.map +1 -1
  46. package/dist/core/codegen/schemas/template-literal.d.ts.map +1 -1
  47. package/dist/core/codegen/schemas/template-literal.js +1 -1
  48. package/dist/core/codegen/schemas/template-literal.js.map +1 -1
  49. package/dist/core/extract/index.d.ts.map +1 -1
  50. package/dist/core/extract/index.js +2 -0
  51. package/dist/core/extract/index.js.map +1 -1
  52. package/dist/core/extract/zod-version.d.ts +78 -0
  53. package/dist/core/extract/zod-version.d.ts.map +1 -0
  54. package/dist/core/extract/zod-version.js +109 -0
  55. package/dist/core/extract/zod-version.js.map +1 -0
  56. package/dist/core/iife.d.ts +8 -0
  57. package/dist/core/iife.d.ts.map +1 -1
  58. package/dist/core/iife.js +8 -0
  59. package/dist/core/iife.js.map +1 -1
  60. package/dist/jit.d.ts.map +1 -1
  61. package/dist/jit.js +9 -1
  62. package/dist/jit.js.map +1 -1
  63. package/dist/runtime.d.ts +3 -0
  64. package/dist/runtime.js +4 -1
  65. package/dist/unplugin/hoist-compile.d.ts.map +1 -1
  66. package/dist/unplugin/hoist-compile.js +3 -1
  67. package/dist/unplugin/hoist-compile.js.map +1 -1
  68. package/dist/unplugin/transform.d.ts.map +1 -1
  69. package/dist/unplugin/transform.js +5 -0
  70. package/dist/unplugin/transform.js.map +1 -1
  71. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"iife.js","names":[],"sources":["../../src/core/iife.ts"],"sourcesContent":["/**\n * Shared CompiledSchema<T> IIFE generation.\n * Used by both CLI emitter and unplugin transform.\n */\n\nimport { RETAINED_SCHEMA_VAR } from \"./codegen/context.js\";\nimport type { CompiledSchemaInfo } from \"./pipeline.js\";\n\n/**\n * Import statement required by generateIIFE output (references\n * __zodCompilerConfig). `core` is bound for $ZodAsyncError: superRefine and\n * custom callbacks may only reveal that they are async through the promise\n * they return, at which point zod's own synchronous parse raises.\n */\nexport const ZOD_CONFIG_IMPORT =\n 'import { config as __zodCompilerConfig, core as __zcCore, ZodRealError as __zcZodError } from \"zod\";';\n\n/**\n * File-level `__zcMsg` declaration (must appear once after ZOD_CONFIG_IMPORT):\n * the message an issue gets when nothing was baked into it at build time.\n *\n * Resolves zod's tail of `finalizeIssue` — `config.customError` then\n * `config.localeError` then \"Invalid input\" — and does it PER CALL, because the\n * config is mutable: `z.config({ localeError })` in an entry point runs after\n * the schema modules it imports, so a value snapshotted at module init misses\n * it. Reading a captured `localeError` alone also dropped `customError`\n * outright, silently ignoring the global map most i18n setups install.\n *\n * The head of zod's chain — the schema's own `error` option — is baked into the\n * issue at build time and short-circuits this. The one link that cannot be\n * reproduced is a per-CALL `ctx.error`, which would have to travel through\n * `safeParse`; that entry point sits at V8's inlining budget, where even an\n * unused extra parameter measured ~12% on every parse.\n *\n * Only ever called while building an error, never on a successful parse.\n */\nexport const ZOD_MSG_DECLARATION =\n 'function __zcUw(m){return typeof m===\"string\"?m:(m===undefined||m===null?undefined:m.message);}' +\n \"var __zcMsg=function(iss){var c=__zodCompilerConfig(),m;\" +\n \"if(c.customError){m=__zcUw(c.customError(iss));if(m!==undefined&&m!==null)return m;}\" +\n \"if(c.localeError){m=__zcUw(c.localeError(iss));if(m!==undefined&&m!==null)return m;}\" +\n 'return \"Invalid input\";};';\n\n/**\n * Shared failure-result for __zcFin / __zcFinD. Inline mode (CLI emitter)\n * declares it once per compiled file; lean mode (all unplugin bundlers) declares\n * it once per bundle in the plugin-materialized runtime module (module-local —\n * generated code only ever references __zcFin/__zcFinD, never __ZcFail).\n *\n * Why a prototype getter and not `{success:false, get error(){...}}`: an object\n * literal with an inline accessor forces V8 down its slow accessor-defining\n * allocation path — ~110ns per failure, measured — which dominates the entire\n * invalid-input cost whenever callers never read `.error`. Hosting `error` on\n * the prototype turns each failure into a plain field-only instantiation (~2ns,\n * ~13x), with the lazy-cache semantics intact. (Trade-off: `error` is a\n * prototype accessor, so it no longer shows up in `Object.keys(result)` / spread\n * / JSON.stringify of the result wrapper — `.success`/`.error`/`.data` access,\n * destructuring, and `in` are unaffected.)\n *\n * One class serves both finalizers, so the instances share one hidden class:\n * __zcFin passes pre-collected issues in `_e` (with `_f===null`); __zcFinD\n * passes the hosted slow-walk in `_f` plus the input in `_i`, and the getter\n * runs the walk on first `.error` read. The whole finalization — locale fill\n * (__zcMsg applied ONLY when an issue carries no message, never overwriting a\n * baked-in custom/fallback message), input strip, and ZodError construction\n * (zod v4 JSON.stringifies every issue into `message` and captures a stack\n * trace) — stays deferred inside the cached accessor exactly as before, since\n * the issues array is observable solely through `.error`.\n *\n * `input` is `delete`d, not assigned `undefined`. Key PRESENCE is observable —\n * `\"input\" in issue`, `Object.keys(issue)`, object spread, `toStrictEqual`\n * against a zod issue — and zod's `util.finalizeIssue` does `delete full.input`\n * whenever `reportInput` is off, so assignment left every compiled issue one\n * enumerable key wider than zod's. The delete's dictionary-mode transition is\n * affordable precisely BECAUSE of the deferral above: it runs only after a\n * caller asks for `.error` on a failed parse, and is memoised in `_c`. Neither\n * a successful parse nor a `.success`/`.is()` check on a rejected one reaches\n * it — measured, the two are unchanged, while the `.error` read itself goes\n * ~3.95us -> ~4.19us per failure, i.e. ~6% of a path whose cost is already\n * dominated by the ZodError construction on the next line (stack capture plus a\n * JSON.stringify of every issue).\n */\nexport const FAIL_CLASS_DECL =\n \"function __ZcFail(e,f,i){this.success=false;this._e=e;this._f=f;this._i=i;this._c=undefined;}\" +\n 'Object.defineProperty(__ZcFail.prototype,\"error\",{configurable:true,get:function(){' +\n \"if(this._c)return this._c;\" +\n \"var e=this._f!==null?this._f(this._i):this._e;\" +\n 'for(var i=0;i<e.length;i++){if(e[i].message===undefined&&typeof __zcMsg===\"function\")e[i].message=__zcMsg(e[i]);delete e[i].input;delete e[i].continue;}' +\n \"return this._c=new __zcZodError(e);}});\";\n\n/** Eager finalizer (mutation / partial-fast-path schemas): issues already\n * collected in `e`; success short-circuits to a plain result literal. */\nexport const FIN_DECL =\n \"function __zcFin(e,d){if(!e.length)return{success:true,data:d};return new __ZcFail(e,null,null);}\";\n\n/**\n * Deferred-collection finalizer for Fast-Path-eligible schemas. When the\n * fast check fails, the ENTIRE slow path (the issue-collecting re-walk) is\n * pushed into the cached `.error` accessor instead of running eagerly:\n * fast-eligible schemas never mutate, so the walk's only output is the\n * issues array, which is observable solely through `.error` — one step\n * further along the same lazy boundary `__zcFin` already established (locale\n * fill, input strip, ZodError construction). A failed safeParse whose\n * `.error` is never read costs the fast check alone.\n *\n * Takes the schema's HOSTED slow-walk function plus the input — NOT a\n * per-call closure: `__zcFinD(__sw_N, input)` allocates only the result\n * object, where `__zcFinD(function(){...})` paid a closure environment and\n * function object per failure. Hosting the walk also shrinks safeParse to\n * two statements, within V8's inlining budget (the success-path result\n * literal becomes escape-analyzable at monomorphic call sites).\n *\n * The walk re-reads `input` at `.error`-read time; a caller that mutates\n * the input between safeParse and reading `.error` sees issues for the\n * mutated value (zod materializes at parse time). Same caveat class as the\n * documented __zcFin deferral.\n */\nexport const FIN_DEFERRED_DECL = \"function __zcFinD(f,inp){return new __ZcFail(null,f,inp);}\";\n\n/**\n * Compact-mode failure class — a lazy failure that delegates error reporting to\n * the ORIGINAL Zod schema's `safeParse`. Used by `output: \"compact\"`, where the\n * compiled slow walk is dropped entirely: a mutation-free schema's fast check\n * is the only generated validation, and on a fast-check failure the cold error\n * path is produced by the retained Zod schema itself (`zod` is the source of\n * truth, so the issues are byte-identical — no second validation engine).\n *\n * `_z` is the schema's PRISTINE safeParse method, captured by\n * emitRetainedMethod (see context.ts), and `_r` is its receiver — the `__zs`\n * binding generateIIFE places above that capture. Both are read before the\n * trailing `__zcMkv` call installs anything, so the method is zod's own\n * implementation, never the compiled delegate, avoiding infinite recursion\n * without allocating a bound function. The zod parse is deferred until `.error`\n * is read and cached, so\n * the common `safeParse(x).success`/`.is(x)` checks on invalid input cost only\n * the fast check (zod never runs) — the same deferral boundary `__zcFinD`\n * establishes for the compiled slow walk. Sound because compact mode is gated\n * on a TOTAL fast path: `fc(input) === false` ⟹ zod rejects, so `success:false`\n * holds without consulting zod.\n *\n * The getter returns zod's OWN ZodError verbatim (no locale fill / input strip /\n * re-wrap — zod already finalized it), so a delegated failure is exactly what\n * the unaltered schema would have produced.\n */\nexport const FAILZ_CLASS_DECL =\n \"function __ZcFailZ(z,r,i){this.success=false;this._z=z;this._r=r;this._i=i;this._c=undefined;}\" +\n 'Object.defineProperty(__ZcFailZ.prototype,\"error\",{configurable:true,get:function(){' +\n \"return this._c||(this._c=this._z.call(this._r,this._i).error);}});\";\n\n/** Compact-mode finalizer: retain a pristine safeParse, its receiver, and input lazily. */\nexport const FINZ_DECL = \"function __zcFinZ(z,r,i){return new __ZcFailZ(z,r,i);}\";\n\n/**\n * Validator factory. Inline mode (CLI emitter) declares it once per compiled\n * file; lean mode (all unplugin bundlers) exports it once per bundle from the\n * plugin-materialized runtime module — generated code never imports it from\n * the zod-compiler package itself, so zod-compiler stays a devDependency and\n * the helper set is always version-locked to the codegen that calls it.\n * Wraps a safeParse function into the CompiledSchema interface.\n *\n * IDENTITY-PRESERVING: with zodCompat (schema != null) the compiled\n * parse/safeParse/parseAsync/safeParseAsync are installed as OWN properties\n * on the original schema object, which is returned as-is. zod v4 keys\n * several APIs on object identity — toJSONSchema's ctx.seen registers the\n * object it is handed while each processor closure captures the original\n * inst (a wrapper crashes `optionalProcessor` with \"Cannot set properties\n * of undefined (setting 'ref')\" the moment a compiled schema is composed\n * into another schema), and globalRegistry/.meta() is a WeakMap keyed by\n * the schema instance (a wrapper silently loses OpenAPI titles/ids). An\n * Object.create wrapper breaks both; mutating the original breaks neither\n * (zod's internal parsing flows through _zod.run, never the public\n * methods, and derived schemas — .optional(), .extend() — are fresh\n * instances that fall back to plain zod). schema=null (zodCompat: false)\n * still produces a plain method-bag object.\n *\n * fc is the schema's hosted fast-check boolean function (null when no Fast\n * Path exists). parse()/parseAsync() try it first and return the input\n * directly on success: fc is small enough for V8 to inline, so the hot parse\n * path runs with zero allocations — calling fn would allocate an intermediate\n * SafeParseResult that escape analysis cannot remove (fn never inlines).\n * Fast-path-eligible schemas never mutate, so fc(input) ⟹ data === input.\n *\n * is is the TOTAL fast-check predicate (fc when the fast path is total, else\n * null). Installed as `.is()` — a zero-allocation boolean type guard. When\n * null (partial fast path or none) `.is()` derives from fn(input).success: a\n * partial fc can pass-through valid input but its `false` does not imply\n * rejection (a default/catch may still succeed), so it would be unsound as a\n * standalone guard.\n *\n * parseAsync/safeParseAsync wrap the SYNC validator, which is right for every\n * schema the compiler can reproduce — none of them are async. It is wrong for\n * the ones it cannot: an `async` refinement or a `z.promise()` extracts to a\n * Zod delegate, and delegating means calling Zod's SYNCHRONOUS safeParse, which\n * raises `$ZodAsyncError` by design. Wrapping that gave the compiled schema an\n * async pair that rejected with `$ZodAsyncError` for EVERY input — including\n * valid ones — where Zod resolves normally, so `await UserSchema.parseAsync(x)`\n * stopped working the moment any part of the schema went async.\n *\n * So both are guarded: a synchronous throw hands off to the schema's ORIGINAL\n * async method, captured before these are installed — the same escape hatch\n * `~standard` already uses for its throw path, and for the same reason (the\n * compiled validator has no async mode to offer, and Zod's is exact). It also\n * fixes the smaller wart that a throwing `fn` made `parseAsync` throw\n * SYNCHRONOUSLY rather than return a rejected promise. With `zodCompat: false`\n * there is no schema to delegate to and the throw propagates as before.\n *\n * `~standard` is REPLACED, not merely preserved. Zod builds it lazily as\n * `validate: (v) => safeParse(inst, v)` — the core FUNCTION, which goes straight\n * to `inst._zod.run`. It never reads the schema's own `safeParse` property, so\n * installing the compiled one leaves this route entirely uncompiled: measured\n * 271.7 ns against the compiled 26.6 ns on the same schema, i.e. Standard Schema\n * consumers (tRPC, Hono, TanStack) were getting plain Zod.\n *\n * Zod's own `~standard` is never READ, only overwritten. Zod installs the slot\n * with `util.defineLazy` — commented there as \"avoid creating objects for every\n * schema\" — so it is an accessor that builds `{version, vendor, validate}` plus\n * its closure on first touch. Reading it to capture a fallback fired that getter\n * for every compiled schema while the module was still initializing.\n *\n * How much that costs depends on the entry point, and only one of them is free:\n * classic `zod` forces the slot itself during `ZodType.init` (it does\n * `Object.assign(inst[\"~standard\"], { jsonSchema })`), so there the read hit an\n * already-built object and cost only an accessor call. `zod/mini` and raw\n * `zod/v4/core` never touch it, so for those the read built — and retained — an\n * object and a closure per schema, purely to capture a fallback the schema will\n * most likely never expose to a Standard Schema consumer.\n *\n * The throw path is rebuilt instead of captured. Zod's validate catches a\n * synchronous throw and retries through `safeParseAsync` — that is how an async\n * refinement resolves and how a throwing check surfaces as a rejected promise\n * rather than a synchronous throw — so calling the already-captured `zspa` and\n * mapping its result is the same route to the same result. `vendor` is likewise\n * a constant: schema discovery only ever admits zod schemas, and classic, mini\n * and core all hardcode `vendor: \"zod\"` themselves.\n *\n * Not carried over (unchanged by this, and pre-dating it): classic's\n * `~standard.jsonSchema` extension, which the replacement object has never\n * reproduced.\n *\n * Installed with defineProperty rather than assignment: Zod's lazy setter\n * redefines the slot as non-writable, so a second `__zcMkv` on the same schema\n * object — two exports aliasing one schema — would throw under ESM strict mode.\n */\nexport const MK_VALIDATOR_DECL =\n \"function __zcMkv(fn,schema,fc,is){var w=schema||{};var zpa=w.parseAsync,zspa=w.safeParseAsync;w.parse=fc?function(input){if(fc(input))return input;var r=fn(input);if(r.success)return r.data;throw r.error;}:function(input){var r=fn(input);if(r.success)return r.data;throw r.error;};w.safeParse=fn;w.safeParseAsync=function(input){try{return Promise.resolve(fn(input));}catch(e){if(zspa)return zspa(input);throw e;}};w.parseAsync=fc?function(input){try{if(fc(input))return Promise.resolve(input);var r=fn(input);if(r.success)return Promise.resolve(r.data);return Promise.reject(r.error);}catch(e){if(zpa)return zpa(input);throw e;}}:function(input){try{var r=fn(input);if(r.success)return Promise.resolve(r.data);return Promise.reject(r.error);}catch(e){if(zpa)return zpa(input);throw e;}};w.is=is||function(input){return fn(input).success;};\" +\n 'Object.defineProperty(w,\"~standard\",{configurable:true,value:{version:1,vendor:\"zod\",validate:function(input){var r;try{if(fc&&fc(input))return{value:input};r=fn(input);}catch(e){if(zspa)return zspa(input).then(function(q){return q.success?{value:q.data}:{issues:q.error.issues};});throw e;}return r.success?{value:r.data}:{issues:r.error.issues};}}});' +\n \"return w;}\";\n\nfunction extractFunctionName(functionDef: string): string {\n const match = /^function\\s+(\\w+)\\s*\\(/.exec(functionDef);\n if (!match?.[1]) {\n throw new Error(\"Cannot extract function name from generated code\");\n }\n return match[1];\n}\n\n/**\n * Does the IIFE's preamble DEREFERENCE the retained schema at evaluation time?\n *\n * Only `__rf` does: every entry is `__zs<accessPath>`, and an access path walks\n * the schema's structure — `._zod.innerType` fires a `z.lazy()` getter,\n * `.shape` materializes a `z.object()` shape (zod v4 reads every property\n * descriptor, so ONE `.shape` read fires ALL of an object's recursion getters).\n * Binding `__zs` itself does not: constructing the schema leaves deferred\n * callbacks unforced, and `usesRetainedSchema`'s only read is `__zs.safeParse`,\n * a prototype getter that binds the method and installs the bound copy on the\n * instance without touching the schema's structure.\n *\n * The distinction decides whether the IIFE may be evaluated inside the\n * INITIALIZER of the binding the schema's own deferred callbacks close over —\n * see the self-referential path in the unplugin's autoDiscover rewrite.\n */\nexport function iifeDerefsSchema(schema: CompiledSchemaInfo): boolean {\n return schema.refEntries.length > 0;\n}\n\n/**\n * Generate a `/* @__PURE__ * /` IIFE wrapping a compiled validator.\n *\n * @param schemaExpr - Expression resolving to the original Zod schema\n * (e.g. `\"UserSchema\"` in unplugin, `\"(__src_X as any).schema\"` in CLI)\n * @param schema\n * @param options - `pure: false` drops the `/* @__PURE__ * /` annotation, for\n * the one caller that emits the IIFE as a STATEMENT following the\n * declaration rather than as its initializer: there the call's whole point is\n * its side effect (`__zcMkv` installing the compiled methods on the schema),\n * and a bundler that believed it pure would drop the compilation outright.\n */\nexport function generateIIFE(\n schemaExpr: string,\n schema: CompiledSchemaInfo,\n options?: { zodCompat?: boolean | undefined; pure?: boolean | undefined },\n): string {\n const { codegenResult, refEntries } = schema;\n const fnName = extractFunctionName(codegenResult.functionDef);\n const zodCompat = options?.zodCompat !== false;\n // Every fallback access starts from the same source schema, and compact\n // delegation names it outright. Capture it once whenever either needs it, so\n // an inline initializer is not reconstructed for each path and again for the\n // identity-preserving __zcMkv target.\n const bindsSchema = refEntries.length > 0 || codegenResult.usesRetainedSchema === true;\n const retainedSchema = bindsSchema ? RETAINED_SCHEMA_VAR : schemaExpr;\n const schemaArg = zodCompat ? retainedSchema : \"null\";\n const fcArg = codegenResult.fastFnName ?? \"null\";\n // `.is()` gets the fast-check directly only when it is a total predicate;\n // partial/none falls back to safeParse().success inside __zcMkv. A rebuilding\n // schema has no by-reference `fc` but still names its predicate separately.\n const isArg = codegenResult.isFnName ?? (codegenResult.fastTotal ? fcArg : \"null\");\n\n return [\n options?.pure === false ? \"(() => {\" : \"/* @__PURE__ */ (() => {\",\n ...(bindsSchema ? [`var ${RETAINED_SCHEMA_VAR}=${schemaExpr};`] : []),\n // Only fallback refs need the array; a compact validator with none of its\n // own reads `__zs` directly rather than allocating `[__zs]` to index into.\n ...(refEntries.length > 0\n ? [`var __rf=[${refEntries.map((fb) => `${retainedSchema}${fb.accessPath}`).join(\",\")}];`]\n : []),\n ...codegenResult.code\n .split(\"\\n\")\n .filter((l) => l.trim() !== \"\" && l.trim() !== \"/* zod-compiler */\"),\n codegenResult.functionDef,\n `return __zcMkv(${fnName},${schemaArg},${fcArg},${isArg});`,\n \"})()\",\n ].join(\"\\n\");\n}\n"],"mappings":";;;;;;;;;;;;AAcA,MAAa,oBACX;;;;;;;;;;;;;;;;;;;;AAqBF,MAAa,sBACX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CF,MAAa,kBACX;;;AASF,MAAa,WACX;;;;;;;;;;;;;;;;;;;;;;;AAwBF,MAAa,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BjC,MAAa,mBACX;;AAKF,MAAa,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6FzB,MAAa,oBACX;AAIF,SAAS,oBAAoB,aAA6B;CACxD,MAAM,QAAQ,yBAAyB,KAAK,WAAW;CACvD,IAAI,CAAC,QAAQ,IACX,MAAM,IAAI,MAAM,kDAAkD;CAEpE,OAAO,MAAM;AACf;;;;;;;;;;;;;;;;;AAkBA,SAAgB,iBAAiB,QAAqC;CACpE,OAAO,OAAO,WAAW,SAAS;AACpC;;;;;;;;;;;;;AAcA,SAAgB,aACd,YACA,QACA,SACQ;CACR,MAAM,EAAE,eAAe,eAAe;CACtC,MAAM,SAAS,oBAAoB,cAAc,WAAW;CAC5D,MAAM,YAAY,SAAS,cAAc;CAKzC,MAAM,cAAc,WAAW,SAAS,KAAK,cAAc,uBAAuB;CAClF,MAAM,iBAAiB,cAAc,sBAAsB;CAC3D,MAAM,YAAY,YAAY,iBAAiB;CAC/C,MAAM,QAAQ,cAAc,cAAc;CAI1C,MAAM,QAAQ,cAAc,aAAa,cAAc,YAAY,QAAQ;CAE3E,OAAO;EACL,SAAS,SAAS,QAAQ,aAAa;EACvC,GAAI,cAAc,CAAC,OAAO,oBAAoB,GAAG,WAAW,EAAE,IAAI,CAAC;EAGnE,GAAI,WAAW,SAAS,IACpB,CAAC,aAAa,WAAW,KAAK,OAAO,GAAG,iBAAiB,GAAG,YAAY,CAAC,CAAC,KAAK,GAAG,EAAE,GAAG,IACvF,CAAC;EACL,GAAG,cAAc,KACd,MAAM,IAAI,CAAC,CACX,QAAQ,MAAM,EAAE,KAAK,MAAM,MAAM,EAAE,KAAK,MAAM,oBAAoB;EACrE,cAAc;EACd,kBAAkB,OAAO,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM;EACxD;CACF,CAAC,CAAC,KAAK,IAAI;AACb"}
1
+ {"version":3,"file":"iife.js","names":[],"sources":["../../src/core/iife.ts"],"sourcesContent":["/**\n * Shared CompiledSchema<T> IIFE generation.\n * Used by both CLI emitter and unplugin transform.\n */\n\nimport { RETAINED_SCHEMA_VAR } from \"./codegen/context.js\";\nimport type { CompiledSchemaInfo } from \"./pipeline.js\";\n\n/**\n * Import statement required by generateIIFE output (references\n * __zodCompilerConfig). `core` is bound for $ZodAsyncError: superRefine and\n * custom callbacks may only reveal that they are async through the promise\n * they return, at which point zod's own synchronous parse raises.\n */\nexport const ZOD_CONFIG_IMPORT =\n 'import { config as __zodCompilerConfig, core as __zcCore, ZodRealError as __zcZodError } from \"zod\";';\n\n/**\n * File-level `__zcMsg` declaration (must appear once after ZOD_CONFIG_IMPORT):\n * the message an issue gets when nothing was baked into it at build time.\n *\n * Resolves zod's tail of `finalizeIssue` — `config.customError` then\n * `config.localeError` then \"Invalid input\" — and does it PER CALL, because the\n * config is mutable: `z.config({ localeError })` in an entry point runs after\n * the schema modules it imports, so a value snapshotted at module init misses\n * it. Reading a captured `localeError` alone also dropped `customError`\n * outright, silently ignoring the global map most i18n setups install.\n *\n * The head of zod's chain — the schema's own `error` option — is baked into the\n * issue at build time and short-circuits this. The one link that cannot be\n * reproduced is a per-CALL `ctx.error`, which would have to travel through\n * `safeParse`; that entry point sits at V8's inlining budget, where even an\n * unused extra parameter measured ~12% on every parse.\n *\n * Only ever called while building an error, never on a successful parse.\n */\nexport const ZOD_MSG_DECLARATION =\n 'function __zcUw(m){return typeof m===\"string\"?m:(m===undefined||m===null?undefined:m.message);}' +\n \"var __zcMsg=function(iss){var c=__zodCompilerConfig(),m;\" +\n \"if(c.customError){m=__zcUw(c.customError(iss));if(m!==undefined&&m!==null)return m;}\" +\n \"if(c.localeError){m=__zcUw(c.localeError(iss));if(m!==undefined&&m!==null)return m;}\" +\n 'return \"Invalid input\";};';\n\n/**\n * Shared failure-result for __zcFin / __zcFinD. Inline mode (CLI emitter)\n * declares it once per compiled file; lean mode (all unplugin bundlers) declares\n * it once per bundle in the plugin-materialized runtime module (module-local —\n * generated code only ever references __zcFin/__zcFinD, never __ZcFail).\n *\n * Why a prototype getter and not `{success:false, get error(){...}}`: an object\n * literal with an inline accessor forces V8 down its slow accessor-defining\n * allocation path — ~110ns per failure, measured — which dominates the entire\n * invalid-input cost whenever callers never read `.error`. Hosting `error` on\n * the prototype turns each failure into a plain field-only instantiation (~2ns,\n * ~13x), with the lazy-cache semantics intact. (Trade-off: `error` is a\n * prototype accessor, so it no longer shows up in `Object.keys(result)` / spread\n * / JSON.stringify of the result wrapper — `.success`/`.error`/`.data` access,\n * destructuring, and `in` are unaffected.)\n *\n * One class serves both finalizers, so the instances share one hidden class:\n * __zcFin passes pre-collected issues in `_e` (with `_f===null`); __zcFinD\n * passes the hosted slow-walk in `_f` plus the input in `_i`, and the getter\n * runs the walk on first `.error` read. The whole finalization — locale fill\n * (__zcMsg applied ONLY when an issue carries no message, never overwriting a\n * baked-in custom/fallback message), input strip, and ZodError construction\n * (zod v4 JSON.stringifies every issue into `message` and captures a stack\n * trace) — stays deferred inside the cached accessor exactly as before, since\n * the issues array is observable solely through `.error`.\n *\n * `input` is `delete`d, not assigned `undefined`. Key PRESENCE is observable —\n * `\"input\" in issue`, `Object.keys(issue)`, object spread, `toStrictEqual`\n * against a zod issue — and zod's `util.finalizeIssue` does `delete full.input`\n * whenever `reportInput` is off, so assignment left every compiled issue one\n * enumerable key wider than zod's. The delete's dictionary-mode transition is\n * affordable precisely BECAUSE of the deferral above: it runs only after a\n * caller asks for `.error` on a failed parse, and is memoised in `_c`. Neither\n * a successful parse nor a `.success`/`.is()` check on a rejected one reaches\n * it — measured, the two are unchanged, while the `.error` read itself goes\n * ~3.95us -> ~4.19us per failure, i.e. ~6% of a path whose cost is already\n * dominated by the ZodError construction on the next line (stack capture plus a\n * JSON.stringify of every issue).\n */\nexport const FAIL_CLASS_DECL =\n \"function __ZcFail(e,f,i){this.success=false;this._e=e;this._f=f;this._i=i;this._c=undefined;}\" +\n 'Object.defineProperty(__ZcFail.prototype,\"error\",{configurable:true,get:function(){' +\n \"if(this._c)return this._c;\" +\n \"var e=this._f!==null?this._f(this._i):this._e;\" +\n 'for(var i=0;i<e.length;i++){if(e[i].message===undefined&&typeof __zcMsg===\"function\")e[i].message=__zcMsg(e[i]);delete e[i].input;delete e[i].continue;}' +\n \"return this._c=new __zcZodError(e);}});\";\n\n/** Eager finalizer (mutation / partial-fast-path schemas): issues already\n * collected in `e`; success short-circuits to a plain result literal. */\nexport const FIN_DECL =\n \"function __zcFin(e,d){if(!e.length)return{success:true,data:d};return new __ZcFail(e,null,null);}\";\n\n/**\n * Deferred-collection finalizer for Fast-Path-eligible schemas. When the\n * fast check fails, the ENTIRE slow path (the issue-collecting re-walk) is\n * pushed into the cached `.error` accessor instead of running eagerly:\n * fast-eligible schemas never mutate, so the walk's only output is the\n * issues array, which is observable solely through `.error` — one step\n * further along the same lazy boundary `__zcFin` already established (locale\n * fill, input strip, ZodError construction). A failed safeParse whose\n * `.error` is never read costs the fast check alone.\n *\n * Takes the schema's HOSTED slow-walk function plus the input — NOT a\n * per-call closure: `__zcFinD(__sw_N, input)` allocates only the result\n * object, where `__zcFinD(function(){...})` paid a closure environment and\n * function object per failure. Hosting the walk also shrinks safeParse to\n * two statements, within V8's inlining budget (the success-path result\n * literal becomes escape-analyzable at monomorphic call sites).\n *\n * The walk re-reads `input` at `.error`-read time; a caller that mutates\n * the input between safeParse and reading `.error` sees issues for the\n * mutated value (zod materializes at parse time). Same caveat class as the\n * documented __zcFin deferral.\n */\nexport const FIN_DEFERRED_DECL = \"function __zcFinD(f,inp){return new __ZcFail(null,f,inp);}\";\n\n/**\n * Compact-mode failure class — a lazy failure that delegates error reporting to\n * the ORIGINAL Zod schema's `safeParse`. Used by `output: \"compact\"`, where the\n * compiled slow walk is dropped entirely: a mutation-free schema's fast check\n * is the only generated validation, and on a fast-check failure the cold error\n * path is produced by the retained Zod schema itself (`zod` is the source of\n * truth, so the issues are byte-identical — no second validation engine).\n *\n * `_z` is the schema's PRISTINE safeParse method, captured by\n * emitRetainedMethod (see context.ts), and `_r` is its receiver — the `__zs`\n * binding generateIIFE places above that capture. Both are read before the\n * trailing `__zcMkv` call installs anything, so the method is zod's own\n * implementation, never the compiled delegate, avoiding infinite recursion\n * without allocating a bound function. The zod parse is deferred until `.error`\n * is read and cached, so\n * the common `safeParse(x).success`/`.is(x)` checks on invalid input cost only\n * the fast check (zod never runs) — the same deferral boundary `__zcFinD`\n * establishes for the compiled slow walk. Sound because compact mode is gated\n * on a TOTAL fast path: `fc(input) === false` ⟹ zod rejects, so `success:false`\n * holds without consulting zod.\n *\n * The getter returns zod's OWN ZodError verbatim (no locale fill / input strip /\n * re-wrap — zod already finalized it), so a delegated failure is exactly what\n * the unaltered schema would have produced.\n */\nexport const FAILZ_CLASS_DECL =\n \"function __ZcFailZ(z,r,i){this.success=false;this._z=z;this._r=r;this._i=i;this._c=undefined;}\" +\n 'Object.defineProperty(__ZcFailZ.prototype,\"error\",{configurable:true,get:function(){' +\n \"return this._c||(this._c=this._z.call(this._r,this._i).error);}});\";\n\n/** Compact-mode finalizer: retain a pristine safeParse, its receiver, and input lazily. */\nexport const FINZ_DECL = \"function __zcFinZ(z,r,i){return new __ZcFailZ(z,r,i);}\";\n\n/**\n * Validator factory. Inline mode (CLI emitter) declares it once per compiled\n * file; lean mode (all unplugin bundlers) exports it once per bundle from the\n * plugin-materialized runtime module — generated code never imports it from\n * the zod-compiler package itself, so zod-compiler stays a devDependency and\n * the helper set is always version-locked to the codegen that calls it.\n * Wraps a safeParse function into the CompiledSchema interface.\n *\n * IDENTITY-PRESERVING: with zodCompat (schema != null) the compiled\n * parse/safeParse/parseAsync/safeParseAsync are installed as OWN properties\n * on the original schema object, which is returned as-is. zod v4 keys\n * several APIs on object identity — toJSONSchema's ctx.seen registers the\n * object it is handed while each processor closure captures the original\n * inst (a wrapper crashes `optionalProcessor` with \"Cannot set properties\n * of undefined (setting 'ref')\" the moment a compiled schema is composed\n * into another schema), and globalRegistry/.meta() is a WeakMap keyed by\n * the schema instance (a wrapper silently loses OpenAPI titles/ids). An\n * Object.create wrapper breaks both; mutating the original breaks neither\n * (zod's internal parsing flows through _zod.run, never the public\n * methods, and derived schemas — .optional(), .extend() — are fresh\n * instances that fall back to plain zod). schema=null (zodCompat: false)\n * still produces a plain method-bag object.\n *\n * fc is the schema's hosted fast-check boolean function (null when no Fast\n * Path exists). parse()/parseAsync() try it first and return the input\n * directly on success: fc is small enough for V8 to inline, so the hot parse\n * path runs with zero allocations — calling fn would allocate an intermediate\n * SafeParseResult that escape analysis cannot remove (fn never inlines).\n * Fast-path-eligible schemas never mutate, so fc(input) ⟹ data === input.\n *\n * is is the TOTAL fast-check predicate (fc when the fast path is total, else\n * null). Installed as `.is()` — a zero-allocation boolean type guard. When\n * null (partial fast path or none) `.is()` derives from fn(input).success: a\n * partial fc can pass-through valid input but its `false` does not imply\n * rejection (a default/catch may still succeed), so it would be unsound as a\n * standalone guard.\n *\n * parseAsync/safeParseAsync wrap the SYNC validator, which is right for every\n * schema the compiler can reproduce — none of them are async. It is wrong for\n * the ones it cannot: an `async` refinement or a `z.promise()` extracts to a\n * Zod delegate, and delegating means calling Zod's SYNCHRONOUS safeParse, which\n * raises `$ZodAsyncError` by design. Wrapping that gave the compiled schema an\n * async pair that rejected with `$ZodAsyncError` for EVERY input — including\n * valid ones — where Zod resolves normally, so `await UserSchema.parseAsync(x)`\n * stopped working the moment any part of the schema went async.\n *\n * So both are guarded: a synchronous throw hands off to the schema's ORIGINAL\n * async method, captured before these are installed — the same escape hatch\n * `~standard` already uses for its throw path, and for the same reason (the\n * compiled validator has no async mode to offer, and Zod's is exact). It also\n * fixes the smaller wart that a throwing `fn` made `parseAsync` throw\n * SYNCHRONOUSLY rather than return a rejected promise. With `zodCompat: false`\n * there is no schema to delegate to and the throw propagates as before.\n *\n * `~standard` is REPLACED, not merely preserved. Zod builds it lazily as\n * `validate: (v) => safeParse(inst, v)` — the core FUNCTION, which goes straight\n * to `inst._zod.run`. It never reads the schema's own `safeParse` property, so\n * installing the compiled one leaves this route entirely uncompiled: measured\n * 271.7 ns against the compiled 26.6 ns on the same schema, i.e. Standard Schema\n * consumers (tRPC, Hono, TanStack) were getting plain Zod.\n *\n * Zod's own `~standard` is never READ, only overwritten. Zod installs the slot\n * with `util.defineLazy` — commented there as \"avoid creating objects for every\n * schema\" — so it is an accessor that builds `{version, vendor, validate}` plus\n * its closure on first touch. Reading it to capture a fallback fired that getter\n * for every compiled schema while the module was still initializing.\n *\n * How much that costs depends on the entry point, and only one of them is free:\n * classic `zod` forces the slot itself during `ZodType.init` (it does\n * `Object.assign(inst[\"~standard\"], { jsonSchema })`), so there the read hit an\n * already-built object and cost only an accessor call. `zod/mini` and raw\n * `zod/v4/core` never touch it, so for those the read built — and retained — an\n * object and a closure per schema, purely to capture a fallback the schema will\n * most likely never expose to a Standard Schema consumer.\n *\n * The throw path is rebuilt instead of captured. Zod's validate catches a\n * synchronous throw and retries through `safeParseAsync` — that is how an async\n * refinement resolves and how a throwing check surfaces as a rejected promise\n * rather than a synchronous throw — so calling the already-captured `zspa` and\n * mapping its result is the same route to the same result. `vendor` is likewise\n * a constant: schema discovery only ever admits zod schemas, and classic, mini\n * and core all hardcode `vendor: \"zod\"` themselves.\n *\n * Not carried over (unchanged by this, and pre-dating it): classic's\n * `~standard.jsonSchema` extension, which the replacement object has never\n * reproduced.\n *\n * Installed with defineProperty rather than assignment: Zod's lazy setter\n * redefines the slot as non-writable, so a second `__zcMkv` on the same schema\n * object — two exports aliasing one schema — would throw under ESM strict mode.\n *\n * A rebuilding schema (no `fc`) reaches `parse()` through `fn`, i.e. through a\n * SafeParseResult unwrapped a line later. Handing the build function over so\n * `parse()` could call it directly was measured and declined: the wrapper is a\n * young-generation bump allocation that V8 elides outright where `fn` inlines,\n * and even across eight schemas sharing this closure — where it cannot — the\n * direct call came out level (41.5 vs 41.8 ns), for a wider signature on every\n * bundle.\n */\nexport const MK_VALIDATOR_DECL =\n \"function __zcMkv(fn,schema,fc,is){var w=schema||{};var zpa=w.parseAsync,zspa=w.safeParseAsync;w.parse=fc?function(input){if(fc(input))return input;var r=fn(input);if(r.success)return r.data;throw r.error;}:function(input){var r=fn(input);if(r.success)return r.data;throw r.error;};w.safeParse=fn;w.safeParseAsync=function(input){try{return Promise.resolve(fn(input));}catch(e){if(zspa)return zspa(input);throw e;}};w.parseAsync=fc?function(input){try{if(fc(input))return Promise.resolve(input);var r=fn(input);if(r.success)return Promise.resolve(r.data);return Promise.reject(r.error);}catch(e){if(zpa)return zpa(input);throw e;}}:function(input){try{var r=fn(input);if(r.success)return Promise.resolve(r.data);return Promise.reject(r.error);}catch(e){if(zpa)return zpa(input);throw e;}};w.is=is||function(input){return fn(input).success;};\" +\n 'Object.defineProperty(w,\"~standard\",{configurable:true,value:{version:1,vendor:\"zod\",validate:function(input){var r;try{if(fc&&fc(input))return{value:input};r=fn(input);}catch(e){if(zspa)return zspa(input).then(function(q){return q.success?{value:q.data}:{issues:q.error.issues};});throw e;}return r.success?{value:r.data}:{issues:r.error.issues};}}});' +\n \"return w;}\";\n\nfunction extractFunctionName(functionDef: string): string {\n const match = /^function\\s+(\\w+)\\s*\\(/.exec(functionDef);\n if (!match?.[1]) {\n throw new Error(\"Cannot extract function name from generated code\");\n }\n return match[1];\n}\n\n/**\n * Does the IIFE's preamble DEREFERENCE the retained schema at evaluation time?\n *\n * Only `__rf` does: every entry is `__zs<accessPath>`, and an access path walks\n * the schema's structure — `._zod.innerType` fires a `z.lazy()` getter,\n * `.shape` materializes a `z.object()` shape (zod v4 reads every property\n * descriptor, so ONE `.shape` read fires ALL of an object's recursion getters).\n * Binding `__zs` itself does not: constructing the schema leaves deferred\n * callbacks unforced, and `usesRetainedSchema`'s only read is `__zs.safeParse`,\n * a prototype getter that binds the method and installs the bound copy on the\n * instance without touching the schema's structure.\n *\n * The distinction decides whether the IIFE may be evaluated inside the\n * INITIALIZER of the binding the schema's own deferred callbacks close over —\n * see the self-referential path in the unplugin's autoDiscover rewrite.\n */\nexport function iifeDerefsSchema(schema: CompiledSchemaInfo): boolean {\n return schema.refEntries.length > 0;\n}\n\n/**\n * Generate a `/* @__PURE__ * /` IIFE wrapping a compiled validator.\n *\n * @param schemaExpr - Expression resolving to the original Zod schema\n * (e.g. `\"UserSchema\"` in unplugin, `\"(__src_X as any).schema\"` in CLI)\n * @param schema\n * @param options - `pure: false` drops the `/* @__PURE__ * /` annotation, for\n * the one caller that emits the IIFE as a STATEMENT following the\n * declaration rather than as its initializer: there the call's whole point is\n * its side effect (`__zcMkv` installing the compiled methods on the schema),\n * and a bundler that believed it pure would drop the compilation outright.\n */\nexport function generateIIFE(\n schemaExpr: string,\n schema: CompiledSchemaInfo,\n options?: { zodCompat?: boolean | undefined; pure?: boolean | undefined },\n): string {\n const { codegenResult, refEntries } = schema;\n const fnName = extractFunctionName(codegenResult.functionDef);\n const zodCompat = options?.zodCompat !== false;\n // Every fallback access starts from the same source schema, and compact\n // delegation names it outright. Capture it once whenever either needs it, so\n // an inline initializer is not reconstructed for each path and again for the\n // identity-preserving __zcMkv target.\n const bindsSchema = refEntries.length > 0 || codegenResult.usesRetainedSchema === true;\n const retainedSchema = bindsSchema ? RETAINED_SCHEMA_VAR : schemaExpr;\n const schemaArg = zodCompat ? retainedSchema : \"null\";\n const fcArg = codegenResult.fastFnName ?? \"null\";\n // `.is()` gets the fast-check directly only when it is a total predicate;\n // partial/none falls back to safeParse().success inside __zcMkv. A rebuilding\n // schema has no by-reference `fc` but still names its predicate separately.\n const isArg = codegenResult.isFnName ?? (codegenResult.fastTotal ? fcArg : \"null\");\n\n return [\n options?.pure === false ? \"(() => {\" : \"/* @__PURE__ */ (() => {\",\n ...(bindsSchema ? [`var ${RETAINED_SCHEMA_VAR}=${schemaExpr};`] : []),\n // Only fallback refs need the array; a compact validator with none of its\n // own reads `__zs` directly rather than allocating `[__zs]` to index into.\n ...(refEntries.length > 0\n ? [`var __rf=[${refEntries.map((fb) => `${retainedSchema}${fb.accessPath}`).join(\",\")}];`]\n : []),\n ...codegenResult.code\n .split(\"\\n\")\n .filter((l) => l.trim() !== \"\" && l.trim() !== \"/* zod-compiler */\"),\n codegenResult.functionDef,\n `return __zcMkv(${fnName},${schemaArg},${fcArg},${isArg});`,\n \"})()\",\n ].join(\"\\n\");\n}\n"],"mappings":";;;;;;;;;;;;AAcA,MAAa,oBACX;;;;;;;;;;;;;;;;;;;;AAqBF,MAAa,sBACX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CF,MAAa,kBACX;;;AASF,MAAa,WACX;;;;;;;;;;;;;;;;;;;;;;;AAwBF,MAAa,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BjC,MAAa,mBACX;;AAKF,MAAa,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqGzB,MAAa,oBACX;AAIF,SAAS,oBAAoB,aAA6B;CACxD,MAAM,QAAQ,yBAAyB,KAAK,WAAW;CACvD,IAAI,CAAC,QAAQ,IACX,MAAM,IAAI,MAAM,kDAAkD;CAEpE,OAAO,MAAM;AACf;;;;;;;;;;;;;;;;;AAkBA,SAAgB,iBAAiB,QAAqC;CACpE,OAAO,OAAO,WAAW,SAAS;AACpC;;;;;;;;;;;;;AAcA,SAAgB,aACd,YACA,QACA,SACQ;CACR,MAAM,EAAE,eAAe,eAAe;CACtC,MAAM,SAAS,oBAAoB,cAAc,WAAW;CAC5D,MAAM,YAAY,SAAS,cAAc;CAKzC,MAAM,cAAc,WAAW,SAAS,KAAK,cAAc,uBAAuB;CAClF,MAAM,iBAAiB,cAAc,sBAAsB;CAC3D,MAAM,YAAY,YAAY,iBAAiB;CAC/C,MAAM,QAAQ,cAAc,cAAc;CAI1C,MAAM,QAAQ,cAAc,aAAa,cAAc,YAAY,QAAQ;CAE3E,OAAO;EACL,SAAS,SAAS,QAAQ,aAAa;EACvC,GAAI,cAAc,CAAC,OAAO,oBAAoB,GAAG,WAAW,EAAE,IAAI,CAAC;EAGnE,GAAI,WAAW,SAAS,IACpB,CAAC,aAAa,WAAW,KAAK,OAAO,GAAG,iBAAiB,GAAG,YAAY,CAAC,CAAC,KAAK,GAAG,EAAE,GAAG,IACvF,CAAC;EACL,GAAG,cAAc,KACd,MAAM,IAAI,CAAC,CACX,QAAQ,MAAM,EAAE,KAAK,MAAM,MAAM,EAAE,KAAK,MAAM,oBAAoB;EACrE,cAAc;EACd,kBAAkB,OAAO,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM;EACxD;CACF,CAAC,CAAC,KAAK,IAAI;AACb"}
package/dist/jit.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"jit.d.ts","names":[],"sources":["../src/jit.ts"],"mappings":";;;UAoFiB;;;;;;;EAOf;;EAEA;;;;;;;;;;;;;;;;;;;;;;;iBAwBc,IAAI,UAAU,SAC5B,QAAQ,GACR,UAAU,aACT,IAAI,eAAe,OAAO;;;;;;;;;;;;;;iBAoNb,OAAO,iBAAiB,UAAU"}
1
+ {"version":3,"file":"jit.d.ts","names":[],"sources":["../src/jit.ts"],"mappings":";;;UA2FiB;;;;;;;EAOf;;EAEA;;;;;;;;;;;;;;;;;;;;;;;iBAwBc,IAAI,UAAU,SAC5B,QAAQ,GACR,UAAU,aACT,IAAI,eAAe,OAAO;;;;;;;;;;;;;;iBAiOb,OAAO,iBAAiB,UAAU"}
package/dist/jit.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { isZodSchema } from "./is-zod-schema.js";
2
2
  import { FAILZ_CLASS_DECL, FAIL_CLASS_DECL, FINZ_DECL, FIN_DECL, FIN_DEFERRED_DECL, MK_VALIDATOR_DECL, ZOD_MSG_DECLARATION, generateIIFE } from "./core/iife.js";
3
+ import { isSupportedZodVersion, isUnsupportedZodVersionError, unsupportedZodVersionMessage, warnUnsupportedZodOnce, zodVersionOf } from "./core/extract/zod-version.js";
3
4
  import { compileSchemas } from "./core/pipeline.js";
4
5
  import { ZodRealError, config, core } from "zod";
5
6
  //#region src/jit.ts
@@ -99,6 +100,11 @@ const seen = /* @__PURE__ */ new WeakSet();
99
100
  function jit(schema, options) {
100
101
  const target = schema;
101
102
  if (seen.has(target)) return schema;
103
+ const version = zodVersionOf(schema);
104
+ if (isZodSchema(schema) && !isSupportedZodVersion(version)) {
105
+ warnUnsupportedZodOnce(unsupportedZodVersionMessage(version));
106
+ return schema;
107
+ }
102
108
  seen.add(target);
103
109
  if (options?.eager === true) {
104
110
  materialize(schema, options);
@@ -258,7 +264,9 @@ function materialize(schema, options) {
258
264
  if (!codegenAllowed()) return;
259
265
  try {
260
266
  buildValidator(schema, options);
261
- } catch {}
267
+ } catch (error) {
268
+ if (isUnsupportedZodVersionError(error)) warnUnsupportedZodOnce(error.message);
269
+ }
262
270
  }
263
271
  /**
264
272
  * Generate the validator and evaluate it, reproducing the module a
package/dist/jit.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"jit.js","names":["zodCore","zodConfig"],"sources":["../src/jit.ts"],"sourcesContent":["/**\n * Runtime compilation — the same extract → codegen pipeline the build plugin\n * runs, executed in-process and evaluated through `new Function`.\n *\n * The AOT paths (unplugin, CLI) need a build step to fire. Plenty of everyday\n * code has none: `tsx server.ts`, `node --experimental-strip-types`, a Jest\n * suite, a serverless handler bundled by someone else's toolchain, a library\n * that ships schemas to consumers. There `compile()` is a no-op and every parse\n * runs plain Zod. `jit()` closes that gap — one call, no build integration,\n * measured 3-25x on everyday schemas at ~0.1-0.2 ms of one-time compilation.\n *\n * Nothing here re-implements validation: {@link compileSchemas} and\n * {@link generateIIFE} are the exact modules the plugin and CLI use, so the\n * generated validator, its Zod parity and its performance are identical to what\n * a build would have emitted. The only difference is *when* the code is\n * produced.\n *\n * Compilation is LAZY by default: `jit()` installs accessors that compile on\n * the first read of a parse method and replace themselves with the compiled\n * ones. Importing a module of 500 schemas therefore costs nothing, and a\n * serverless invocation touching three of them pays for three.\n *\n * Zod itself keeps the parse methods on the PROTOTYPE (since 4.4): each is a\n * getter that binds the method on first read and installs the bound copy as an\n * own data property, so a fresh schema carries no own `parse` / `safeParse` at\n * all, while one that has been read already does. `jit()` meets both shapes —\n * it snapshots whatever the slots hold, fronts them with its own accessors, and\n * hands back exactly what it found whenever it has to step aside.\n *\n * Runtime code generation is not always permitted — a strict CSP without\n * `unsafe-eval`, some edge runtimes. Zod v4 has the same constraint (its object\n * fast-pass is itself a `new Function`) and already exposes the two switches\n * for it: the `core.util.allowsEval` probe and `z.config({ jitless: true })`.\n * `jit()` honours both and degrades to plain Zod, so one setting governs both\n * compilers. Those targets are where the build plugin belongs anyway — it emits\n * the same validator with no runtime evaluation at all.\n */\n\nimport { config as zodConfig, core as zodCore, ZodRealError, type output, type ZodType } from \"zod\";\nimport {\n FAIL_CLASS_DECL,\n FAILZ_CLASS_DECL,\n FIN_DECL,\n FIN_DEFERRED_DECL,\n FINZ_DECL,\n generateIIFE,\n MK_VALIDATOR_DECL,\n ZOD_MSG_DECLARATION,\n} from \"./core/iife.js\";\nimport { compileSchemas } from \"./core/pipeline.js\";\nimport type { CompiledSchema } from \"./core/types.js\";\nimport { isZodSchema } from \"./is-zod-schema.js\";\n\n/**\n * The declarations `ZOD_CONFIG_IMPORT` supplies to an emitted module, minus the\n * import itself — `zod`'s three bindings arrive as parameters instead, so the\n * evaluated code has no module scope to resolve. Byte-for-byte the same helper\n * source the CLI emitter writes into a `.compiled.ts`, so a JIT validator and\n * an AOT one share their entire runtime layer.\n */\nconst RUNTIME_PRELUDE = [\n ZOD_MSG_DECLARATION,\n FAIL_CLASS_DECL,\n MK_VALIDATOR_DECL,\n FIN_DECL,\n FIN_DEFERRED_DECL,\n FAILZ_CLASS_DECL,\n FINZ_DECL,\n].join(\"\\n\");\n\n/**\n * Methods `__zcMkv` installs. Each is fronted by a compile-on-read accessor\n * until the schema materializes.\n *\n * `~standard` earns its place: Zod builds it as a closure over `_zod.run`, not\n * over the schema's `safeParse` property, so a Standard Schema consumer (tRPC,\n * Hono, TanStack Form) that never touches `safeParse` would otherwise keep\n * running plain Zod forever behind a \"compiled\" schema.\n */\nconst SLOTS = [\"parse\", \"safeParse\", \"parseAsync\", \"safeParseAsync\", \"is\", \"~standard\"] as const;\n\n/** Schemas already handed to `jit()`, so a second call is a no-op rather than a recompile. */\nconst seen = new WeakSet<object>();\n\nexport interface JitOptions {\n /**\n * Compile immediately instead of on first use. Costs ~0.1-0.2 ms per schema\n * at import time; useful for a long-lived server that would rather pay during\n * startup than on the first request, or to surface a compilation failure\n * eagerly. Default `false`.\n */\n eager?: boolean | undefined;\n /** Use compact codegen and delegate cold error production to Zod. @default \"schema\" */\n output?: \"schema\" | \"compact\" | undefined;\n}\n\n/**\n * Compile `schema` in-process and install the compiled `parse` / `safeParse` /\n * `parseAsync` / `safeParseAsync` / `is` / `~standard` on it.\n *\n * Returns the SAME object — identity-preserving exactly as the build plugin is,\n * so `.shape`, `_zod`, `instanceof`, `z.toJSONSchema()`, `.meta()` and\n * composition into a larger schema all keep working, and every existing\n * reference to the schema picks the compiled methods up.\n *\n * ```ts\n * import { z } from \"zod\";\n * import { jit } from \"zod-compiler/jit\";\n *\n * export const UserSchema = jit(z.object({ name: z.string(), email: z.email() }));\n * UserSchema.safeParse(input); // compiled on this first call\n * ```\n *\n * Schemas the compiler cannot reproduce fall back to Zod per sub-schema, the\n * same way they do at build time; a schema that cannot be compiled at all is\n * left as plain Zod.\n */\nexport function jit<T extends ZodType>(\n schema: T,\n options?: JitOptions,\n): T & CompiledSchema<output<T>> {\n const target = schema as unknown as Record<string, unknown>;\n if (seen.has(target)) return schema as T & CompiledSchema<output<T>>;\n seen.add(target);\n\n if (options?.eager === true) {\n materialize(schema, options);\n return schema as T & CompiledSchema<output<T>>;\n }\n\n // Snapshot what each slot holds before anything is installed: materialize()\n // restores it before handing the object to `__zcMkv`, so the generated code\n // sees a pristine schema — it captures the original `parseAsync` /\n // `safeParseAsync` as its throw paths, and capturing a stub there would loop\n // back into itself. An untouched schema holds NOTHING here: Zod keeps the\n // methods on the prototype and only installs a bound own copy on first read,\n // so `undefined` is the common entry and means \"back to the prototype\". A\n // schema that was read before `jit()` — or that the build plugin's `__zcMkv`\n // already materialized — carries the copies, and those go back verbatim.\n const original = new Map<string, PropertyDescriptor | undefined>();\n try {\n for (const slot of SLOTS) {\n original.set(slot, Object.getOwnPropertyDescriptor(target, slot));\n }\n } catch {\n // The target answers a descriptor query with a throw — an exotic wrapper,\n // not anything Zod built. Without a snapshot there is nothing to roll back\n // to, so install nothing and hand back the schema exactly as it came.\n return schema as T & CompiledSchema<output<T>>;\n }\n\n // Installing the accessors is the step most likely to throw rather than\n // degrade: a slot locked non-configurable (a future Zod, another wrapper) makes\n // defineProperty raise, and `jit()` is called at module scope — so an\n // unhandled throw here takes down the importing app at boot. Roll back to\n // whatever Zod had and leave the schema alone instead.\n let pending = true;\n try {\n installAccessors(\n target,\n original,\n () => {\n if (!pending) return;\n pending = false;\n restore(target, original);\n materialize(schema, options);\n },\n () => {\n if (!pending) return;\n pending = false;\n // Restore EVERY slot, not just the one being written. A left-behind\n // accessor whose trigger has been cancelled would read `target[slot]`\n // and re-enter itself — unbounded recursion, which is what a later read\n // of an untouched slot (`~standard`, from a Standard Schema consumer)\n // would otherwise hit.\n //\n // Reached only when something WRITES a slot before anything reads one: a\n // test double, another wrapper, or an AOT `safeParse` assigned directly.\n // The build plugin's own `__zcMkv` does not land here — its first\n // statement READS `parseAsync`/`safeParseAsync` to capture their\n // originals, so it triggers materialization and then overwrites the\n // compiled-by-jit methods with the compiled-by-plugin ones.\n restore(target, original);\n },\n );\n } catch {\n pending = false;\n restore(target, original);\n }\n\n return schema as T & CompiledSchema<output<T>>;\n}\n\n/**\n * Front every installed method with a compile-on-read accessor. `trigger`\n * materializes the schema, which normally replaces these accessors with the\n * compiled methods (or restores what Zod had). When that replacement does not\n * take, the getter serves Zod's own method rather than re-reading the slot —\n * see the re-entrancy note in the body.\n */\nfunction installAccessors(\n target: Record<string, unknown>,\n original: ReadonlyMap<string, PropertyDescriptor | undefined>,\n trigger: () => void,\n cancel: () => void,\n): void {\n for (const slot of SLOTS) {\n // Re-entrancy is settled structurally rather than by inspection. Reading the\n // slot again is how this getter normally hands over — to the compiled method\n // materialize() installed, or to whatever restore() put back — but the\n // handover can fail to take: a target frozen after `jit()` refuses both, and\n // a second copy of this module in the graph leaves ITS accessor on the slot,\n // so the two bounce reads between them. `trigger()` is spent by then, so\n // either way the read recurses until the stack blows. While a read is already\n // in flight, or once the accessor is known to be stranded, serve Zod's own\n // method instead: a schema that cannot be compiled still parses.\n let reading = false;\n // A stranded accessor is permanent, so what it serves is resolved once.\n let stranded: { value: unknown } | undefined;\n const serveZod = (): unknown => {\n stranded ??= { value: fromZod(target, original, slot) };\n return stranded.value;\n };\n const read = function (): unknown {\n if (reading) return serveZod();\n reading = true;\n try {\n trigger();\n if (stillFrontedBy(target, slot, read)) return serveZod();\n return target[slot];\n } finally {\n reading = false;\n }\n };\n Object.defineProperty(target, slot, {\n configurable: true,\n // Keep the schema's own-key set as it was. A slot Zod already materialized\n // is an enumerable own copy, so the accessor fronting it is enumerable too;\n // an untouched slot has no own key at all, and `is` never exists on a Zod\n // schema, so those stay out of `Object.keys` — the non-enumerable\n // convention `compile()` already uses.\n enumerable: original.get(slot)?.enumerable ?? false,\n get: read,\n set(value: unknown) {\n // Someone overwrote a method before first use (a test double, another\n // wrapper). Their value wins, and compilation is cancelled outright —\n // materializing later would restore the snapshot over it. With the\n // accessor gone the write does what it would have without `jit()`:\n // overwrite the own copy, or run Zod's prototype setter, which installs\n // one. Only a target that refused the rollback still routes here, and a\n // direct define is the one way left to honour the write.\n cancel();\n if (!stillFrontedBy(target, slot, read)) {\n target[slot] = value;\n return;\n }\n Object.defineProperty(target, slot, {\n configurable: true,\n enumerable: original.get(slot)?.enumerable ?? false,\n value,\n writable: true,\n });\n },\n });\n }\n}\n\n/**\n * Is `slot` still fronted by this very accessor — i.e. did the replacement that\n * `trigger()` was supposed to perform not take? A target that will not answer\n * the question is assumed to still hold it, since reading the slot to find out\n * is the recursion being avoided.\n */\nfunction stillFrontedBy(\n target: Record<string, unknown>,\n slot: string,\n getter: () => unknown,\n): boolean {\n try {\n return Object.getOwnPropertyDescriptor(target, slot)?.get === getter;\n } catch {\n return true;\n }\n}\n\n/**\n * Zod's own value for `slot`, bypassing the accessor that fronts it.\n *\n * A slot the snapshot holds is answered from the snapshot. One it does not hold\n * lives on the prototype, where Zod's getter binds the method to its receiver\n * and installs the bound copy as an own property — on a target that accepts\n * the install, that also replaces the stranded accessor with the copy, which is\n * the handover materialize() could not make. A target that refuses the install\n * (frozen, or a wrapper that vetoes `defineProperty`) makes Zod's getter throw\n * instead; there the read goes through a stand-in that inherits from the target,\n * so the copy lands on the stand-in and the method reaches `_zod` through the\n * chain. `undefined` for `is`, which no plain Zod schema carries — the same\n * thing every other degradation path leaves there.\n */\nfunction fromZod(\n target: Record<string, unknown>,\n original: ReadonlyMap<string, PropertyDescriptor | undefined>,\n slot: string,\n): unknown {\n const descriptor = original.get(slot);\n if (descriptor !== undefined) {\n // A snapshot can hold another accessor (a second copy of this module\n // installed first), so invoke it rather than reading a `value` it lacks.\n return descriptor.get === undefined ? descriptor.value : descriptor.get.call(target);\n }\n const proto = Object.getPrototypeOf(target) as object | null;\n if (proto === null) return undefined;\n try {\n return Reflect.get(proto, slot, target);\n } catch {\n return Reflect.get(proto, slot, Object.create(target));\n }\n}\n\n/**\n * Compile every Zod schema found among an object's own values — typically a\n * module namespace, so a whole schema file opts in with one call:\n *\n * ```ts\n * import * as schemas from \"./schemas.js\";\n * jitAll(schemas);\n * ```\n *\n * The namespace object itself is never written to (a module namespace is\n * read-only); `jit()` mutates the schema objects it holds, which is what every\n * importer of that module already references.\n */\nexport function jitAll(schemas: object, options?: JitOptions): void {\n for (const value of Object.values(schemas)) {\n if (isZodSchema(value)) jit(value, options);\n }\n}\n\n/**\n * Put back what the snapshot holds, dropping the compile-on-read accessors: the\n * own copy Zod (or the build plugin) had installed, or nothing at all, so the\n * slot reads through to Zod's prototype getter again.\n */\nfunction restore(\n target: Record<string, unknown>,\n original: ReadonlyMap<string, PropertyDescriptor | undefined>,\n): void {\n for (const slot of SLOTS) {\n // Per slot, because this also runs as the rollback for a failed install: a\n // target that refuses one slot must not cost the others their restoration.\n // A slot left fronted by its accessor still reads correctly — the getter\n // serves Zod's own method — but it keeps a redundant indirection, so\n // restoring what can be restored is worth the try/catch.\n try {\n const descriptor = original.get(slot);\n if (descriptor === undefined) delete target[slot];\n else Object.defineProperty(target, slot, descriptor);\n } catch {\n // Nothing further to try for this slot.\n }\n }\n}\n\n/**\n * Whether runtime code generation is permitted here. Read per call, never\n * snapshotted: `z.config({ jitless: true })` runs in an entry point, after the\n * schema modules it imports have already been evaluated.\n */\nfunction codegenAllowed(): boolean {\n return zodCore.globalConfig.jitless !== true && zodCore.util.allowsEval.value;\n}\n\n/**\n * Run the pipeline and let the generated IIFE install its methods on `schema`.\n * Swallows failure: a schema that cannot be compiled keeps Zod's own methods,\n * which the caller already has, so there is nothing to report and nothing to\n * break.\n */\nfunction materialize(schema: unknown, options?: JitOptions): void {\n if (!codegenAllowed()) return;\n try {\n buildValidator(schema, options);\n } catch {\n // Left as plain Zod. Deliberately silent: `jit()` is an optimization, and a\n // schema using a construct the compiler declines is a supported outcome,\n // not an error.\n }\n}\n\n/**\n * Generate the validator and evaluate it, reproducing the module a\n * `.compiled.ts` would have been: helper preamble, the file-level shared block,\n * then the `__zcMkv` IIFE whose `__rf[]` bases and install target are the live\n * schema object passed in as `__schema`.\n */\nfunction buildValidator(schema: unknown, options?: JitOptions): void {\n const { schemas, shared } = compileSchemas([{ exportName: \"jit\", schema }], {\n compact: options?.output === \"compact\",\n mode: \"inline\",\n });\n const compiled = schemas[0];\n if (compiled === undefined) throw new Error(\"zod-compiler: schema produced no validator\");\n\n const body = [RUNTIME_PRELUDE, shared.code, `return ${generateIIFE(\"__schema\", compiled)};`].join(\n \"\\n\",\n );\n\n // The three bindings ZOD_CONFIG_IMPORT would have imported, passed in so the\n // evaluated code needs no module resolution of its own.\n // oxlint-disable-next-line no-new-func -- generating the validator IS the feature\n const factory = new Function(\n \"__zodCompilerConfig\",\n \"__zcCore\",\n \"__zcZodError\",\n \"__schema\",\n body,\n ) as (\n zodConfigFn: typeof zodConfig,\n zodCoreNs: typeof zodCore,\n zodErrorCtor: typeof ZodRealError,\n target: unknown,\n ) => unknown;\n\n factory(zodConfig, zodCore, ZodRealError, schema);\n}\n"],"mappings":"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"}
1
+ {"version":3,"file":"jit.js","names":["zodCore","zodConfig"],"sources":["../src/jit.ts"],"sourcesContent":["/**\n * Runtime compilation — the same extract → codegen pipeline the build plugin\n * runs, executed in-process and evaluated through `new Function`.\n *\n * The AOT paths (unplugin, CLI) need a build step to fire. Plenty of everyday\n * code has none: `tsx server.ts`, `node --experimental-strip-types`, a Jest\n * suite, a serverless handler bundled by someone else's toolchain, a library\n * that ships schemas to consumers. There `compile()` is a no-op and every parse\n * runs plain Zod. `jit()` closes that gap — one call, no build integration,\n * measured 3-25x on everyday schemas at ~0.1-0.2 ms of one-time compilation.\n *\n * Nothing here re-implements validation: {@link compileSchemas} and\n * {@link generateIIFE} are the exact modules the plugin and CLI use, so the\n * generated validator, its Zod parity and its performance are identical to what\n * a build would have emitted. The only difference is *when* the code is\n * produced.\n *\n * Compilation is LAZY by default: `jit()` installs accessors that compile on\n * the first read of a parse method and replace themselves with the compiled\n * ones. Importing a module of 500 schemas therefore costs nothing, and a\n * serverless invocation touching three of them pays for three.\n *\n * Zod itself keeps the parse methods on the PROTOTYPE (since 4.4): each is a\n * getter that binds the method on first read and installs the bound copy as an\n * own data property, so a fresh schema carries no own `parse` / `safeParse` at\n * all, while one that has been read already does. `jit()` meets both shapes —\n * it snapshots whatever the slots hold, fronts them with its own accessors, and\n * hands back exactly what it found whenever it has to step aside.\n *\n * Runtime code generation is not always permitted — a strict CSP without\n * `unsafe-eval`, some edge runtimes. Zod v4 has the same constraint (its object\n * fast-pass is itself a `new Function`) and already exposes the two switches\n * for it: the `core.util.allowsEval` probe and `z.config({ jitless: true })`.\n * `jit()` honours both and degrades to plain Zod, so one setting governs both\n * compilers. Those targets are where the build plugin belongs anyway — it emits\n * the same validator with no runtime evaluation at all.\n */\n\nimport { config as zodConfig, core as zodCore, ZodRealError, type output, type ZodType } from \"zod\";\nimport {\n FAIL_CLASS_DECL,\n FAILZ_CLASS_DECL,\n FIN_DECL,\n FIN_DEFERRED_DECL,\n FINZ_DECL,\n generateIIFE,\n MK_VALIDATOR_DECL,\n ZOD_MSG_DECLARATION,\n} from \"./core/iife.js\";\nimport {\n isSupportedZodVersion,\n isUnsupportedZodVersionError,\n unsupportedZodVersionMessage,\n warnUnsupportedZodOnce,\n zodVersionOf,\n} from \"./core/extract/zod-version.js\";\nimport { compileSchemas } from \"./core/pipeline.js\";\nimport type { CompiledSchema } from \"./core/types.js\";\nimport { isZodSchema } from \"./is-zod-schema.js\";\n\n/**\n * The declarations `ZOD_CONFIG_IMPORT` supplies to an emitted module, minus the\n * import itself — `zod`'s three bindings arrive as parameters instead, so the\n * evaluated code has no module scope to resolve. Byte-for-byte the same helper\n * source the CLI emitter writes into a `.compiled.ts`, so a JIT validator and\n * an AOT one share their entire runtime layer.\n */\nconst RUNTIME_PRELUDE = [\n ZOD_MSG_DECLARATION,\n FAIL_CLASS_DECL,\n MK_VALIDATOR_DECL,\n FIN_DECL,\n FIN_DEFERRED_DECL,\n FAILZ_CLASS_DECL,\n FINZ_DECL,\n].join(\"\\n\");\n\n/**\n * Methods `__zcMkv` installs. Each is fronted by a compile-on-read accessor\n * until the schema materializes.\n *\n * `~standard` earns its place: Zod builds it as a closure over `_zod.run`, not\n * over the schema's `safeParse` property, so a Standard Schema consumer (tRPC,\n * Hono, TanStack Form) that never touches `safeParse` would otherwise keep\n * running plain Zod forever behind a \"compiled\" schema.\n */\nconst SLOTS = [\"parse\", \"safeParse\", \"parseAsync\", \"safeParseAsync\", \"is\", \"~standard\"] as const;\n\n/** Schemas already handed to `jit()`, so a second call is a no-op rather than a recompile. */\nconst seen = new WeakSet<object>();\n\nexport interface JitOptions {\n /**\n * Compile immediately instead of on first use. Costs ~0.1-0.2 ms per schema\n * at import time; useful for a long-lived server that would rather pay during\n * startup than on the first request, or to surface a compilation failure\n * eagerly. Default `false`.\n */\n eager?: boolean | undefined;\n /** Use compact codegen and delegate cold error production to Zod. @default \"schema\" */\n output?: \"schema\" | \"compact\" | undefined;\n}\n\n/**\n * Compile `schema` in-process and install the compiled `parse` / `safeParse` /\n * `parseAsync` / `safeParseAsync` / `is` / `~standard` on it.\n *\n * Returns the SAME object — identity-preserving exactly as the build plugin is,\n * so `.shape`, `_zod`, `instanceof`, `z.toJSONSchema()`, `.meta()` and\n * composition into a larger schema all keep working, and every existing\n * reference to the schema picks the compiled methods up.\n *\n * ```ts\n * import { z } from \"zod\";\n * import { jit } from \"zod-compiler/jit\";\n *\n * export const UserSchema = jit(z.object({ name: z.string(), email: z.email() }));\n * UserSchema.safeParse(input); // compiled on this first call\n * ```\n *\n * Schemas the compiler cannot reproduce fall back to Zod per sub-schema, the\n * same way they do at build time; a schema that cannot be compiled at all is\n * left as plain Zod.\n */\nexport function jit<T extends ZodType>(\n schema: T,\n options?: JitOptions,\n): T & CompiledSchema<output<T>> {\n const target = schema as unknown as Record<string, unknown>;\n if (seen.has(target)) return schema as T & CompiledSchema<output<T>>;\n\n // A schema built by a zod this release does not reproduce is left as plain\n // Zod — correct, just not faster — and said so once (see zod-version.ts).\n // Not thrown: `jit()` runs at module scope, where a throw takes the importing\n // app down at boot over what is a dependency-range problem. Checked before\n // anything is installed, so neither the eager path nor a later\n // compile-on-read accessor can reach the pipeline for it. A value that is not\n // a zod schema at all is not a version problem and takes the usual route.\n const version = zodVersionOf(schema);\n if (isZodSchema(schema) && !isSupportedZodVersion(version)) {\n warnUnsupportedZodOnce(unsupportedZodVersionMessage(version));\n return schema as T & CompiledSchema<output<T>>;\n }\n seen.add(target);\n\n if (options?.eager === true) {\n materialize(schema, options);\n return schema as T & CompiledSchema<output<T>>;\n }\n\n // Snapshot what each slot holds before anything is installed: materialize()\n // restores it before handing the object to `__zcMkv`, so the generated code\n // sees a pristine schema — it captures the original `parseAsync` /\n // `safeParseAsync` as its throw paths, and capturing a stub there would loop\n // back into itself. An untouched schema holds NOTHING here: Zod keeps the\n // methods on the prototype and only installs a bound own copy on first read,\n // so `undefined` is the common entry and means \"back to the prototype\". A\n // schema that was read before `jit()` — or that the build plugin's `__zcMkv`\n // already materialized — carries the copies, and those go back verbatim.\n const original = new Map<string, PropertyDescriptor | undefined>();\n try {\n for (const slot of SLOTS) {\n original.set(slot, Object.getOwnPropertyDescriptor(target, slot));\n }\n } catch {\n // The target answers a descriptor query with a throw — an exotic wrapper,\n // not anything Zod built. Without a snapshot there is nothing to roll back\n // to, so install nothing and hand back the schema exactly as it came.\n return schema as T & CompiledSchema<output<T>>;\n }\n\n // Installing the accessors is the step most likely to throw rather than\n // degrade: a slot locked non-configurable (a future Zod, another wrapper) makes\n // defineProperty raise, and `jit()` is called at module scope — so an\n // unhandled throw here takes down the importing app at boot. Roll back to\n // whatever Zod had and leave the schema alone instead.\n let pending = true;\n try {\n installAccessors(\n target,\n original,\n () => {\n if (!pending) return;\n pending = false;\n restore(target, original);\n materialize(schema, options);\n },\n () => {\n if (!pending) return;\n pending = false;\n // Restore EVERY slot, not just the one being written. A left-behind\n // accessor whose trigger has been cancelled would read `target[slot]`\n // and re-enter itself — unbounded recursion, which is what a later read\n // of an untouched slot (`~standard`, from a Standard Schema consumer)\n // would otherwise hit.\n //\n // Reached only when something WRITES a slot before anything reads one: a\n // test double, another wrapper, or an AOT `safeParse` assigned directly.\n // The build plugin's own `__zcMkv` does not land here — its first\n // statement READS `parseAsync`/`safeParseAsync` to capture their\n // originals, so it triggers materialization and then overwrites the\n // compiled-by-jit methods with the compiled-by-plugin ones.\n restore(target, original);\n },\n );\n } catch {\n pending = false;\n restore(target, original);\n }\n\n return schema as T & CompiledSchema<output<T>>;\n}\n\n/**\n * Front every installed method with a compile-on-read accessor. `trigger`\n * materializes the schema, which normally replaces these accessors with the\n * compiled methods (or restores what Zod had). When that replacement does not\n * take, the getter serves Zod's own method rather than re-reading the slot —\n * see the re-entrancy note in the body.\n */\nfunction installAccessors(\n target: Record<string, unknown>,\n original: ReadonlyMap<string, PropertyDescriptor | undefined>,\n trigger: () => void,\n cancel: () => void,\n): void {\n for (const slot of SLOTS) {\n // Re-entrancy is settled structurally rather than by inspection. Reading the\n // slot again is how this getter normally hands over — to the compiled method\n // materialize() installed, or to whatever restore() put back — but the\n // handover can fail to take: a target frozen after `jit()` refuses both, and\n // a second copy of this module in the graph leaves ITS accessor on the slot,\n // so the two bounce reads between them. `trigger()` is spent by then, so\n // either way the read recurses until the stack blows. While a read is already\n // in flight, or once the accessor is known to be stranded, serve Zod's own\n // method instead: a schema that cannot be compiled still parses.\n let reading = false;\n // A stranded accessor is permanent, so what it serves is resolved once.\n let stranded: { value: unknown } | undefined;\n const serveZod = (): unknown => {\n stranded ??= { value: fromZod(target, original, slot) };\n return stranded.value;\n };\n const read = function (): unknown {\n if (reading) return serveZod();\n reading = true;\n try {\n trigger();\n if (stillFrontedBy(target, slot, read)) return serveZod();\n return target[slot];\n } finally {\n reading = false;\n }\n };\n Object.defineProperty(target, slot, {\n configurable: true,\n // Keep the schema's own-key set as it was. A slot Zod already materialized\n // is an enumerable own copy, so the accessor fronting it is enumerable too;\n // an untouched slot has no own key at all, and `is` never exists on a Zod\n // schema, so those stay out of `Object.keys` — the non-enumerable\n // convention `compile()` already uses.\n enumerable: original.get(slot)?.enumerable ?? false,\n get: read,\n set(value: unknown) {\n // Someone overwrote a method before first use (a test double, another\n // wrapper). Their value wins, and compilation is cancelled outright —\n // materializing later would restore the snapshot over it. With the\n // accessor gone the write does what it would have without `jit()`:\n // overwrite the own copy, or run Zod's prototype setter, which installs\n // one. Only a target that refused the rollback still routes here, and a\n // direct define is the one way left to honour the write.\n cancel();\n if (!stillFrontedBy(target, slot, read)) {\n target[slot] = value;\n return;\n }\n Object.defineProperty(target, slot, {\n configurable: true,\n enumerable: original.get(slot)?.enumerable ?? false,\n value,\n writable: true,\n });\n },\n });\n }\n}\n\n/**\n * Is `slot` still fronted by this very accessor — i.e. did the replacement that\n * `trigger()` was supposed to perform not take? A target that will not answer\n * the question is assumed to still hold it, since reading the slot to find out\n * is the recursion being avoided.\n */\nfunction stillFrontedBy(\n target: Record<string, unknown>,\n slot: string,\n getter: () => unknown,\n): boolean {\n try {\n return Object.getOwnPropertyDescriptor(target, slot)?.get === getter;\n } catch {\n return true;\n }\n}\n\n/**\n * Zod's own value for `slot`, bypassing the accessor that fronts it.\n *\n * A slot the snapshot holds is answered from the snapshot. One it does not hold\n * lives on the prototype, where Zod's getter binds the method to its receiver\n * and installs the bound copy as an own property — on a target that accepts\n * the install, that also replaces the stranded accessor with the copy, which is\n * the handover materialize() could not make. A target that refuses the install\n * (frozen, or a wrapper that vetoes `defineProperty`) makes Zod's getter throw\n * instead; there the read goes through a stand-in that inherits from the target,\n * so the copy lands on the stand-in and the method reaches `_zod` through the\n * chain. `undefined` for `is`, which no plain Zod schema carries — the same\n * thing every other degradation path leaves there.\n */\nfunction fromZod(\n target: Record<string, unknown>,\n original: ReadonlyMap<string, PropertyDescriptor | undefined>,\n slot: string,\n): unknown {\n const descriptor = original.get(slot);\n if (descriptor !== undefined) {\n // A snapshot can hold another accessor (a second copy of this module\n // installed first), so invoke it rather than reading a `value` it lacks.\n return descriptor.get === undefined ? descriptor.value : descriptor.get.call(target);\n }\n const proto = Object.getPrototypeOf(target) as object | null;\n if (proto === null) return undefined;\n try {\n return Reflect.get(proto, slot, target);\n } catch {\n return Reflect.get(proto, slot, Object.create(target));\n }\n}\n\n/**\n * Compile every Zod schema found among an object's own values — typically a\n * module namespace, so a whole schema file opts in with one call:\n *\n * ```ts\n * import * as schemas from \"./schemas.js\";\n * jitAll(schemas);\n * ```\n *\n * The namespace object itself is never written to (a module namespace is\n * read-only); `jit()` mutates the schema objects it holds, which is what every\n * importer of that module already references.\n */\nexport function jitAll(schemas: object, options?: JitOptions): void {\n for (const value of Object.values(schemas)) {\n if (isZodSchema(value)) jit(value, options);\n }\n}\n\n/**\n * Put back what the snapshot holds, dropping the compile-on-read accessors: the\n * own copy Zod (or the build plugin) had installed, or nothing at all, so the\n * slot reads through to Zod's prototype getter again.\n */\nfunction restore(\n target: Record<string, unknown>,\n original: ReadonlyMap<string, PropertyDescriptor | undefined>,\n): void {\n for (const slot of SLOTS) {\n // Per slot, because this also runs as the rollback for a failed install: a\n // target that refuses one slot must not cost the others their restoration.\n // A slot left fronted by its accessor still reads correctly — the getter\n // serves Zod's own method — but it keeps a redundant indirection, so\n // restoring what can be restored is worth the try/catch.\n try {\n const descriptor = original.get(slot);\n if (descriptor === undefined) delete target[slot];\n else Object.defineProperty(target, slot, descriptor);\n } catch {\n // Nothing further to try for this slot.\n }\n }\n}\n\n/**\n * Whether runtime code generation is permitted here. Read per call, never\n * snapshotted: `z.config({ jitless: true })` runs in an entry point, after the\n * schema modules it imports have already been evaluated.\n */\nfunction codegenAllowed(): boolean {\n return zodCore.globalConfig.jitless !== true && zodCore.util.allowsEval.value;\n}\n\n/**\n * Run the pipeline and let the generated IIFE install its methods on `schema`.\n * Swallows failure: a schema that cannot be compiled keeps Zod's own methods,\n * which the caller already has, so there is nothing to report and nothing to\n * break.\n */\nfunction materialize(schema: unknown, options?: JitOptions): void {\n if (!codegenAllowed()) return;\n try {\n buildValidator(schema, options);\n } catch (error) {\n // Left as plain Zod. Deliberately silent: `jit()` is an optimization, and a\n // schema using a construct the compiler declines is a supported outcome,\n // not an error. The one exception is the zod version guard — a mismatched\n // zod is a configuration problem the user has to hear about. `jit()` itself\n // refuses such a schema before installing anything; this covers a caller\n // that reached the pipeline another way.\n if (isUnsupportedZodVersionError(error)) warnUnsupportedZodOnce(error.message);\n }\n}\n\n/**\n * Generate the validator and evaluate it, reproducing the module a\n * `.compiled.ts` would have been: helper preamble, the file-level shared block,\n * then the `__zcMkv` IIFE whose `__rf[]` bases and install target are the live\n * schema object passed in as `__schema`.\n */\nfunction buildValidator(schema: unknown, options?: JitOptions): void {\n const { schemas, shared } = compileSchemas([{ exportName: \"jit\", schema }], {\n compact: options?.output === \"compact\",\n mode: \"inline\",\n });\n const compiled = schemas[0];\n if (compiled === undefined) throw new Error(\"zod-compiler: schema produced no validator\");\n\n const body = [RUNTIME_PRELUDE, shared.code, `return ${generateIIFE(\"__schema\", compiled)};`].join(\n \"\\n\",\n );\n\n // The three bindings ZOD_CONFIG_IMPORT would have imported, passed in so the\n // evaluated code needs no module resolution of its own.\n // oxlint-disable-next-line no-new-func -- generating the validator IS the feature\n const factory = new Function(\n \"__zodCompilerConfig\",\n \"__zcCore\",\n \"__zcZodError\",\n \"__schema\",\n body,\n ) as (\n zodConfigFn: typeof zodConfig,\n zodCoreNs: typeof zodCore,\n zodErrorCtor: typeof ZodRealError,\n target: unknown,\n ) => unknown;\n\n factory(zodConfig, zodCore, ZodRealError, schema);\n}\n"],"mappings":"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"}
package/dist/runtime.d.ts CHANGED
@@ -30,6 +30,9 @@ export declare const __zcPfx: any;
30
30
  export declare const __zcCu: any;
31
31
  export declare const __zcSr: any;
32
32
  export declare const __zcSrOk: any;
33
+ export declare const __zcAsy: any;
34
+ export declare const __zcRd: any;
35
+ export declare const __zcRf: any;
33
36
  export declare const __zcReEmail: any;
34
37
  export declare const __zcReEmailSrc: any;
35
38
  export declare const __zcReUuid: any;
package/dist/runtime.js CHANGED
@@ -30,8 +30,11 @@ export function __zcPs(o){if(!Object.prototype.hasOwnProperty.call(o,"__proto__"
30
30
  export function __zcPlain(o){if(typeof o!=="object"||o===null||Array.isArray(o))return false;var c=o.constructor;if(c===Object||c===undefined||typeof c!=="function")return true;var p=c.prototype;if(typeof p!=="object"||p===null||Array.isArray(p))return false;return Object.prototype.hasOwnProperty.call(p,"isPrototypeOf");}
31
31
  export function __zcPfx(d,s,b,k){for(var i=0;i<s.length;i++){var x=s[i];x.path=b.concat(k,x.path);d.push(x);}}
32
32
  export function __zcCu(f,v){var r=f(v);if(r&&typeof r.then==="function"){throw new __zcCore.$ZodAsyncError();}return !!r;}
33
- export function __zcSr(f,v,p,e,m){var q={value:v,issues:[]};__zcSrRun(f,q);for(var i=0;i<q.issues.length;i++){var s=q.issues[i],t={};for(var k in s){if(k!=="inst"&&k!=="continue")t[k]=s[k];}if(s.continue!==true)q.aborted=true;t.path=s.path&&s.path.length?p.concat(s.path):p;if(t.message===undefined&&m!==undefined)t.message=m;e.push(t);}return q;}
33
+ export function __zcSr(f,v,p,e,m){var q={value:v,issues:[]};__zcSrRun(f,q);for(var i=0;i<q.issues.length;i++){var s=q.issues[i],t={};for(var k in s){if(k!=="inst"&&k!=="continue")t[k]=s[k];}if(s.continue!==true){q.aborted=true;t.continue=false;}t.path=s.path&&s.path.length?p.concat(s.path):p;if(t.message===undefined&&m!==undefined)t.message=m;e.push(t);}return q;}
34
34
  export function __zcSrOk(f,v){var p={value:v,issues:[]};__zcSrRun(f,p);return p.issues.length===0&&p.value===v;}
35
+ export function __zcAsy(){throw new __zcCore.$ZodAsyncError();}
36
+ export function __zcRd(z,v,c){var r=z.run({value:v,issues:[]},c);if(r instanceof Promise)throw new __zcCore.$ZodAsyncError();return r;}
37
+ export function __zcRf(s,c,e,p){var g=__zcCore.config();for(var i=0;i<s.length;i++){var q=s[i],f=__zcCore.util.finalizeIssue(q,c,g);f.path=p.concat(f.path);if(q.continue!==true)f.continue=false;e.push(f);}}
35
38
  export const __zcReEmail=new RegExp("^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$");
36
39
  export const __zcReEmailSrc="/^(?!\\.)(?!.*\\.\\.)([A-Za-z0-9_'+\\-\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$/";
37
40
  export const __zcReUuid=new RegExp("^([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$");
@@ -1 +1 @@
1
- {"version":3,"file":"hoist-compile.d.ts","names":[],"sources":["../../src/unplugin/hoist-compile.ts"],"mappings":";;;;;UA8CiB;;EAEf;;EAEA;;EAEA,MAAM;;;;;;iBAyFc,sBACpB,kBAAkB,iBAClB,cACA,YACA,MAAM,cACL,QAAQ"}
1
+ {"version":3,"file":"hoist-compile.d.ts","names":[],"sources":["../../src/unplugin/hoist-compile.ts"],"mappings":";;;;;UAkDiB;;EAEf;;EAEA;;EAEA,MAAM;;;;;;iBA6Fc,sBACpB,kBAAkB,iBAClB,cACA,YACA,MAAM,cACL,QAAQ"}
@@ -1,5 +1,6 @@
1
1
  import { isZodSchema } from "../is-zod-schema.js";
2
2
  import { loadModule } from "../loader.js";
3
+ import { isUnsupportedZodVersionError, warnUnsupportedZodOnce } from "../core/extract/zod-version.js";
3
4
  import { generateValidator } from "../core/codegen/index.js";
4
5
  import { extractSchema } from "../core/extract/index.js";
5
6
  import { analyzeHoistedExpression, collectImportBindings } from "./hoist.js";
@@ -50,7 +51,8 @@ async function compileOne(schema, importDetails, id, mode, moduleCache) {
50
51
  refEntries
51
52
  }
52
53
  };
53
- } catch {
54
+ } catch (error) {
55
+ if (isUnsupportedZodVersionError(error)) warnUnsupportedZodOnce(error.message);
54
56
  return null;
55
57
  }
56
58
  }
@@ -1 +1 @@
1
- {"version":3,"file":"hoist-compile.js","names":[],"sources":["../../src/unplugin/hoist-compile.ts"],"sourcesContent":["/**\n * Build-time compilation of hoisted schemas.\n *\n * After hoistZodSchemasMeta() lifts `z.object({...})` constructions to\n * module-scope `_zh_*` declarations, this step evaluates each hoisted\n * expression with the project's real zod module, runs the regular\n * extract → codegen pipeline on the resulting schema object, and hands the\n * transform a compiled IIFE to splice in as the declaration initializer:\n *\n * const _zh_x = z.object({ id: z.number() });\n * ⇣\n * const _zh_x = /* @__PURE__ *\\/ (() => { ... return __zcMkv(...); })();\n *\n * Eligibility is STRICTER than hoist eligibility. Hoisting only moves an\n * expression; compiling it bakes build-time evaluation results into\n * generated checks, so the construction must be deterministic:\n *\n * - Every EAGER free identifier must be a zod-package binding. Anything\n * else (other imports: `getLimit()`, globals: `new Date()`,\n * `Math.random()`) could evaluate differently at build time vs module\n * load — those schemas stay plainly hoisted.\n * - DEFERRED references (inside refine/transform/default callbacks) are\n * unrestricted: callbacks reach generated code via fn.toString() or stay\n * on the runtime-constructed schema (`__rf` delegation), never via their\n * build-time closure values. If extraction itself needs a deferred value\n * it cannot have (z.lazy(() => ImportedChild)), evaluation throws and the\n * schema falls back to a plain hoist.\n *\n * Every failure path is graceful: the declaration keeps its original zod\n * expression and runtime behavior is unchanged.\n */\n\nimport type { CodegenMode } from \"../core/codegen/context.js\";\nimport { generateValidator } from \"../core/codegen/index.js\";\nimport { extractSchema, type RefEntry } from \"../core/extract/index.js\";\nimport type { CompiledSchemaInfo } from \"../core/pipeline.js\";\nimport { isZodSchema } from \"../is-zod-schema.js\";\nimport { loadModule } from \"../loader.js\";\nimport {\n analyzeHoistedExpression,\n collectImportBindings,\n type HoistedSchema,\n type ImportDetail,\n} from \"./hoist.js\";\n\n/** A hoisted declaration whose initializer can be replaced with a compiled IIFE. */\nexport interface CompiledHoistedSchema {\n /** The `_zh_*` declaration name. */\n name: string;\n /** Original construction expression text (becomes the IIFE's schema expression). */\n text: string;\n /** Compiled validator for the schema. */\n info: CompiledSchemaInfo;\n}\n\n/**\n * Evaluate a hoisted expression with its zod bindings and compile it.\n * Returns null when the schema is ineligible or anything fails.\n */\nasync function compileOne(\n schema: HoistedSchema,\n importDetails: Map<string, ImportDetail>,\n id: string,\n mode: CodegenMode,\n moduleCache: Map<string, Promise<Record<string, unknown>>>,\n): Promise<CompiledHoistedSchema | null> {\n const analysis = analyzeHoistedExpression(schema.text);\n if (analysis === null) return null;\n\n // Determinism gate: eager evaluation may only touch zod bindings.\n const free = new Set([...analysis.eagerFree, ...analysis.deferredFree]);\n const bindings: Array<{ name: string; detail: ImportDetail }> = [];\n for (const name of analysis.eagerFree) {\n const detail = importDetails.get(name);\n if (!detail || !isZodSpecifier(detail.specifier)) return null;\n }\n // Inject every free import binding we can resolve (eager ones are all zod\n // by the gate above; deferred ones are best-effort — extraction only\n // dereferences them for build-time-invoked callbacks like z.lazy getters).\n for (const name of free) {\n const detail = importDetails.get(name);\n if (detail && isZodSpecifier(detail.specifier)) {\n bindings.push({ name, detail });\n } else if (analysis.eagerFree.has(name)) {\n return null;\n }\n // deferred non-zod names stay unbound: the evaluated closure would throw\n // if invoked at build time, which the try/catch below converts to a skip.\n }\n\n try {\n const values = await Promise.all(\n bindings.map(async ({ detail }) => {\n let loading = moduleCache.get(detail.specifier);\n if (!loading) {\n loading = loadModule(detail.specifier, id);\n moduleCache.set(detail.specifier, loading);\n }\n const mod = await loading;\n return detail.imported === \"*\" ? mod : mod[detail.imported];\n }),\n );\n\n const evaluate = new Function(\n ...bindings.map((b) => b.name),\n `\"use strict\"; return (${schema.text});`,\n );\n const value: unknown = evaluate(...values);\n if (!isZodSchema(value)) return null;\n\n const refEntries: RefEntry[] = [];\n const ir = extractSchema(value, refEntries);\n // A root fallback compiles to a pure delegation wrapper — strictly worse\n // than leaving the plain hoisted construction in place.\n if (ir.type === \"fallback\") return null;\n\n const codegenResult = generateValidator(ir, schema.name, { mode });\n return {\n name: schema.name,\n text: schema.text,\n info: { exportName: schema.name, codegenResult, refEntries },\n };\n } catch {\n return null;\n }\n}\n\nfunction isZodSpecifier(specifier: string): boolean {\n return (\n specifier === \"zod\" ||\n specifier === \"zod/v4\" ||\n specifier === \"zod/mini\" ||\n specifier === \"zod/v4/mini\" ||\n specifier === \"zod/v4-mini\"\n );\n}\n\n/**\n * Compile every eligible hoisted schema. Failures are silent per schema —\n * the caller leaves ineligible declarations as plain hoists.\n */\nexport async function compileHoistedSchemas(\n schemas: readonly HoistedSchema[],\n code: string,\n id: string,\n mode: CodegenMode,\n): Promise<CompiledHoistedSchema[]> {\n const { details } = collectImportBindings(code);\n const moduleCache = new Map<string, Promise<Record<string, unknown>>>();\n const compiled: CompiledHoistedSchema[] = [];\n for (const schema of schemas) {\n const result = await compileOne(schema, details, id, mode, moduleCache);\n if (result !== null) compiled.push(result);\n }\n return compiled;\n}\n"],"mappings":";;;;;;;;;;AA2DA,eAAe,WACb,QACA,eACA,IACA,MACA,aACuC;CACvC,MAAM,WAAW,yBAAyB,OAAO,IAAI;CACrD,IAAI,aAAa,MAAM,OAAO;CAG9B,MAAM,uBAAO,IAAI,IAAI,CAAC,GAAG,SAAS,WAAW,GAAG,SAAS,YAAY,CAAC;CACtE,MAAM,WAA0D,CAAC;CACjE,KAAK,MAAM,QAAQ,SAAS,WAAW;EACrC,MAAM,SAAS,cAAc,IAAI,IAAI;EACrC,IAAI,CAAC,UAAU,CAAC,eAAe,OAAO,SAAS,GAAG,OAAO;CAC3D;CAIA,KAAK,MAAM,QAAQ,MAAM;EACvB,MAAM,SAAS,cAAc,IAAI,IAAI;EACrC,IAAI,UAAU,eAAe,OAAO,SAAS,GAC3C,SAAS,KAAK;GAAE;GAAM;EAAO,CAAC;OACzB,IAAI,SAAS,UAAU,IAAI,IAAI,GACpC,OAAO;CAIX;CAEA,IAAI;EACF,MAAM,SAAS,MAAM,QAAQ,IAC3B,SAAS,IAAI,OAAO,EAAE,aAAa;GACjC,IAAI,UAAU,YAAY,IAAI,OAAO,SAAS;GAC9C,IAAI,CAAC,SAAS;IACZ,UAAU,WAAW,OAAO,WAAW,EAAE;IACzC,YAAY,IAAI,OAAO,WAAW,OAAO;GAC3C;GACA,MAAM,MAAM,MAAM;GAClB,OAAO,OAAO,aAAa,MAAM,MAAM,IAAI,OAAO;EACpD,CAAC,CACH;EAMA,MAAM,QAAiB,IAJF,SACnB,GAAG,SAAS,KAAK,MAAM,EAAE,IAAI,GAC7B,yBAAyB,OAAO,KAAK,GAET,CAAC,CAAC,GAAG,MAAM;EACzC,IAAI,CAAC,YAAY,KAAK,GAAG,OAAO;EAEhC,MAAM,aAAyB,CAAC;EAChC,MAAM,KAAK,cAAc,OAAO,UAAU;EAG1C,IAAI,GAAG,SAAS,YAAY,OAAO;EAEnC,MAAM,gBAAgB,kBAAkB,IAAI,OAAO,MAAM,EAAE,KAAK,CAAC;EACjE,OAAO;GACL,MAAM,OAAO;GACb,MAAM,OAAO;GACb,MAAM;IAAE,YAAY,OAAO;IAAM;IAAe;GAAW;EAC7D;CACF,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAS,eAAe,WAA4B;CAClD,OACE,cAAc,SACd,cAAc,YACd,cAAc,cACd,cAAc,iBACd,cAAc;AAElB;;;;;AAMA,eAAsB,sBACpB,SACA,MACA,IACA,MACkC;CAClC,MAAM,EAAE,YAAY,sBAAsB,IAAI;CAC9C,MAAM,8BAAc,IAAI,IAA8C;CACtE,MAAM,WAAoC,CAAC;CAC3C,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,SAAS,MAAM,WAAW,QAAQ,SAAS,IAAI,MAAM,WAAW;EACtE,IAAI,WAAW,MAAM,SAAS,KAAK,MAAM;CAC3C;CACA,OAAO;AACT"}
1
+ {"version":3,"file":"hoist-compile.js","names":[],"sources":["../../src/unplugin/hoist-compile.ts"],"sourcesContent":["/**\n * Build-time compilation of hoisted schemas.\n *\n * After hoistZodSchemasMeta() lifts `z.object({...})` constructions to\n * module-scope `_zh_*` declarations, this step evaluates each hoisted\n * expression with the project's real zod module, runs the regular\n * extract → codegen pipeline on the resulting schema object, and hands the\n * transform a compiled IIFE to splice in as the declaration initializer:\n *\n * const _zh_x = z.object({ id: z.number() });\n * ⇣\n * const _zh_x = /* @__PURE__ *\\/ (() => { ... return __zcMkv(...); })();\n *\n * Eligibility is STRICTER than hoist eligibility. Hoisting only moves an\n * expression; compiling it bakes build-time evaluation results into\n * generated checks, so the construction must be deterministic:\n *\n * - Every EAGER free identifier must be a zod-package binding. Anything\n * else (other imports: `getLimit()`, globals: `new Date()`,\n * `Math.random()`) could evaluate differently at build time vs module\n * load — those schemas stay plainly hoisted.\n * - DEFERRED references (inside refine/transform/default callbacks) are\n * unrestricted: callbacks reach generated code via fn.toString() or stay\n * on the runtime-constructed schema (`__rf` delegation), never via their\n * build-time closure values. If extraction itself needs a deferred value\n * it cannot have (z.lazy(() => ImportedChild)), evaluation throws and the\n * schema falls back to a plain hoist.\n *\n * Every failure path is graceful: the declaration keeps its original zod\n * expression and runtime behavior is unchanged.\n */\n\nimport type { CodegenMode } from \"../core/codegen/context.js\";\nimport { generateValidator } from \"../core/codegen/index.js\";\nimport { extractSchema, type RefEntry } from \"../core/extract/index.js\";\nimport {\n isUnsupportedZodVersionError,\n warnUnsupportedZodOnce,\n} from \"../core/extract/zod-version.js\";\nimport type { CompiledSchemaInfo } from \"../core/pipeline.js\";\nimport { isZodSchema } from \"../is-zod-schema.js\";\nimport { loadModule } from \"../loader.js\";\nimport {\n analyzeHoistedExpression,\n collectImportBindings,\n type HoistedSchema,\n type ImportDetail,\n} from \"./hoist.js\";\n\n/** A hoisted declaration whose initializer can be replaced with a compiled IIFE. */\nexport interface CompiledHoistedSchema {\n /** The `_zh_*` declaration name. */\n name: string;\n /** Original construction expression text (becomes the IIFE's schema expression). */\n text: string;\n /** Compiled validator for the schema. */\n info: CompiledSchemaInfo;\n}\n\n/**\n * Evaluate a hoisted expression with its zod bindings and compile it.\n * Returns null when the schema is ineligible or anything fails.\n */\nasync function compileOne(\n schema: HoistedSchema,\n importDetails: Map<string, ImportDetail>,\n id: string,\n mode: CodegenMode,\n moduleCache: Map<string, Promise<Record<string, unknown>>>,\n): Promise<CompiledHoistedSchema | null> {\n const analysis = analyzeHoistedExpression(schema.text);\n if (analysis === null) return null;\n\n // Determinism gate: eager evaluation may only touch zod bindings.\n const free = new Set([...analysis.eagerFree, ...analysis.deferredFree]);\n const bindings: Array<{ name: string; detail: ImportDetail }> = [];\n for (const name of analysis.eagerFree) {\n const detail = importDetails.get(name);\n if (!detail || !isZodSpecifier(detail.specifier)) return null;\n }\n // Inject every free import binding we can resolve (eager ones are all zod\n // by the gate above; deferred ones are best-effort — extraction only\n // dereferences them for build-time-invoked callbacks like z.lazy getters).\n for (const name of free) {\n const detail = importDetails.get(name);\n if (detail && isZodSpecifier(detail.specifier)) {\n bindings.push({ name, detail });\n } else if (analysis.eagerFree.has(name)) {\n return null;\n }\n // deferred non-zod names stay unbound: the evaluated closure would throw\n // if invoked at build time, which the try/catch below converts to a skip.\n }\n\n try {\n const values = await Promise.all(\n bindings.map(async ({ detail }) => {\n let loading = moduleCache.get(detail.specifier);\n if (!loading) {\n loading = loadModule(detail.specifier, id);\n moduleCache.set(detail.specifier, loading);\n }\n const mod = await loading;\n return detail.imported === \"*\" ? mod : mod[detail.imported];\n }),\n );\n\n const evaluate = new Function(\n ...bindings.map((b) => b.name),\n `\"use strict\"; return (${schema.text});`,\n );\n const value: unknown = evaluate(...values);\n if (!isZodSchema(value)) return null;\n\n const refEntries: RefEntry[] = [];\n const ir = extractSchema(value, refEntries);\n // A root fallback compiles to a pure delegation wrapper — strictly worse\n // than leaving the plain hoisted construction in place.\n if (ir.type === \"fallback\") return null;\n\n const codegenResult = generateValidator(ir, schema.name, { mode });\n return {\n name: schema.name,\n text: schema.text,\n info: { exportName: schema.name, codegenResult, refEntries },\n };\n } catch (error) {\n // Anything the hoisted expression cannot do at build time is a skip, not a\n // failure — except the zod version guard, a dependency-range problem the\n // build has to report (once: every schema trips it the same way).\n if (isUnsupportedZodVersionError(error)) warnUnsupportedZodOnce(error.message);\n return null;\n }\n}\n\nfunction isZodSpecifier(specifier: string): boolean {\n return (\n specifier === \"zod\" ||\n specifier === \"zod/v4\" ||\n specifier === \"zod/mini\" ||\n specifier === \"zod/v4/mini\" ||\n specifier === \"zod/v4-mini\"\n );\n}\n\n/**\n * Compile every eligible hoisted schema. Failures are silent per schema —\n * the caller leaves ineligible declarations as plain hoists.\n */\nexport async function compileHoistedSchemas(\n schemas: readonly HoistedSchema[],\n code: string,\n id: string,\n mode: CodegenMode,\n): Promise<CompiledHoistedSchema[]> {\n const { details } = collectImportBindings(code);\n const moduleCache = new Map<string, Promise<Record<string, unknown>>>();\n const compiled: CompiledHoistedSchema[] = [];\n for (const schema of schemas) {\n const result = await compileOne(schema, details, id, mode, moduleCache);\n if (result !== null) compiled.push(result);\n }\n return compiled;\n}\n"],"mappings":";;;;;;;;;;;AA+DA,eAAe,WACb,QACA,eACA,IACA,MACA,aACuC;CACvC,MAAM,WAAW,yBAAyB,OAAO,IAAI;CACrD,IAAI,aAAa,MAAM,OAAO;CAG9B,MAAM,uBAAO,IAAI,IAAI,CAAC,GAAG,SAAS,WAAW,GAAG,SAAS,YAAY,CAAC;CACtE,MAAM,WAA0D,CAAC;CACjE,KAAK,MAAM,QAAQ,SAAS,WAAW;EACrC,MAAM,SAAS,cAAc,IAAI,IAAI;EACrC,IAAI,CAAC,UAAU,CAAC,eAAe,OAAO,SAAS,GAAG,OAAO;CAC3D;CAIA,KAAK,MAAM,QAAQ,MAAM;EACvB,MAAM,SAAS,cAAc,IAAI,IAAI;EACrC,IAAI,UAAU,eAAe,OAAO,SAAS,GAC3C,SAAS,KAAK;GAAE;GAAM;EAAO,CAAC;OACzB,IAAI,SAAS,UAAU,IAAI,IAAI,GACpC,OAAO;CAIX;CAEA,IAAI;EACF,MAAM,SAAS,MAAM,QAAQ,IAC3B,SAAS,IAAI,OAAO,EAAE,aAAa;GACjC,IAAI,UAAU,YAAY,IAAI,OAAO,SAAS;GAC9C,IAAI,CAAC,SAAS;IACZ,UAAU,WAAW,OAAO,WAAW,EAAE;IACzC,YAAY,IAAI,OAAO,WAAW,OAAO;GAC3C;GACA,MAAM,MAAM,MAAM;GAClB,OAAO,OAAO,aAAa,MAAM,MAAM,IAAI,OAAO;EACpD,CAAC,CACH;EAMA,MAAM,QAAiB,IAJF,SACnB,GAAG,SAAS,KAAK,MAAM,EAAE,IAAI,GAC7B,yBAAyB,OAAO,KAAK,GAET,CAAC,CAAC,GAAG,MAAM;EACzC,IAAI,CAAC,YAAY,KAAK,GAAG,OAAO;EAEhC,MAAM,aAAyB,CAAC;EAChC,MAAM,KAAK,cAAc,OAAO,UAAU;EAG1C,IAAI,GAAG,SAAS,YAAY,OAAO;EAEnC,MAAM,gBAAgB,kBAAkB,IAAI,OAAO,MAAM,EAAE,KAAK,CAAC;EACjE,OAAO;GACL,MAAM,OAAO;GACb,MAAM,OAAO;GACb,MAAM;IAAE,YAAY,OAAO;IAAM;IAAe;GAAW;EAC7D;CACF,SAAS,OAAO;EAId,IAAI,6BAA6B,KAAK,GAAG,uBAAuB,MAAM,OAAO;EAC7E,OAAO;CACT;AACF;AAEA,SAAS,eAAe,WAA4B;CAClD,OACE,cAAc,SACd,cAAc,YACd,cAAc,cACd,cAAc,iBACd,cAAc;AAElB;;;;;AAMA,eAAsB,sBACpB,SACA,MACA,IACA,MACkC;CAClC,MAAM,EAAE,YAAY,sBAAsB,IAAI;CAC9C,MAAM,8BAAc,IAAI,IAA8C;CACtE,MAAM,WAAoC,CAAC;CAC3C,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,SAAS,MAAM,WAAW,QAAQ,SAAS,IAAI,MAAM,WAAW;EACtE,IAAI,WAAW,MAAM,SAAS,KAAK,MAAM;CAC3C;CACA,OAAO;AACT"}
@@ -1 +1 @@
1
- {"version":3,"file":"transform.d.ts","names":[],"sources":["../../src/unplugin/transform.ts"],"mappings":";;;;UA8BiB;EACf;EACA;EACA;EACA;EACA;EACA;;;;;;;;;;cAsHW,wBAAsB;;;;iBAgDnB,gBAAgB,YAAY,UAAU;;;;;;;;;;;;;cA6BzC;EAAuB,SAAS;EAAU,SAAS;;;;;;;;iBAgChD,oBAAoB,UAAU,2BAA2B;iBAOzD,IAAI;iBAKJ,KAAK;UAKJ;EACf;EACA,KAAK;;;;;;;iBAQe,cACpB,cACA,YACA,SAAS,mBACR;;;;;;iBAUmB,qBACpB,cACA,YACA,SAAS,mBACR,QAAQ;;;;iBAqXK,cACd,cACA,SAAS,sBACT;EAAY;;;;;;iBAiDE,kBAAkB,cAAc;;;;;iBAgBhC,0BACd,cACA,SAAS,sBACT;EAAY;;;;;;iBAgGE,oBAAoB"}
1
+ {"version":3,"file":"transform.d.ts","names":[],"sources":["../../src/unplugin/transform.ts"],"mappings":";;;;UAkCiB;EACf;EACA;EACA;EACA;EACA;EACA;;;;;;;;;;cAsHW,wBAAsB;;;;iBAgDnB,gBAAgB,YAAY,UAAU;;;;;;;;;;;;;cA6BzC;EAAuB,SAAS;EAAU,SAAS;;;;;;;;iBAgChD,oBAAoB,UAAU,2BAA2B;iBAOzD,IAAI;iBAKJ,KAAK;UAKJ;EACf;EACA,KAAK;;;;;;;iBAQe,cACpB,cACA,YACA,SAAS,mBACR;;;;;;iBAUmB,qBACpB,cACA,YACA,SAAS,mBACR,QAAQ;;;;iBA2XK,cACd,cACA,SAAS,sBACT;EAAY;;;;;;iBAiDE,kBAAkB,cAAc;;;;;iBAgBhC,0BACd,cACA,SAAS,sBACT;EAAY;;;;;;iBAgGE,oBAAoB"}
@@ -1,6 +1,7 @@
1
1
  import { ProcessExitDuringLoadError } from "../loader.js";
2
2
  import { discoverSchemas } from "../discovery.js";
3
3
  import { FAILZ_CLASS_DECL, FAIL_CLASS_DECL, FINZ_DECL, FIN_DECL, FIN_DEFERRED_DECL, MK_VALIDATOR_DECL, ZOD_CONFIG_IMPORT, ZOD_MSG_DECLARATION, generateIIFE, iifeDerefsSchema } from "../core/iife.js";
4
+ import { isUnsupportedZodVersionError, warnUnsupportedZodOnce } from "../core/extract/zod-version.js";
4
5
  import "../core/codegen/dedupe.js";
5
6
  import { aggregateUsedHelpers, compileSchemas } from "../core/pipeline.js";
6
7
  import { mayExportSchemas } from "../static-filter.js";
@@ -315,6 +316,10 @@ async function transformCodeWithMap(code, id, options) {
315
316
  compact: options.compact,
316
317
  onError(exportName, error) {
317
318
  failedCount++;
319
+ if (isUnsupportedZodVersionError(error)) {
320
+ warnUnsupportedZodOnce(error.message);
321
+ return;
322
+ }
318
323
  warn(`Failed to compile "${exportName}" in ${id}: ${error.message}. Keeping original${autoDiscover ? "" : " compile()"} call.`);
319
324
  }
320
325
  }));