zod-compiler 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/dist/cli/commands/check.d.ts.map +1 -1
- package/dist/cli/commands/check.js +5 -0
- package/dist/cli/commands/check.js.map +1 -1
- package/dist/core/codegen/build-path.js +18 -6
- package/dist/core/codegen/build-path.js.map +1 -1
- package/dist/core/codegen/context.d.ts +12 -1
- package/dist/core/codegen/context.d.ts.map +1 -1
- package/dist/core/codegen/context.js +15 -1
- package/dist/core/codegen/context.js.map +1 -1
- package/dist/core/codegen/index.js +1 -0
- package/dist/core/codegen/index.js.map +1 -1
- package/dist/core/codegen/issue-decls.d.ts +67 -5
- package/dist/core/codegen/issue-decls.d.ts.map +1 -1
- package/dist/core/codegen/issue-decls.js +72 -7
- package/dist/core/codegen/issue-decls.js.map +1 -1
- package/dist/core/codegen/schemas/array.d.ts.map +1 -1
- package/dist/core/codegen/schemas/array.js +3 -3
- package/dist/core/codegen/schemas/array.js.map +1 -1
- package/dist/core/codegen/schemas/effect.d.ts +13 -1
- package/dist/core/codegen/schemas/effect.d.ts.map +1 -1
- package/dist/core/codegen/schemas/effect.js +26 -3
- package/dist/core/codegen/schemas/effect.js.map +1 -1
- package/dist/core/codegen/schemas/fallback.d.ts +17 -1
- package/dist/core/codegen/schemas/fallback.d.ts.map +1 -1
- package/dist/core/codegen/schemas/fallback.js +31 -15
- package/dist/core/codegen/schemas/fallback.js.map +1 -1
- package/dist/core/codegen/schemas/number.d.ts.map +1 -1
- package/dist/core/codegen/schemas/number.js +35 -35
- package/dist/core/codegen/schemas/number.js.map +1 -1
- package/dist/core/codegen/schemas/object.d.ts.map +1 -1
- package/dist/core/codegen/schemas/object.js +64 -29
- package/dist/core/codegen/schemas/object.js.map +1 -1
- package/dist/core/codegen/schemas/recursive-ref.d.ts.map +1 -1
- package/dist/core/codegen/schemas/recursive-ref.js +1 -0
- package/dist/core/codegen/schemas/recursive-ref.js.map +1 -1
- package/dist/core/codegen/schemas/string.d.ts.map +1 -1
- package/dist/core/codegen/schemas/string.js +3 -3
- package/dist/core/codegen/schemas/string.js.map +1 -1
- package/dist/core/codegen/schemas/template-literal.d.ts.map +1 -1
- package/dist/core/codegen/schemas/template-literal.js +1 -1
- package/dist/core/codegen/schemas/template-literal.js.map +1 -1
- package/dist/core/extract/index.d.ts.map +1 -1
- package/dist/core/extract/index.js +2 -0
- package/dist/core/extract/index.js.map +1 -1
- package/dist/core/extract/zod-version.d.ts +78 -0
- package/dist/core/extract/zod-version.d.ts.map +1 -0
- package/dist/core/extract/zod-version.js +109 -0
- package/dist/core/extract/zod-version.js.map +1 -0
- package/dist/jit.d.ts.map +1 -1
- package/dist/jit.js +9 -1
- package/dist/jit.js.map +1 -1
- package/dist/runtime.d.ts +3 -0
- package/dist/runtime.js +4 -1
- package/dist/unplugin/hoist-compile.d.ts.map +1 -1
- package/dist/unplugin/hoist-compile.js +3 -1
- package/dist/unplugin/hoist-compile.js.map +1 -1
- package/dist/unplugin/transform.d.ts.map +1 -1
- package/dist/unplugin/transform.js +5 -0
- package/dist/unplugin/transform.js.map +1 -1
- package/package.json +1 -1
|
@@ -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":"
|
|
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,S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C3C,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":";;;;
|
|
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
|
}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.js","names":[],"sources":["../../src/unplugin/transform.ts"],"sourcesContent":["import remapping from \"@jridgewell/remapping\";\nimport { parseExpressionAt } from \"acorn\";\nimport MagicString from \"magic-string\";\nimport picomatch from \"picomatch\";\nimport type { CodegenMode } from \"../core/codegen/context.js\";\nimport { SHARED_BLOCK_MARKER } from \"../core/codegen/dedupe.js\";\nimport {\n FAIL_CLASS_DECL,\n FAILZ_CLASS_DECL,\n FIN_DECL,\n FIN_DEFERRED_DECL,\n FINZ_DECL,\n generateIIFE,\n iifeDerefsSchema,\n MK_VALIDATOR_DECL,\n ZOD_CONFIG_IMPORT,\n ZOD_MSG_DECLARATION,\n} from \"../core/iife.js\";\nimport { aggregateUsedHelpers, type CompiledSchemaInfo, compileSchemas } from \"../core/pipeline.js\";\nimport type { DiscoveredSchema } from \"../core/types.js\";\nimport { discoverSchemas } from \"../discovery.js\";\nimport { ProcessExitDuringLoadError } from \"../loader.js\";\nimport { mayExportSchemas } from \"../static-filter.js\";\nimport { applyEdits, type Edit, type Insertion, moduleHeadOffset } from \"./edits.js\";\nimport { hoistZodSchemasMeta } from \"./hoist.js\";\nimport { compileHoistedSchemas } from \"./hoist-compile.js\";\nimport type { TransformOptions, ZodCompilerPluginOptions } from \"./types.js\";\nimport { VIRTUAL_RUNTIME_ID } from \"./virtual.js\";\n\n/** JSON shape of the composed sourcemap returned alongside transformed code. */\nexport interface TransformSourceMap {\n version: number;\n sources: (string | null)[];\n sourcesContent?: (string | null)[];\n names: string[];\n mappings: string;\n file?: string | null;\n}\n\n/**\n * The transform pipeline as a chain of edit batches. Each batch is applied\n * to the CURRENT text through a MagicString (one stage map per batch); the\n * final original→output map is the remapping-composed chain. Deriving the\n * output string and the map from the same edit list makes divergence\n * impossible.\n */\nclass StagedTransform {\n current: string;\n private readonly source: string;\n private readonly maps: unknown[] = [];\n\n constructor(original: string, source: string) {\n this.current = original;\n this.source = source;\n }\n\n apply(edits: readonly Edit[], insert?: Insertion): void {\n this.stage(edits, (s) => {\n if (insert === undefined) return false;\n s.appendLeft(insert.offset, insert.text);\n return true;\n });\n }\n\n /**\n * Apply `edits`, then prepend `deferred`'s text to the module head — both\n * inside ONE stage.\n *\n * The head injection (runtime import + shared dedup block) has to be decided\n * from the REWRITTEN source, because `computeRuntimePrefix` probes it for\n * already-present markers. Staging it separately made a whole second\n * `generateMap` over the full generated output — for what is only a prepend at\n * the module head — and then forced `remapping` to compose the two. Together\n * those were the dominant cost of a transform: on a 320-schema project they ran\n * to 64% of total wall time, more than discovery and codegen combined. Deferring\n * the insertion into the same MagicString buys byte-identical output and an\n * equivalent map for one generation and no composition — 1.6x (small schemas)\n * to 3.4x (large ones) on the transform, scaling with how much code a file\n * emits, since that is what both costs are proportional to.\n *\n * `deferred` returns TEXT, not an `Insertion`: `appendLeft` resolves offsets\n * against the PRE-edit text while `deferred` is shown the POST-edit text, so a\n * callback-supplied offset would be in the wrong coordinate system. Deriving it\n * here from `this.current` keeps the two in step by construction.\n */\n applyThen(edits: readonly Edit[], deferred?: (rewritten: string) => string | undefined): void {\n this.stage(edits, (s, rewritten) => {\n // `?.()` short-circuits its arguments, so a stage with no deferred step\n // never materializes the rewritten text.\n const head = deferred?.(rewritten());\n if (head === undefined) return false;\n s.appendLeft(moduleHeadOffset(this.current), head);\n return true;\n });\n }\n\n /**\n * One stage: apply `edits` to a fresh MagicString, let `inject` add at most\n * one insertion, then commit the text and its map. `inject` reports whether\n * it inserted, so a no-op stage can be skipped entirely.\n *\n * `rewritten` is a thunk, not a string: materializing it costs a full\n * `toString()` over generated-code-sized input, and the injectors that do not\n * read it (every `apply()` call) must not pay for it.\n */\n private stage(\n edits: readonly Edit[],\n inject: (s: MagicString, rewritten: () => string) => boolean,\n ): void {\n const s = new MagicString(this.current);\n for (const e of edits) {\n if (e.start === e.end) {\n s.appendLeft(e.start, e.text);\n } else {\n s.overwrite(e.start, e.end, e.text);\n }\n }\n // `toString()` is the only way to show the deferred step what the rewrite\n // produced; it measured well under 1% of a transform.\n const inserted = inject(s, () => (edits.length === 0 ? this.current : s.toString()));\n if (edits.length === 0 && !inserted) return;\n this.current = s.toString();\n // `hires: \"boundary\"` is load-bearing, not a tuning knob: without it every\n // mapping collapses to column 0, so a stack frame or debugger breakpoint in\n // untouched user code below a compiled schema lands at the start of its line\n // instead of the right column (tests/unplugin/sourcemap.test.ts pins it). It\n // is also the most expensive thing here, which is why the stage COUNT is\n // what to economize on.\n this.maps.push(s.generateMap({ source: this.source, hires: \"boundary\", includeContent: true }));\n }\n\n /** Composed original→current map, or null when nothing was applied. */\n map(): TransformSourceMap | null {\n if (this.maps.length === 0) return null;\n // A single stage needs no composition: `remapping` over a one-map chain\n // reproduces that map, and it is expensive on generated-code-sized input.\n const [only] = this.maps;\n if (this.maps.length === 1) return only as TransformSourceMap;\n const chain = [...this.maps].reverse();\n return remapping(\n chain as Parameters<typeof remapping>[0],\n () => null,\n ) as unknown as TransformSourceMap;\n }\n}\n\n/**\n * Matches a runtime (non-type-only) import from \"zod\".\n *\n * One of the three triggers ZOD_MENTION (the transform hook's `code` filter)\n * must remain a superset of — widening this to a specifier that does not\n * contain \"zod\" silently strips those files from every bundler with native\n * hook filters. `describe(\"code filter soundness\")` fails if it drifts.\n */\nexport const HAS_RUNTIME_ZOD_IMPORT =\n /import\\s+(?!type\\s)[^;]*from\\s+[\"']zod(?:\\/v\\d+)?(?:[/-]mini)?[\"']/;\n\n/**\n * Opt-in phase timing (ZOD_COMPILER_TIMING=1): accumulates per-phase wall time\n * across all transform calls and prints a summary on process exit. Used to\n * attribute plugin overhead in real builds/test runs.\n */\nconst TIMING = process.env[\"ZOD_COMPILER_TIMING\"] === \"1\";\n\n/** A single file's discovery exceeding this is worth an actionable warning. */\nconst SLOW_DISCOVERY_WARN_MS = 5_000;\nconst phaseTotals = new Map<string, { ms: number; calls: number }>();\nlet timingHookInstalled = false;\n\n/** Dedupes the process.exit-during-discovery warning to one per process. */\nlet warnedProcessExit = false;\n\nfunction timePhase<T>(phase: string, fn: () => T): T {\n if (!TIMING) return fn();\n const t0 = performance.now();\n const done = (): void => {\n const dt = performance.now() - t0;\n const agg = phaseTotals.get(phase) ?? { ms: 0, calls: 0 };\n agg.ms += dt;\n agg.calls++;\n phaseTotals.set(phase, agg);\n };\n if (!timingHookInstalled) {\n timingHookInstalled = true;\n process.on(\"exit\", () => {\n const rows = [...phaseTotals.entries()].sort((a, b) => b[1].ms - a[1].ms);\n for (const [name, { ms, calls }] of rows) {\n log(`timing ${name}: ${ms.toFixed(1)}ms over ${calls} call(s)`);\n }\n });\n }\n const r = fn();\n if (r instanceof Promise) {\n return r.finally(done) as T;\n }\n done();\n return r;\n}\n\n/**\n * Check if a file should be transformed by the plugin.\n */\nexport function shouldTransform(id: string, options?: ZodCompilerPluginOptions): boolean {\n if (!/\\.[cm]?[jt]sx?$/.test(id)) return false;\n if (id.includes(\"node_modules\")) return false;\n if (id.endsWith(\".d.ts\")) return false;\n if (id.endsWith(\".compiled.ts\") || id.endsWith(\".compiled.js\")) return false;\n\n if (options?.exclude?.some((pattern) => picomatch.isMatch(id, pattern, { contains: true })))\n return false;\n if (\n options?.include &&\n !options.include.some((pattern) => picomatch.isMatch(id, pattern, { contains: true }))\n )\n return false;\n\n return true;\n}\n\n/**\n * `id` half of the transform hook filter: the option-independent checks of\n * shouldTransform(), restated as patterns the bundler can evaluate itself.\n * Rolldown, Vite and Rollup 4.40+ apply hook filters natively, so a rejected\n * module never crosses into JS; unplugin applies the same patterns in JS for\n * the rest (webpack/rspack skip installing the transform loader entirely).\n *\n * The `include`/`exclude` options stay out of the filter on purpose: they are\n * matched with picomatch's `contains: true` semantics, which the native glob\n * support (patterns resolved against cwd, matched whole) would silently\n * narrow — shouldTransform() keeps applying them inside the handler.\n */\nexport const TRANSFORM_ID_FILTER: { exclude: RegExp[]; include: RegExp[] } = {\n exclude: [/node_modules/, /\\.d\\.ts$/, /\\.compiled\\.[jt]s$/],\n include: [/\\.[cm]?[jt]sx?$/],\n};\n\n/**\n * Every transform path that can change a file needs the substring \"zod\" (or\n * \"Zod\") somewhere in the source. There are exactly three triggers, and each\n * one is pinned by `describe(\"code filter soundness\")` in the transform tests:\n *\n * 1. auto-discovery — HAS_RUNTIME_ZOD_IMPORT (above), which only matches\n * specifiers spelled \"zod…\";\n * 2. `schemas: \"explicit\"` — an import from \"zod-compiler\" (the package name);\n * 3. hoisting — a root imported from ZOD_MODULES or an identifier matching\n * SCHEMA_NAME_PATTERN (both in hoist.ts; a *custom* pattern drops this\n * filter entirely, see transformCodeFilter).\n *\n * Adding a fourth trigger that can fire without a \"zod\" mention MUST widen\n * this pattern, or those files are silently skipped on every bundler with\n * native hook filters — no error, schemas just quietly stay uncompiled.\n *\n * Spelled as a character class rather than an `i` flag: native filters\n * recompile these patterns outside JS, where flag support is narrower.\n */\nconst ZOD_MENTION = /[Zz]od/;\n\n/**\n * `code` half of the transform hook filter, or undefined when no sound filter\n * exists. A custom `hoist.schemaNamePattern` promotes arbitrary imported\n * identifiers to schema roots (`UserModel`), so nothing in such a file is\n * guaranteed to mention zod — those setups keep the unfiltered behavior.\n */\nexport function transformCodeFilter(options?: ZodCompilerPluginOptions): RegExp | undefined {\n const namePattern = typeof options?.hoist === \"object\" ? options.hoist.schemaNamePattern : null;\n // `null` disables name matching and `undefined` keeps the default\n // /ZodSchema$/ — both leave a \"zod\" mention as the only way in.\n return namePattern === null || namePattern === undefined ? ZOD_MENTION : undefined;\n}\n\nexport function log(msg: string): void {\n // oxlint-disable-next-line no-console -- build output\n console.log(`[zod-compiler] ${msg}`);\n}\n\nexport function warn(msg: string): void {\n // oxlint-disable-next-line no-console -- build output\n console.warn(`[zod-compiler] ${msg}`);\n}\n\nexport interface TransformOutput {\n code: string;\n map: TransformSourceMap | null;\n}\n\n/**\n * Transform source code by replacing compile() calls with optimized validators.\n * Returns the transformed code or null if no transformation was needed.\n * Compatibility wrapper over transformCodeWithMap() — discards the map.\n */\nexport async function transformCode(\n code: string,\n id: string,\n options: TransformOptions,\n): Promise<string | null> {\n const result = await transformCodeWithMap(code, id, options);\n return result === null ? null : result.code;\n}\n\n/**\n * transformCode + a composed sourcemap (original → output). Stack traces in\n * transformed files shift by prepended declarations and expanded IIFEs\n * without it — a vitest assertion can be reported dozens of lines off.\n */\nexport async function transformCodeWithMap(\n code: string,\n id: string,\n options: TransformOptions,\n): Promise<TransformOutput | null> {\n const verbose = options.verbose === true;\n const autoDiscover = options.autoDiscover === true;\n const mode = options.mode;\n const staged = new StagedTransform(code, id);\n\n // Hoist Zod schema construction out of function bodies to module scope\n // (babel-plugin-zod-hoist equivalent). Mode-independent: inline schemas\n // live exactly in the files that export none. hoistZodSchemasMeta() bails\n // in microseconds when no eligible imports exist.\n let hoistedSchemas: ReturnType<typeof hoistZodSchemasMeta> = null;\n if (options.hoist !== false && code.includes(\"import\")) {\n hoistedSchemas = timePhase(\"hoist\", () =>\n hoistZodSchemasMeta(code, {\n ...(typeof options.hoist === \"object\" ? options.hoist : undefined),\n onScan: options.onSubstantialWork,\n }),\n );\n if (hoistedSchemas !== null) {\n staged.apply(hoistedSchemas.edits, hoistedSchemas.insert);\n if (verbose) {\n log(`Hoisted inline Zod schemas in ${id}`);\n }\n }\n }\n\n // Compile the hoisted schemas (autoDiscover only — compiling anonymous\n // schemas is auto-discovery of unexported module-scope schemas). Each\n // hoisted `const _zh_x = z.object({...});` whose construction is\n // deterministic (eager refs are zod bindings only) is evaluated at build\n // time and its initializer replaced with the compiled validator IIFE;\n // anything ineligible stays a plain hoist.\n const hoistHelpers = new Set<string>();\n let hoistCompiledCount = 0;\n if (autoDiscover && hoistedSchemas !== null && hoistedSchemas.schemas.length > 0) {\n const hoistCompiled = await timePhase(\"hoist-compile\", () =>\n compileHoistedSchemas(hoistedSchemas.schemas, code, id, mode),\n );\n const spliceEdits: Edit[] = [];\n for (const h of hoistCompiled) {\n const decl = `const ${h.name} = ${h.text};`;\n const at = staged.current.indexOf(decl);\n if (at === -1) continue;\n const iife = generateIIFE(h.text, h.info, { zodCompat: options.zodCompat });\n spliceEdits.push({ start: at, end: at + decl.length, text: `const ${h.name} = ${iife};` });\n hoistCompiledCount++;\n for (const helper of h.info.codegenResult.usedHelpers) {\n hoistHelpers.add(helper);\n }\n if (verbose) {\n log(` ✓ ${h.name} (hoisted schema compiled)`);\n }\n }\n staged.apply(spliceEdits);\n if (hoistCompiledCount > 0) {\n hoistHelpers.add(\"__zcMkv\");\n hoistHelpers.add(\"__zcFin\");\n }\n }\n\n // When only hoisting (± hoisted-schema compilation) changed the file, that\n // is still a transform result — with runtime helpers injected if any\n // hoisted schema compiled.\n const finishHoistOnly = (): TransformOutput | null => {\n if (staged.current === code) return null;\n if (hoistCompiledCount > 0) {\n options.onBuildStats?.({\n files: 1,\n schemas: hoistedSchemas?.schemas.length ?? 0,\n optimized: hoistCompiledCount,\n failed: 0,\n });\n const prefix = computeRuntimePrefix(staged.current, hoistHelpers, mode, options.runtimeId);\n if (prefix !== null) {\n staged.applyThen([], () => prefix);\n }\n }\n return { code: staged.current, map: staged.map() };\n };\n\n // Quick bail-out check. Both gates are also encoded in the transform hook's\n // `code` filter (ZOD_MENTION) so bundlers can skip the hook call entirely —\n // relaxing either one here without widening that pattern makes the bundler\n // drop those files before this code ever runs.\n if (autoDiscover) {\n // autoDiscover: any file with a runtime Zod import is a candidate.\n // Skip `import type` — these files have no runtime schemas.\n if (!HAS_RUNTIME_ZOD_IMPORT.test(staged.current)) return finishHoistOnly();\n } else {\n // Legacy mode: require compile() from zod-compiler. The word-boundary\n // check matters: the package name itself contains the substring\n // \"compile\", so a plain includes(\"compile\") would match every import of\n // \"zod-compiler\" — \\bcompile\\b does not match inside \"zod-compiler\"\n // (no boundary between \"e\" and \"r\") but matches compile( / { compile }.\n if (!staged.current.includes(\"zod-compiler\") || !/\\bcompile\\b/.test(staged.current))\n return finishHoistOnly();\n }\n\n // Static pre-filter: skip files whose exports provably cannot be schemas\n // (functions, components, constants, type-only modules) without executing\n // them. Conservative — anything ambiguous stays a candidate. The filter\n // transpiles + parses the file, so its outcome is worth persisting even\n // when the eventual result is null.\n options.onSubstantialWork?.();\n if (!(await timePhase(\"static-filter\", () => mayExportSchemas(staged.current, id))))\n return finishHoistOnly();\n\n // Discover schemas by executing the file. Module executions are cached in\n // the shared loader; watch/HMR changes invalidate via invalidateModuleCache().\n options.onDiscovery?.();\n let schemas: DiscoveredSchema[];\n const discoverStart = performance.now();\n try {\n schemas = await timePhase(\"discover\", () => discoverSchemas(id, { autoDiscover }));\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n // A module in the import graph called process.exit() during discovery —\n // typically an env-validation guard in a CI build where secrets are\n // intentionally absent. The loader converts that exit into a catchable\n // error so the build survives; the affected files just fall back to\n // runtime Zod. Handle it in both modes (never crash on a build-time exit)\n // and surface the cooperative remedy once.\n if (e instanceof ProcessExitDuringLoadError) {\n // The result reflects missing secrets, not file content — never persist\n // it to the content-hashed disk cache (a later secret-ful build would be\n // served this stale \"nothing compiled\" entry).\n options.onUncacheableResult?.();\n if (!warnedProcessExit) {\n warnedProcessExit = true;\n warn(\n `${id} (or a module it imports) called process.exit during build-time schema ` +\n `discovery — those files fall back to runtime Zod instead of crashing the build. ` +\n `This is usually an env-validation guard; wrap it in ` +\n `\\`if (!process.env.ZOD_COMPILER) { ... }\\` to keep these schemas compiled, or set ` +\n `schemas:\"explicit\" / use include to scope discovery.`,\n );\n }\n return finishHoistOnly();\n }\n // In autoDiscover mode, files that can't be loaded (JSX components,\n // unresolved path aliases, etc.) are expected — warn and skip.\n if (autoDiscover) {\n if (verbose) {\n warn(`Skipping ${id}: ${msg}`);\n }\n return finishHoistOnly();\n }\n throw new Error(`[zod-compiler] Failed to load schemas from ${id}: ${msg}`);\n }\n const discoverMs = performance.now() - discoverStart;\n if (discoverMs >= SLOW_DISCOVERY_WARN_MS) {\n // Discovery executes the file's whole first-party import graph inside\n // the bundler's single-threaded process — on saturated CI hosts a large\n // graph can stall the event loop long enough to trip test timeouts.\n // Surface the cost with the two effective remedies.\n warn(\n `Discovery of ${id} took ${(discoverMs / 1000).toFixed(1)}s executing its import graph ` +\n `in the bundler process. Persist node_modules/.cache/zod-compiler across CI runs to pay ` +\n `this once, or narrow autoDiscover/include for test runs (see README \"Large projects ` +\n `and CI\"). ZOD_COMPILER_TIMING=1 prints a per-phase breakdown.`,\n );\n }\n if (schemas.length === 0) return finishHoistOnly();\n\n // Lean mode (every bundler in VIRTUAL_MODULE_FRAMEWORKS — Vite/Rollup/webpack/rspack/etc.)\n // imports shared helpers from a runtime module for cross-file dedup: virtual:zod-compiler/runtime\n // on virtual-friendly bundlers, the __zod-compiler-runtime__ bare specifier on webpack/rspack.\n // Inline mode (CLI emitter, and any bundler not in VIRTUAL_MODULE_FRAMEWORKS) emits self-contained\n // file-level helpers.\n let failedCount = 0;\n const { schemas: compiled, shared } = timePhase(\"compile\", () =>\n compileSchemas(schemas, {\n mode,\n compact: options.compact,\n onError(exportName, error) {\n failedCount++;\n warn(\n `Failed to compile \"${exportName}\" in ${id}: ${error.message}. Keeping original${autoDiscover ? \"\" : \" compile()\"} call.`,\n );\n },\n }),\n );\n\n if (verbose) {\n if (autoDiscover) {\n log(\n `Auto-discovering: ${id} (${schemas.length} Zod export${schemas.length > 1 ? \"s\" : \"\"} found)`,\n );\n }\n for (const s of compiled) {\n const rfCount = s.refEntries.length;\n const rfSuffix = rfCount > 0 ? ` (${rfCount} ref${rfCount > 1 ? \"s\" : \"\"})` : \"\";\n log(` ✓ ${s.exportName}${rfSuffix}`);\n }\n if (failedCount > 0) {\n log(` ✗ ${failedCount} schema(s) failed`);\n }\n }\n\n if (compiled.length === 0) return finishHoistOnly();\n\n // Report build stats only when at least one schema was compiled\n // (hoisted-schema compiles count alongside export schemas).\n options.onBuildStats?.({\n files: 1,\n schemas: schemas.length + (hoistedSchemas?.schemas.length ?? 0),\n optimized: compiled.length + hoistCompiledCount,\n failed: failedCount,\n });\n\n // __zcMkv and __zcFin are always needed (they wrap every IIFE). Helpers used\n // by compiled hoisted schemas ride along in the same injection.\n const usedHelpers = aggregateUsedHelpers(compiled);\n usedHelpers.add(\"__zcMkv\");\n usedHelpers.add(\"__zcFin\");\n for (const helper of hoistHelpers) {\n usedHelpers.add(helper);\n }\n // Shared dedup validators ride the same runtime import.\n for (const helper of shared.usedHelpers) {\n usedHelpers.add(helper);\n }\n\n // Two-pass rewrite: separate compile() schemas from autoDiscover schemas.\n // Both passes collect edits against the same pristine stage input — their\n // target regions are disjoint (compile() assignments vs plain exported\n // declarations of OTHER names), so one batched application is equivalent\n // to the historical sequential rewrites.\n let rewriteEdits: readonly Edit[];\n if (autoDiscover) {\n // Detect compile() schemas by checking source code patterns\n const compileExportNames = new Set<string>();\n for (const s of compiled) {\n const pattern = new RegExp(`\\\\b${s.exportName}\\\\s*=\\\\s*compile[\\\\s<(]`);\n if (pattern.test(staged.current)) {\n compileExportNames.add(s.exportName);\n }\n }\n const compileSchemaInfos = compiled.filter((s) => compileExportNames.has(s.exportName));\n const autoDiscoverSchemaInfos = compiled.filter((s) => !compileExportNames.has(s.exportName));\n\n const edits: Edit[] = [];\n // Pass 1: compile() schemas (includes compile-import removal — only when\n // compile() schemas were actually rewritten, mirroring the historical\n // conditional rewriteSource call)\n if (compileSchemaInfos.length > 0) {\n edits.push(\n ...collectCompileRewriteEdits(staged.current, compileSchemaInfos, {\n zodCompat: options.zodCompat,\n }),\n );\n }\n // Pass 2: plain exported schemas\n if (autoDiscoverSchemaInfos.length > 0) {\n edits.push(\n ...collectAutoDiscoverEdits(staged.current, autoDiscoverSchemaInfos, {\n zodCompat: options.zodCompat,\n }),\n );\n }\n rewriteEdits = edits;\n } else {\n rewriteEdits = collectCompileRewriteEdits(staged.current, compiled, {\n zodCompat: options.zodCompat,\n });\n }\n\n // Head = runtime helpers/import, then the file-level shared dedup block, then\n // the rewritten source. Shared constants and `__zcSw_N` functions live at\n // module scope so every IIFE closes over them; they must follow the runtime\n // import (lean) and the helper decls (inline) that they reference. Guard against\n // double-injection on watch/HMR re-runs the same way computeRuntimePrefix\n // does — a second copy would redeclare every `__zcSw_N`.\n //\n // Deferred into the rewrite's own stage rather than staged after it: both\n // decisions read the REWRITTEN text, and giving a bare prepend its own stage\n // doubled the sourcemap work and added a composition pass (see applyThen).\n staged.applyThen(rewriteEdits, (rewritten) => {\n const prefix = computeRuntimePrefix(rewritten, usedHelpers, mode, options.runtimeId);\n const needsShared = shared.code !== \"\" && !rewritten.includes(SHARED_BLOCK_MARKER);\n const head = (prefix ?? \"\") + (needsShared ? `${shared.code}\\n` : \"\");\n return head === \"\" ? undefined : head;\n });\n return { code: staged.current, map: staged.map() };\n}\n\n/**\n * Prepend the runtime helpers required by the rewritten source.\n *\n * Lean mode emits a single `import { ... } from \"<runtimeId>\";` line —\n * bundlers whose resolveId hook intercepts the specifier dedup helpers across\n * every transformed file into one shared virtual module.\n *\n * Inline mode prepends file-level `function __zcMkv` / `function __zcFin`\n * declarations directly so the file is self-contained.\n *\n * Idempotent: if the file already contains the relevant marker (re-run during\n * watch/HMR), we skip re-injection.\n */\n/** The runtime-helper text to prepend, or null when nothing is needed. */\nfunction computeRuntimePrefix(\n code: string,\n usedHelpers: Set<string>,\n mode: CodegenMode,\n runtimeId: string = VIRTUAL_RUNTIME_ID,\n): string | null {\n if (mode === \"lean\") {\n if (usedHelpers.size === 0) return null;\n // Match the import STATEMENT, not the bare id. `zod-compiler/runtime` is a\n // plain package specifier and a substring of `virtual:zod-compiler/runtime`,\n // so a file merely mentioning either — a comment, a docs snippet — would\n // suppress the import while codegen still emits calls to the helpers.\n if (code.includes(`from \"${runtimeId}\"`)) return null;\n const names = [...usedHelpers].sort().join(\", \");\n return `import { ${names} } from \"${runtimeId}\";\\n`;\n }\n // Inline mode (CLI emitter, and any bundler not in VIRTUAL_MODULE_FRAMEWORKS):\n // ship file-level helper declarations instead of a virtual import.\n // Codegen emits per-IIFE issue literals + per-IIFE `__re_*` decls,\n // so we only need __zcMkv / __zcFin (plus __zcMsg via the zod config import).\n if (!code.includes(\"__zcMkv\")) return null;\n const prefix: string[] = [];\n if (!code.includes(\"__zodCompilerConfig\")) {\n prefix.push(ZOD_CONFIG_IMPORT, ZOD_MSG_DECLARATION);\n }\n if (!code.includes(\"function __zcMkv(\")) {\n prefix.push(MK_VALIDATOR_DECL);\n }\n // __zcFin and __zcFinD both construct __ZcFail; declare it once before either,\n // guarding against a header already shipped earlier in the same module.\n const needsFin = !code.includes(\"function __zcFin(\");\n const needsFinD = code.includes(\"__zcFinD(\") && !code.includes(\"function __zcFinD(\");\n if ((needsFin || needsFinD) && !code.includes(\"function __ZcFail(\")) {\n prefix.push(FAIL_CLASS_DECL);\n }\n if (needsFin) {\n prefix.push(FIN_DECL);\n }\n if (needsFinD) {\n prefix.push(FIN_DEFERRED_DECL);\n }\n // Compact mode (output: \"compact\") delegates cold errors to zod via __zcFinZ,\n // which constructs its own __ZcFailZ (distinct from __ZcFail).\n const needsFinZ = code.includes(\"__zcFinZ(\") && !code.includes(\"function __zcFinZ(\");\n if (needsFinZ && !code.includes(\"function __ZcFailZ(\")) {\n prefix.push(FAILZ_CLASS_DECL);\n }\n if (needsFinZ) {\n prefix.push(FINZ_DECL);\n }\n return prefix.length > 0 ? `${prefix.join(\"\\n\")}\\n` : null;\n}\n\n/**\n * Find the matching closing parenthesis for a compile() call,\n * handling nested parentheses like compile(z.object({...})).\n * Returns the index of the closing ')' or -1 if not found.\n */\nfunction findMatchingParen(code: string, openIndex: number): number {\n let depth = 1;\n for (let i = openIndex + 1; i < code.length; i++) {\n if (code[i] === \"(\") depth++;\n else if (code[i] === \")\") {\n depth--;\n if (depth === 0) return i;\n }\n }\n return -1;\n}\n\n/**\n * Rewrite source code by replacing compile() calls with IIFE-wrapped optimized validators.\n */\nexport function rewriteSource(\n code: string,\n schemas: CompiledSchemaInfo[],\n options?: { zodCompat?: boolean | undefined },\n): string {\n return applyEdits(code, collectCompileRewriteEdits(code, schemas, options));\n}\n\n/**\n * Edits for rewriteSource (compile() call replacements + compile-import\n * removal), collected against pristine `code`. Each schema's declaration is\n * a distinct region and the import statement is distinct from all of them,\n * so the batch is non-overlapping and order-independent.\n */\nfunction collectCompileRewriteEdits(\n code: string,\n schemas: CompiledSchemaInfo[],\n options?: { zodCompat?: boolean | undefined },\n): Edit[] {\n const edits: Edit[] = [];\n for (const schema of schemas) {\n // Match: <exportName> = compile<...>( with word boundary to prevent substring matches\n const prefixPattern = new RegExp(\n `(\\\\b${schema.exportName}\\\\s*=\\\\s*)compile\\\\s*(?:<[^>]*(?:<[^>]*>[^>]*)?>)?\\\\s*\\\\(`,\n );\n const match = prefixPattern.exec(code);\n if (!match) continue;\n\n // Find the matching closing paren (handles nested parens)\n const openParenIndex = match.index + match[0].length - 1;\n const closeParenIndex = findMatchingParen(code, openParenIndex);\n if (closeParenIndex === -1) continue;\n\n const schemaArgName = code\n .slice(openParenIndex + 1, closeParenIndex)\n .trim()\n .replace(/,\\s*$/, \"\");\n const prefix = match[1] ?? \"\";\n edits.push({\n start: match.index,\n end: closeParenIndex + 1,\n text: prefix + generateIIFE(schemaArgName, schema, options),\n });\n }\n edits.push(...collectRemoveCompileImportEdits(code));\n return edits;\n}\n\n/**\n * Find the end position of a JavaScript expression starting at `start` using acorn.\n * Returns the end offset, or -1 if the expression cannot be parsed.\n */\nexport function findExpressionEnd(code: string, start: number): number {\n try {\n const node = parseExpressionAt(code, start, {\n ecmaVersion: \"latest\",\n sourceType: \"module\",\n });\n return node.end;\n } catch {\n return -1;\n }\n}\n\n/**\n * Rewrite source code by replacing plain Zod schema exports with IIFE-wrapped optimized validators.\n * Used by autoDiscover mode (no compile() wrappers needed).\n */\nexport function rewriteSourceAutoDiscover(\n code: string,\n schemas: CompiledSchemaInfo[],\n options?: { zodCompat?: boolean | undefined },\n): string {\n return applyEdits(code, collectAutoDiscoverEdits(code, schemas, options));\n}\n\n/**\n * Does `expr` mention `name` as an identifier?\n *\n * Deliberately lexical, and deliberately biased toward YES. A false positive\n * costs one export its `@__PURE__` annotation; a false negative emits an IIFE\n * that dereferences a binding still under initialization. The expression text\n * is often TypeScript (`z.custom<T>(...)`), which no JS parser here can be\n * trusted to walk, so a word-boundary scan — which cannot miss a real\n * identifier reference — is the sound direction to be wrong in.\n */\nfunction mentionsIdentifier(expr: string, name: string): boolean {\n return new RegExp(`\\\\b${name.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")}\\\\b`).test(expr);\n}\n\n/** Edits for rewriteSourceAutoDiscover, collected against pristine `code`. */\nfunction collectAutoDiscoverEdits(\n code: string,\n schemas: CompiledSchemaInfo[],\n options?: { zodCompat?: boolean | undefined },\n): Edit[] {\n const edits: Edit[] = [];\n for (const schema of schemas) {\n const escapedName = schema.exportName.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n // Match: export? (const|let|var) ExportName[: TypeAnnotation] = <expr>\n const assignPattern = new RegExp(\n `((?:export\\\\s+)?(?:const|let|var)\\\\s+${escapedName}(?:\\\\s*:[^=]*)?\\\\s*=\\\\s*)`,\n );\n const match = assignPattern.exec(code);\n if (!match) continue;\n\n const rhsStart = match.index + match[0].length;\n const rhsEnd = findExpressionEnd(code, rhsStart);\n if (rhsEnd === -1) continue;\n\n const originalExpr = code.slice(rhsStart, rhsEnd).trim();\n\n // A RECURSIVE schema defers its self-reference through a callback —\n // `z.lazy(() => z.array(Node))`, or zod v4's getter form\n // `get children() { return z.array(Node) }` — and that callback closes over\n // the module binding declared here. Replacing the initializer puts the\n // IIFE's `var __rf=[__zs._zod.innerType...]` preamble INSIDE that binding's\n // own initializer, so forcing the callback re-enters a binding that is not\n // yet assigned: a TDZ ReferenceError at module init, or — once a bundler\n // lowers the top-level `const` to `var`, as esbuild does — a silent\n // `undefined` that zod's `defineLazy` then CACHES, permanently poisoning\n // the schema for every consumer (`z.array(undefined)`).\n //\n // So the deref moves out of the initializer: the declaration keeps its\n // original expression and the IIFE follows it as a statement, mutating the\n // now-assigned schema in place. `__zcMkv` returns its argument (identity is\n // preserved by design), so the export is the same object either way, and\n // `__rfp_N`'s pristine-`safeParse` capture still happens before the\n // trailing `__zcMkv` installs anything. The cost is this export's\n // `@__PURE__` annotation — a self-referential schema is no longer\n // droppable when unused.\n if (iifeDerefsSchema(schema) && mentionsIdentifier(originalExpr, schema.exportName)) {\n // `output: \"bag\"` replaces the export with a method bag rather than\n // mutating the schema, so there is nothing to mutate in place — and the\n // user's own recursive reference would resolve to the bag regardless.\n if (options?.zodCompat === false) {\n warn(\n `Skipping self-referential export \"${schema.exportName}\": output \"bag\" cannot preserve its recursive reference. Keeping the original schema.`,\n );\n continue;\n }\n // Re-emitting `originalExpr` verbatim is what makes splitting the\n // declaration safe for `const Schema = <expr>, other = 1;`:\n // findExpressionEnd parses an Expression, and the comma operator makes\n // that span the whole declarator list, so the siblings are inside the\n // text being written back rather than after the statement terminator.\n edits.push({\n start: rhsStart,\n end: rhsEnd,\n text: `${originalExpr};\\n${generateIIFE(schema.exportName, schema, { ...options, pure: false })};`,\n });\n continue;\n }\n\n edits.push({\n start: rhsStart,\n end: rhsEnd,\n text: generateIIFE(originalExpr, schema, options),\n });\n }\n return edits;\n}\n\n/**\n * Remove the `compile` binding from `import { compile, ... } from \"zod-compiler\"` statements.\n * If `compile` is the only import, the entire import line is removed.\n */\nexport function removeCompileImport(code: string): string {\n return applyEdits(code, collectRemoveCompileImportEdits(code));\n}\n\n/** Edits stripping the `compile` binding from zod-compiler import statements. */\nfunction collectRemoveCompileImportEdits(code: string): Edit[] {\n // Match: import { ... } from \"zod-compiler\" or 'zod-compiler'\n const importPattern = /import\\s*\\{([^}]*)\\}\\s*from\\s*[\"']zod-compiler[\"'];?/g;\n const edits: Edit[] = [];\n for (const match of code.matchAll(importPattern)) {\n const imports = match[1] ?? \"\";\n const names = imports\n .split(\",\")\n .map((n) => n.trim())\n .filter(Boolean);\n const remaining = names.filter((n) => n !== \"compile\");\n const text =\n remaining.length === 0 ? \"\" : `import { ${remaining.join(\", \")} } from \"zod-compiler\";`;\n if (text !== match[0]) {\n edits.push({ start: match.index, end: match.index + match[0].length, text });\n }\n }\n return edits;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA8CA,IAAM,kBAAN,MAAsB;CACpB;CACA;CACA,OAAmC,CAAC;CAEpC,YAAY,UAAkB,QAAgB;EAC5C,KAAK,UAAU;EACf,KAAK,SAAS;CAChB;CAEA,MAAM,OAAwB,QAA0B;EACtD,KAAK,MAAM,QAAQ,MAAM;GACvB,IAAI,WAAW,KAAA,GAAW,OAAO;GACjC,EAAE,WAAW,OAAO,QAAQ,OAAO,IAAI;GACvC,OAAO;EACT,CAAC;CACH;;;;;;;;;;;;;;;;;;;;;;CAuBA,UAAU,OAAwB,UAA4D;EAC5F,KAAK,MAAM,QAAQ,GAAG,cAAc;GAGlC,MAAM,OAAO,WAAW,UAAU,CAAC;GACnC,IAAI,SAAS,KAAA,GAAW,OAAO;GAC/B,EAAE,WAAW,iBAAiB,KAAK,OAAO,GAAG,IAAI;GACjD,OAAO;EACT,CAAC;CACH;;;;;;;;;;CAWA,MACE,OACA,QACM;EACN,MAAM,IAAI,IAAI,YAAY,KAAK,OAAO;EACtC,KAAK,MAAM,KAAK,OACd,IAAI,EAAE,UAAU,EAAE,KAChB,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI;OAE5B,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI;EAK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I,WAAW,IAAI;EAEnB,MAAM,eAAe,KAAK,MAAM,UAAU,MAAM,CAAC,CAAC,KAAK;EAqBvD,IAAI,iBAAiB,MAAM,KAAK,mBAAmB,cAAc,OAAO,UAAU,GAAG;GAInF,IAAI,SAAS,cAAc,OAAO;IAChC,KACE,qCAAqC,OAAO,WAAW,sFACzD;IACA;GACF;GAMA,MAAM,KAAK;IACT,OAAO;IACP,KAAK;IACL,MAAM,GAAG,aAAa,KAAK,aAAa,OAAO,YAAY,QAAQ;KAAE,GAAG;KAAS,MAAM;IAAM,CAAC,EAAE;GAClG,CAAC;GACD;EACF;EAEA,MAAM,KAAK;GACT,OAAO;GACP,KAAK;GACL,MAAM,aAAa,cAAc,QAAQ,OAAO;EAClD,CAAC;CACH;CACA,OAAO;AACT;;;;;AAMA,SAAgB,oBAAoB,MAAsB;CACxD,OAAO,WAAW,MAAM,gCAAgC,IAAI,CAAC;AAC/D;;AAGA,SAAS,gCAAgC,MAAsB;CAE7D,MAAM,gBAAgB;CACtB,MAAM,QAAgB,CAAC;CACvB,KAAK,MAAM,SAAS,KAAK,SAAS,aAAa,GAAG;EAMhD,MAAM,aALU,MAAM,MAAM,GAAA,CAEzB,MAAM,GAAG,CAAC,CACV,KAAK,MAAM,EAAE,KAAK,CAAC,CAAC,CACpB,OAAO,OACY,CAAC,CAAC,QAAQ,MAAM,MAAM,SAAS;EACrD,MAAM,OACJ,UAAU,WAAW,IAAI,KAAK,YAAY,UAAU,KAAK,IAAI,EAAE;EACjE,IAAI,SAAS,MAAM,IACjB,MAAM,KAAK;GAAE,OAAO,MAAM;GAAO,KAAK,MAAM,QAAQ,MAAM,EAAE,CAAC;GAAQ;EAAK,CAAC;CAE/E;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"transform.js","names":[],"sources":["../../src/unplugin/transform.ts"],"sourcesContent":["import remapping from \"@jridgewell/remapping\";\nimport { parseExpressionAt } from \"acorn\";\nimport MagicString from \"magic-string\";\nimport picomatch from \"picomatch\";\nimport type { CodegenMode } from \"../core/codegen/context.js\";\nimport { SHARED_BLOCK_MARKER } from \"../core/codegen/dedupe.js\";\nimport {\n isUnsupportedZodVersionError,\n warnUnsupportedZodOnce,\n} from \"../core/extract/zod-version.js\";\nimport {\n FAIL_CLASS_DECL,\n FAILZ_CLASS_DECL,\n FIN_DECL,\n FIN_DEFERRED_DECL,\n FINZ_DECL,\n generateIIFE,\n iifeDerefsSchema,\n MK_VALIDATOR_DECL,\n ZOD_CONFIG_IMPORT,\n ZOD_MSG_DECLARATION,\n} from \"../core/iife.js\";\nimport { aggregateUsedHelpers, type CompiledSchemaInfo, compileSchemas } from \"../core/pipeline.js\";\nimport type { DiscoveredSchema } from \"../core/types.js\";\nimport { discoverSchemas } from \"../discovery.js\";\nimport { ProcessExitDuringLoadError } from \"../loader.js\";\nimport { mayExportSchemas } from \"../static-filter.js\";\nimport { applyEdits, type Edit, type Insertion, moduleHeadOffset } from \"./edits.js\";\nimport { hoistZodSchemasMeta } from \"./hoist.js\";\nimport { compileHoistedSchemas } from \"./hoist-compile.js\";\nimport type { TransformOptions, ZodCompilerPluginOptions } from \"./types.js\";\nimport { VIRTUAL_RUNTIME_ID } from \"./virtual.js\";\n\n/** JSON shape of the composed sourcemap returned alongside transformed code. */\nexport interface TransformSourceMap {\n version: number;\n sources: (string | null)[];\n sourcesContent?: (string | null)[];\n names: string[];\n mappings: string;\n file?: string | null;\n}\n\n/**\n * The transform pipeline as a chain of edit batches. Each batch is applied\n * to the CURRENT text through a MagicString (one stage map per batch); the\n * final original→output map is the remapping-composed chain. Deriving the\n * output string and the map from the same edit list makes divergence\n * impossible.\n */\nclass StagedTransform {\n current: string;\n private readonly source: string;\n private readonly maps: unknown[] = [];\n\n constructor(original: string, source: string) {\n this.current = original;\n this.source = source;\n }\n\n apply(edits: readonly Edit[], insert?: Insertion): void {\n this.stage(edits, (s) => {\n if (insert === undefined) return false;\n s.appendLeft(insert.offset, insert.text);\n return true;\n });\n }\n\n /**\n * Apply `edits`, then prepend `deferred`'s text to the module head — both\n * inside ONE stage.\n *\n * The head injection (runtime import + shared dedup block) has to be decided\n * from the REWRITTEN source, because `computeRuntimePrefix` probes it for\n * already-present markers. Staging it separately made a whole second\n * `generateMap` over the full generated output — for what is only a prepend at\n * the module head — and then forced `remapping` to compose the two. Together\n * those were the dominant cost of a transform: on a 320-schema project they ran\n * to 64% of total wall time, more than discovery and codegen combined. Deferring\n * the insertion into the same MagicString buys byte-identical output and an\n * equivalent map for one generation and no composition — 1.6x (small schemas)\n * to 3.4x (large ones) on the transform, scaling with how much code a file\n * emits, since that is what both costs are proportional to.\n *\n * `deferred` returns TEXT, not an `Insertion`: `appendLeft` resolves offsets\n * against the PRE-edit text while `deferred` is shown the POST-edit text, so a\n * callback-supplied offset would be in the wrong coordinate system. Deriving it\n * here from `this.current` keeps the two in step by construction.\n */\n applyThen(edits: readonly Edit[], deferred?: (rewritten: string) => string | undefined): void {\n this.stage(edits, (s, rewritten) => {\n // `?.()` short-circuits its arguments, so a stage with no deferred step\n // never materializes the rewritten text.\n const head = deferred?.(rewritten());\n if (head === undefined) return false;\n s.appendLeft(moduleHeadOffset(this.current), head);\n return true;\n });\n }\n\n /**\n * One stage: apply `edits` to a fresh MagicString, let `inject` add at most\n * one insertion, then commit the text and its map. `inject` reports whether\n * it inserted, so a no-op stage can be skipped entirely.\n *\n * `rewritten` is a thunk, not a string: materializing it costs a full\n * `toString()` over generated-code-sized input, and the injectors that do not\n * read it (every `apply()` call) must not pay for it.\n */\n private stage(\n edits: readonly Edit[],\n inject: (s: MagicString, rewritten: () => string) => boolean,\n ): void {\n const s = new MagicString(this.current);\n for (const e of edits) {\n if (e.start === e.end) {\n s.appendLeft(e.start, e.text);\n } else {\n s.overwrite(e.start, e.end, e.text);\n }\n }\n // `toString()` is the only way to show the deferred step what the rewrite\n // produced; it measured well under 1% of a transform.\n const inserted = inject(s, () => (edits.length === 0 ? this.current : s.toString()));\n if (edits.length === 0 && !inserted) return;\n this.current = s.toString();\n // `hires: \"boundary\"` is load-bearing, not a tuning knob: without it every\n // mapping collapses to column 0, so a stack frame or debugger breakpoint in\n // untouched user code below a compiled schema lands at the start of its line\n // instead of the right column (tests/unplugin/sourcemap.test.ts pins it). It\n // is also the most expensive thing here, which is why the stage COUNT is\n // what to economize on.\n this.maps.push(s.generateMap({ source: this.source, hires: \"boundary\", includeContent: true }));\n }\n\n /** Composed original→current map, or null when nothing was applied. */\n map(): TransformSourceMap | null {\n if (this.maps.length === 0) return null;\n // A single stage needs no composition: `remapping` over a one-map chain\n // reproduces that map, and it is expensive on generated-code-sized input.\n const [only] = this.maps;\n if (this.maps.length === 1) return only as TransformSourceMap;\n const chain = [...this.maps].reverse();\n return remapping(\n chain as Parameters<typeof remapping>[0],\n () => null,\n ) as unknown as TransformSourceMap;\n }\n}\n\n/**\n * Matches a runtime (non-type-only) import from \"zod\".\n *\n * One of the three triggers ZOD_MENTION (the transform hook's `code` filter)\n * must remain a superset of — widening this to a specifier that does not\n * contain \"zod\" silently strips those files from every bundler with native\n * hook filters. `describe(\"code filter soundness\")` fails if it drifts.\n */\nexport const HAS_RUNTIME_ZOD_IMPORT =\n /import\\s+(?!type\\s)[^;]*from\\s+[\"']zod(?:\\/v\\d+)?(?:[/-]mini)?[\"']/;\n\n/**\n * Opt-in phase timing (ZOD_COMPILER_TIMING=1): accumulates per-phase wall time\n * across all transform calls and prints a summary on process exit. Used to\n * attribute plugin overhead in real builds/test runs.\n */\nconst TIMING = process.env[\"ZOD_COMPILER_TIMING\"] === \"1\";\n\n/** A single file's discovery exceeding this is worth an actionable warning. */\nconst SLOW_DISCOVERY_WARN_MS = 5_000;\nconst phaseTotals = new Map<string, { ms: number; calls: number }>();\nlet timingHookInstalled = false;\n\n/** Dedupes the process.exit-during-discovery warning to one per process. */\nlet warnedProcessExit = false;\n\nfunction timePhase<T>(phase: string, fn: () => T): T {\n if (!TIMING) return fn();\n const t0 = performance.now();\n const done = (): void => {\n const dt = performance.now() - t0;\n const agg = phaseTotals.get(phase) ?? { ms: 0, calls: 0 };\n agg.ms += dt;\n agg.calls++;\n phaseTotals.set(phase, agg);\n };\n if (!timingHookInstalled) {\n timingHookInstalled = true;\n process.on(\"exit\", () => {\n const rows = [...phaseTotals.entries()].sort((a, b) => b[1].ms - a[1].ms);\n for (const [name, { ms, calls }] of rows) {\n log(`timing ${name}: ${ms.toFixed(1)}ms over ${calls} call(s)`);\n }\n });\n }\n const r = fn();\n if (r instanceof Promise) {\n return r.finally(done) as T;\n }\n done();\n return r;\n}\n\n/**\n * Check if a file should be transformed by the plugin.\n */\nexport function shouldTransform(id: string, options?: ZodCompilerPluginOptions): boolean {\n if (!/\\.[cm]?[jt]sx?$/.test(id)) return false;\n if (id.includes(\"node_modules\")) return false;\n if (id.endsWith(\".d.ts\")) return false;\n if (id.endsWith(\".compiled.ts\") || id.endsWith(\".compiled.js\")) return false;\n\n if (options?.exclude?.some((pattern) => picomatch.isMatch(id, pattern, { contains: true })))\n return false;\n if (\n options?.include &&\n !options.include.some((pattern) => picomatch.isMatch(id, pattern, { contains: true }))\n )\n return false;\n\n return true;\n}\n\n/**\n * `id` half of the transform hook filter: the option-independent checks of\n * shouldTransform(), restated as patterns the bundler can evaluate itself.\n * Rolldown, Vite and Rollup 4.40+ apply hook filters natively, so a rejected\n * module never crosses into JS; unplugin applies the same patterns in JS for\n * the rest (webpack/rspack skip installing the transform loader entirely).\n *\n * The `include`/`exclude` options stay out of the filter on purpose: they are\n * matched with picomatch's `contains: true` semantics, which the native glob\n * support (patterns resolved against cwd, matched whole) would silently\n * narrow — shouldTransform() keeps applying them inside the handler.\n */\nexport const TRANSFORM_ID_FILTER: { exclude: RegExp[]; include: RegExp[] } = {\n exclude: [/node_modules/, /\\.d\\.ts$/, /\\.compiled\\.[jt]s$/],\n include: [/\\.[cm]?[jt]sx?$/],\n};\n\n/**\n * Every transform path that can change a file needs the substring \"zod\" (or\n * \"Zod\") somewhere in the source. There are exactly three triggers, and each\n * one is pinned by `describe(\"code filter soundness\")` in the transform tests:\n *\n * 1. auto-discovery — HAS_RUNTIME_ZOD_IMPORT (above), which only matches\n * specifiers spelled \"zod…\";\n * 2. `schemas: \"explicit\"` — an import from \"zod-compiler\" (the package name);\n * 3. hoisting — a root imported from ZOD_MODULES or an identifier matching\n * SCHEMA_NAME_PATTERN (both in hoist.ts; a *custom* pattern drops this\n * filter entirely, see transformCodeFilter).\n *\n * Adding a fourth trigger that can fire without a \"zod\" mention MUST widen\n * this pattern, or those files are silently skipped on every bundler with\n * native hook filters — no error, schemas just quietly stay uncompiled.\n *\n * Spelled as a character class rather than an `i` flag: native filters\n * recompile these patterns outside JS, where flag support is narrower.\n */\nconst ZOD_MENTION = /[Zz]od/;\n\n/**\n * `code` half of the transform hook filter, or undefined when no sound filter\n * exists. A custom `hoist.schemaNamePattern` promotes arbitrary imported\n * identifiers to schema roots (`UserModel`), so nothing in such a file is\n * guaranteed to mention zod — those setups keep the unfiltered behavior.\n */\nexport function transformCodeFilter(options?: ZodCompilerPluginOptions): RegExp | undefined {\n const namePattern = typeof options?.hoist === \"object\" ? options.hoist.schemaNamePattern : null;\n // `null` disables name matching and `undefined` keeps the default\n // /ZodSchema$/ — both leave a \"zod\" mention as the only way in.\n return namePattern === null || namePattern === undefined ? ZOD_MENTION : undefined;\n}\n\nexport function log(msg: string): void {\n // oxlint-disable-next-line no-console -- build output\n console.log(`[zod-compiler] ${msg}`);\n}\n\nexport function warn(msg: string): void {\n // oxlint-disable-next-line no-console -- build output\n console.warn(`[zod-compiler] ${msg}`);\n}\n\nexport interface TransformOutput {\n code: string;\n map: TransformSourceMap | null;\n}\n\n/**\n * Transform source code by replacing compile() calls with optimized validators.\n * Returns the transformed code or null if no transformation was needed.\n * Compatibility wrapper over transformCodeWithMap() — discards the map.\n */\nexport async function transformCode(\n code: string,\n id: string,\n options: TransformOptions,\n): Promise<string | null> {\n const result = await transformCodeWithMap(code, id, options);\n return result === null ? null : result.code;\n}\n\n/**\n * transformCode + a composed sourcemap (original → output). Stack traces in\n * transformed files shift by prepended declarations and expanded IIFEs\n * without it — a vitest assertion can be reported dozens of lines off.\n */\nexport async function transformCodeWithMap(\n code: string,\n id: string,\n options: TransformOptions,\n): Promise<TransformOutput | null> {\n const verbose = options.verbose === true;\n const autoDiscover = options.autoDiscover === true;\n const mode = options.mode;\n const staged = new StagedTransform(code, id);\n\n // Hoist Zod schema construction out of function bodies to module scope\n // (babel-plugin-zod-hoist equivalent). Mode-independent: inline schemas\n // live exactly in the files that export none. hoistZodSchemasMeta() bails\n // in microseconds when no eligible imports exist.\n let hoistedSchemas: ReturnType<typeof hoistZodSchemasMeta> = null;\n if (options.hoist !== false && code.includes(\"import\")) {\n hoistedSchemas = timePhase(\"hoist\", () =>\n hoistZodSchemasMeta(code, {\n ...(typeof options.hoist === \"object\" ? options.hoist : undefined),\n onScan: options.onSubstantialWork,\n }),\n );\n if (hoistedSchemas !== null) {\n staged.apply(hoistedSchemas.edits, hoistedSchemas.insert);\n if (verbose) {\n log(`Hoisted inline Zod schemas in ${id}`);\n }\n }\n }\n\n // Compile the hoisted schemas (autoDiscover only — compiling anonymous\n // schemas is auto-discovery of unexported module-scope schemas). Each\n // hoisted `const _zh_x = z.object({...});` whose construction is\n // deterministic (eager refs are zod bindings only) is evaluated at build\n // time and its initializer replaced with the compiled validator IIFE;\n // anything ineligible stays a plain hoist.\n const hoistHelpers = new Set<string>();\n let hoistCompiledCount = 0;\n if (autoDiscover && hoistedSchemas !== null && hoistedSchemas.schemas.length > 0) {\n const hoistCompiled = await timePhase(\"hoist-compile\", () =>\n compileHoistedSchemas(hoistedSchemas.schemas, code, id, mode),\n );\n const spliceEdits: Edit[] = [];\n for (const h of hoistCompiled) {\n const decl = `const ${h.name} = ${h.text};`;\n const at = staged.current.indexOf(decl);\n if (at === -1) continue;\n const iife = generateIIFE(h.text, h.info, { zodCompat: options.zodCompat });\n spliceEdits.push({ start: at, end: at + decl.length, text: `const ${h.name} = ${iife};` });\n hoistCompiledCount++;\n for (const helper of h.info.codegenResult.usedHelpers) {\n hoistHelpers.add(helper);\n }\n if (verbose) {\n log(` ✓ ${h.name} (hoisted schema compiled)`);\n }\n }\n staged.apply(spliceEdits);\n if (hoistCompiledCount > 0) {\n hoistHelpers.add(\"__zcMkv\");\n hoistHelpers.add(\"__zcFin\");\n }\n }\n\n // When only hoisting (± hoisted-schema compilation) changed the file, that\n // is still a transform result — with runtime helpers injected if any\n // hoisted schema compiled.\n const finishHoistOnly = (): TransformOutput | null => {\n if (staged.current === code) return null;\n if (hoistCompiledCount > 0) {\n options.onBuildStats?.({\n files: 1,\n schemas: hoistedSchemas?.schemas.length ?? 0,\n optimized: hoistCompiledCount,\n failed: 0,\n });\n const prefix = computeRuntimePrefix(staged.current, hoistHelpers, mode, options.runtimeId);\n if (prefix !== null) {\n staged.applyThen([], () => prefix);\n }\n }\n return { code: staged.current, map: staged.map() };\n };\n\n // Quick bail-out check. Both gates are also encoded in the transform hook's\n // `code` filter (ZOD_MENTION) so bundlers can skip the hook call entirely —\n // relaxing either one here without widening that pattern makes the bundler\n // drop those files before this code ever runs.\n if (autoDiscover) {\n // autoDiscover: any file with a runtime Zod import is a candidate.\n // Skip `import type` — these files have no runtime schemas.\n if (!HAS_RUNTIME_ZOD_IMPORT.test(staged.current)) return finishHoistOnly();\n } else {\n // Legacy mode: require compile() from zod-compiler. The word-boundary\n // check matters: the package name itself contains the substring\n // \"compile\", so a plain includes(\"compile\") would match every import of\n // \"zod-compiler\" — \\bcompile\\b does not match inside \"zod-compiler\"\n // (no boundary between \"e\" and \"r\") but matches compile( / { compile }.\n if (!staged.current.includes(\"zod-compiler\") || !/\\bcompile\\b/.test(staged.current))\n return finishHoistOnly();\n }\n\n // Static pre-filter: skip files whose exports provably cannot be schemas\n // (functions, components, constants, type-only modules) without executing\n // them. Conservative — anything ambiguous stays a candidate. The filter\n // transpiles + parses the file, so its outcome is worth persisting even\n // when the eventual result is null.\n options.onSubstantialWork?.();\n if (!(await timePhase(\"static-filter\", () => mayExportSchemas(staged.current, id))))\n return finishHoistOnly();\n\n // Discover schemas by executing the file. Module executions are cached in\n // the shared loader; watch/HMR changes invalidate via invalidateModuleCache().\n options.onDiscovery?.();\n let schemas: DiscoveredSchema[];\n const discoverStart = performance.now();\n try {\n schemas = await timePhase(\"discover\", () => discoverSchemas(id, { autoDiscover }));\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n // A module in the import graph called process.exit() during discovery —\n // typically an env-validation guard in a CI build where secrets are\n // intentionally absent. The loader converts that exit into a catchable\n // error so the build survives; the affected files just fall back to\n // runtime Zod. Handle it in both modes (never crash on a build-time exit)\n // and surface the cooperative remedy once.\n if (e instanceof ProcessExitDuringLoadError) {\n // The result reflects missing secrets, not file content — never persist\n // it to the content-hashed disk cache (a later secret-ful build would be\n // served this stale \"nothing compiled\" entry).\n options.onUncacheableResult?.();\n if (!warnedProcessExit) {\n warnedProcessExit = true;\n warn(\n `${id} (or a module it imports) called process.exit during build-time schema ` +\n `discovery — those files fall back to runtime Zod instead of crashing the build. ` +\n `This is usually an env-validation guard; wrap it in ` +\n `\\`if (!process.env.ZOD_COMPILER) { ... }\\` to keep these schemas compiled, or set ` +\n `schemas:\"explicit\" / use include to scope discovery.`,\n );\n }\n return finishHoistOnly();\n }\n // In autoDiscover mode, files that can't be loaded (JSX components,\n // unresolved path aliases, etc.) are expected — warn and skip.\n if (autoDiscover) {\n if (verbose) {\n warn(`Skipping ${id}: ${msg}`);\n }\n return finishHoistOnly();\n }\n throw new Error(`[zod-compiler] Failed to load schemas from ${id}: ${msg}`);\n }\n const discoverMs = performance.now() - discoverStart;\n if (discoverMs >= SLOW_DISCOVERY_WARN_MS) {\n // Discovery executes the file's whole first-party import graph inside\n // the bundler's single-threaded process — on saturated CI hosts a large\n // graph can stall the event loop long enough to trip test timeouts.\n // Surface the cost with the two effective remedies.\n warn(\n `Discovery of ${id} took ${(discoverMs / 1000).toFixed(1)}s executing its import graph ` +\n `in the bundler process. Persist node_modules/.cache/zod-compiler across CI runs to pay ` +\n `this once, or narrow autoDiscover/include for test runs (see README \"Large projects ` +\n `and CI\"). ZOD_COMPILER_TIMING=1 prints a per-phase breakdown.`,\n );\n }\n if (schemas.length === 0) return finishHoistOnly();\n\n // Lean mode (every bundler in VIRTUAL_MODULE_FRAMEWORKS — Vite/Rollup/webpack/rspack/etc.)\n // imports shared helpers from a runtime module for cross-file dedup: virtual:zod-compiler/runtime\n // on virtual-friendly bundlers, the __zod-compiler-runtime__ bare specifier on webpack/rspack.\n // Inline mode (CLI emitter, and any bundler not in VIRTUAL_MODULE_FRAMEWORKS) emits self-contained\n // file-level helpers.\n let failedCount = 0;\n const { schemas: compiled, shared } = timePhase(\"compile\", () =>\n compileSchemas(schemas, {\n mode,\n compact: options.compact,\n onError(exportName, error) {\n failedCount++;\n // An unsupported zod fails every export of every file the same way:\n // one explanation per process says more than a line per export.\n if (isUnsupportedZodVersionError(error)) {\n warnUnsupportedZodOnce(error.message);\n return;\n }\n warn(\n `Failed to compile \"${exportName}\" in ${id}: ${error.message}. Keeping original${autoDiscover ? \"\" : \" compile()\"} call.`,\n );\n },\n }),\n );\n\n if (verbose) {\n if (autoDiscover) {\n log(\n `Auto-discovering: ${id} (${schemas.length} Zod export${schemas.length > 1 ? \"s\" : \"\"} found)`,\n );\n }\n for (const s of compiled) {\n const rfCount = s.refEntries.length;\n const rfSuffix = rfCount > 0 ? ` (${rfCount} ref${rfCount > 1 ? \"s\" : \"\"})` : \"\";\n log(` ✓ ${s.exportName}${rfSuffix}`);\n }\n if (failedCount > 0) {\n log(` ✗ ${failedCount} schema(s) failed`);\n }\n }\n\n if (compiled.length === 0) return finishHoistOnly();\n\n // Report build stats only when at least one schema was compiled\n // (hoisted-schema compiles count alongside export schemas).\n options.onBuildStats?.({\n files: 1,\n schemas: schemas.length + (hoistedSchemas?.schemas.length ?? 0),\n optimized: compiled.length + hoistCompiledCount,\n failed: failedCount,\n });\n\n // __zcMkv and __zcFin are always needed (they wrap every IIFE). Helpers used\n // by compiled hoisted schemas ride along in the same injection.\n const usedHelpers = aggregateUsedHelpers(compiled);\n usedHelpers.add(\"__zcMkv\");\n usedHelpers.add(\"__zcFin\");\n for (const helper of hoistHelpers) {\n usedHelpers.add(helper);\n }\n // Shared dedup validators ride the same runtime import.\n for (const helper of shared.usedHelpers) {\n usedHelpers.add(helper);\n }\n\n // Two-pass rewrite: separate compile() schemas from autoDiscover schemas.\n // Both passes collect edits against the same pristine stage input — their\n // target regions are disjoint (compile() assignments vs plain exported\n // declarations of OTHER names), so one batched application is equivalent\n // to the historical sequential rewrites.\n let rewriteEdits: readonly Edit[];\n if (autoDiscover) {\n // Detect compile() schemas by checking source code patterns\n const compileExportNames = new Set<string>();\n for (const s of compiled) {\n const pattern = new RegExp(`\\\\b${s.exportName}\\\\s*=\\\\s*compile[\\\\s<(]`);\n if (pattern.test(staged.current)) {\n compileExportNames.add(s.exportName);\n }\n }\n const compileSchemaInfos = compiled.filter((s) => compileExportNames.has(s.exportName));\n const autoDiscoverSchemaInfos = compiled.filter((s) => !compileExportNames.has(s.exportName));\n\n const edits: Edit[] = [];\n // Pass 1: compile() schemas (includes compile-import removal — only when\n // compile() schemas were actually rewritten, mirroring the historical\n // conditional rewriteSource call)\n if (compileSchemaInfos.length > 0) {\n edits.push(\n ...collectCompileRewriteEdits(staged.current, compileSchemaInfos, {\n zodCompat: options.zodCompat,\n }),\n );\n }\n // Pass 2: plain exported schemas\n if (autoDiscoverSchemaInfos.length > 0) {\n edits.push(\n ...collectAutoDiscoverEdits(staged.current, autoDiscoverSchemaInfos, {\n zodCompat: options.zodCompat,\n }),\n );\n }\n rewriteEdits = edits;\n } else {\n rewriteEdits = collectCompileRewriteEdits(staged.current, compiled, {\n zodCompat: options.zodCompat,\n });\n }\n\n // Head = runtime helpers/import, then the file-level shared dedup block, then\n // the rewritten source. Shared constants and `__zcSw_N` functions live at\n // module scope so every IIFE closes over them; they must follow the runtime\n // import (lean) and the helper decls (inline) that they reference. Guard against\n // double-injection on watch/HMR re-runs the same way computeRuntimePrefix\n // does — a second copy would redeclare every `__zcSw_N`.\n //\n // Deferred into the rewrite's own stage rather than staged after it: both\n // decisions read the REWRITTEN text, and giving a bare prepend its own stage\n // doubled the sourcemap work and added a composition pass (see applyThen).\n staged.applyThen(rewriteEdits, (rewritten) => {\n const prefix = computeRuntimePrefix(rewritten, usedHelpers, mode, options.runtimeId);\n const needsShared = shared.code !== \"\" && !rewritten.includes(SHARED_BLOCK_MARKER);\n const head = (prefix ?? \"\") + (needsShared ? `${shared.code}\\n` : \"\");\n return head === \"\" ? undefined : head;\n });\n return { code: staged.current, map: staged.map() };\n}\n\n/**\n * Prepend the runtime helpers required by the rewritten source.\n *\n * Lean mode emits a single `import { ... } from \"<runtimeId>\";` line —\n * bundlers whose resolveId hook intercepts the specifier dedup helpers across\n * every transformed file into one shared virtual module.\n *\n * Inline mode prepends file-level `function __zcMkv` / `function __zcFin`\n * declarations directly so the file is self-contained.\n *\n * Idempotent: if the file already contains the relevant marker (re-run during\n * watch/HMR), we skip re-injection.\n */\n/** The runtime-helper text to prepend, or null when nothing is needed. */\nfunction computeRuntimePrefix(\n code: string,\n usedHelpers: Set<string>,\n mode: CodegenMode,\n runtimeId: string = VIRTUAL_RUNTIME_ID,\n): string | null {\n if (mode === \"lean\") {\n if (usedHelpers.size === 0) return null;\n // Match the import STATEMENT, not the bare id. `zod-compiler/runtime` is a\n // plain package specifier and a substring of `virtual:zod-compiler/runtime`,\n // so a file merely mentioning either — a comment, a docs snippet — would\n // suppress the import while codegen still emits calls to the helpers.\n if (code.includes(`from \"${runtimeId}\"`)) return null;\n const names = [...usedHelpers].sort().join(\", \");\n return `import { ${names} } from \"${runtimeId}\";\\n`;\n }\n // Inline mode (CLI emitter, and any bundler not in VIRTUAL_MODULE_FRAMEWORKS):\n // ship file-level helper declarations instead of a virtual import.\n // Codegen emits per-IIFE issue literals + per-IIFE `__re_*` decls,\n // so we only need __zcMkv / __zcFin (plus __zcMsg via the zod config import).\n if (!code.includes(\"__zcMkv\")) return null;\n const prefix: string[] = [];\n if (!code.includes(\"__zodCompilerConfig\")) {\n prefix.push(ZOD_CONFIG_IMPORT, ZOD_MSG_DECLARATION);\n }\n if (!code.includes(\"function __zcMkv(\")) {\n prefix.push(MK_VALIDATOR_DECL);\n }\n // __zcFin and __zcFinD both construct __ZcFail; declare it once before either,\n // guarding against a header already shipped earlier in the same module.\n const needsFin = !code.includes(\"function __zcFin(\");\n const needsFinD = code.includes(\"__zcFinD(\") && !code.includes(\"function __zcFinD(\");\n if ((needsFin || needsFinD) && !code.includes(\"function __ZcFail(\")) {\n prefix.push(FAIL_CLASS_DECL);\n }\n if (needsFin) {\n prefix.push(FIN_DECL);\n }\n if (needsFinD) {\n prefix.push(FIN_DEFERRED_DECL);\n }\n // Compact mode (output: \"compact\") delegates cold errors to zod via __zcFinZ,\n // which constructs its own __ZcFailZ (distinct from __ZcFail).\n const needsFinZ = code.includes(\"__zcFinZ(\") && !code.includes(\"function __zcFinZ(\");\n if (needsFinZ && !code.includes(\"function __ZcFailZ(\")) {\n prefix.push(FAILZ_CLASS_DECL);\n }\n if (needsFinZ) {\n prefix.push(FINZ_DECL);\n }\n return prefix.length > 0 ? `${prefix.join(\"\\n\")}\\n` : null;\n}\n\n/**\n * Find the matching closing parenthesis for a compile() call,\n * handling nested parentheses like compile(z.object({...})).\n * Returns the index of the closing ')' or -1 if not found.\n */\nfunction findMatchingParen(code: string, openIndex: number): number {\n let depth = 1;\n for (let i = openIndex + 1; i < code.length; i++) {\n if (code[i] === \"(\") depth++;\n else if (code[i] === \")\") {\n depth--;\n if (depth === 0) return i;\n }\n }\n return -1;\n}\n\n/**\n * Rewrite source code by replacing compile() calls with IIFE-wrapped optimized validators.\n */\nexport function rewriteSource(\n code: string,\n schemas: CompiledSchemaInfo[],\n options?: { zodCompat?: boolean | undefined },\n): string {\n return applyEdits(code, collectCompileRewriteEdits(code, schemas, options));\n}\n\n/**\n * Edits for rewriteSource (compile() call replacements + compile-import\n * removal), collected against pristine `code`. Each schema's declaration is\n * a distinct region and the import statement is distinct from all of them,\n * so the batch is non-overlapping and order-independent.\n */\nfunction collectCompileRewriteEdits(\n code: string,\n schemas: CompiledSchemaInfo[],\n options?: { zodCompat?: boolean | undefined },\n): Edit[] {\n const edits: Edit[] = [];\n for (const schema of schemas) {\n // Match: <exportName> = compile<...>( with word boundary to prevent substring matches\n const prefixPattern = new RegExp(\n `(\\\\b${schema.exportName}\\\\s*=\\\\s*)compile\\\\s*(?:<[^>]*(?:<[^>]*>[^>]*)?>)?\\\\s*\\\\(`,\n );\n const match = prefixPattern.exec(code);\n if (!match) continue;\n\n // Find the matching closing paren (handles nested parens)\n const openParenIndex = match.index + match[0].length - 1;\n const closeParenIndex = findMatchingParen(code, openParenIndex);\n if (closeParenIndex === -1) continue;\n\n const schemaArgName = code\n .slice(openParenIndex + 1, closeParenIndex)\n .trim()\n .replace(/,\\s*$/, \"\");\n const prefix = match[1] ?? \"\";\n edits.push({\n start: match.index,\n end: closeParenIndex + 1,\n text: prefix + generateIIFE(schemaArgName, schema, options),\n });\n }\n edits.push(...collectRemoveCompileImportEdits(code));\n return edits;\n}\n\n/**\n * Find the end position of a JavaScript expression starting at `start` using acorn.\n * Returns the end offset, or -1 if the expression cannot be parsed.\n */\nexport function findExpressionEnd(code: string, start: number): number {\n try {\n const node = parseExpressionAt(code, start, {\n ecmaVersion: \"latest\",\n sourceType: \"module\",\n });\n return node.end;\n } catch {\n return -1;\n }\n}\n\n/**\n * Rewrite source code by replacing plain Zod schema exports with IIFE-wrapped optimized validators.\n * Used by autoDiscover mode (no compile() wrappers needed).\n */\nexport function rewriteSourceAutoDiscover(\n code: string,\n schemas: CompiledSchemaInfo[],\n options?: { zodCompat?: boolean | undefined },\n): string {\n return applyEdits(code, collectAutoDiscoverEdits(code, schemas, options));\n}\n\n/**\n * Does `expr` mention `name` as an identifier?\n *\n * Deliberately lexical, and deliberately biased toward YES. A false positive\n * costs one export its `@__PURE__` annotation; a false negative emits an IIFE\n * that dereferences a binding still under initialization. The expression text\n * is often TypeScript (`z.custom<T>(...)`), which no JS parser here can be\n * trusted to walk, so a word-boundary scan — which cannot miss a real\n * identifier reference — is the sound direction to be wrong in.\n */\nfunction mentionsIdentifier(expr: string, name: string): boolean {\n return new RegExp(`\\\\b${name.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")}\\\\b`).test(expr);\n}\n\n/** Edits for rewriteSourceAutoDiscover, collected against pristine `code`. */\nfunction collectAutoDiscoverEdits(\n code: string,\n schemas: CompiledSchemaInfo[],\n options?: { zodCompat?: boolean | undefined },\n): Edit[] {\n const edits: Edit[] = [];\n for (const schema of schemas) {\n const escapedName = schema.exportName.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n // Match: export? (const|let|var) ExportName[: TypeAnnotation] = <expr>\n const assignPattern = new RegExp(\n `((?:export\\\\s+)?(?:const|let|var)\\\\s+${escapedName}(?:\\\\s*:[^=]*)?\\\\s*=\\\\s*)`,\n );\n const match = assignPattern.exec(code);\n if (!match) continue;\n\n const rhsStart = match.index + match[0].length;\n const rhsEnd = findExpressionEnd(code, rhsStart);\n if (rhsEnd === -1) continue;\n\n const originalExpr = code.slice(rhsStart, rhsEnd).trim();\n\n // A RECURSIVE schema defers its self-reference through a callback —\n // `z.lazy(() => z.array(Node))`, or zod v4's getter form\n // `get children() { return z.array(Node) }` — and that callback closes over\n // the module binding declared here. Replacing the initializer puts the\n // IIFE's `var __rf=[__zs._zod.innerType...]` preamble INSIDE that binding's\n // own initializer, so forcing the callback re-enters a binding that is not\n // yet assigned: a TDZ ReferenceError at module init, or — once a bundler\n // lowers the top-level `const` to `var`, as esbuild does — a silent\n // `undefined` that zod's `defineLazy` then CACHES, permanently poisoning\n // the schema for every consumer (`z.array(undefined)`).\n //\n // So the deref moves out of the initializer: the declaration keeps its\n // original expression and the IIFE follows it as a statement, mutating the\n // now-assigned schema in place. `__zcMkv` returns its argument (identity is\n // preserved by design), so the export is the same object either way, and\n // `__rfp_N`'s pristine-`safeParse` capture still happens before the\n // trailing `__zcMkv` installs anything. The cost is this export's\n // `@__PURE__` annotation — a self-referential schema is no longer\n // droppable when unused.\n if (iifeDerefsSchema(schema) && mentionsIdentifier(originalExpr, schema.exportName)) {\n // `output: \"bag\"` replaces the export with a method bag rather than\n // mutating the schema, so there is nothing to mutate in place — and the\n // user's own recursive reference would resolve to the bag regardless.\n if (options?.zodCompat === false) {\n warn(\n `Skipping self-referential export \"${schema.exportName}\": output \"bag\" cannot preserve its recursive reference. Keeping the original schema.`,\n );\n continue;\n }\n // Re-emitting `originalExpr` verbatim is what makes splitting the\n // declaration safe for `const Schema = <expr>, other = 1;`:\n // findExpressionEnd parses an Expression, and the comma operator makes\n // that span the whole declarator list, so the siblings are inside the\n // text being written back rather than after the statement terminator.\n edits.push({\n start: rhsStart,\n end: rhsEnd,\n text: `${originalExpr};\\n${generateIIFE(schema.exportName, schema, { ...options, pure: false })};`,\n });\n continue;\n }\n\n edits.push({\n start: rhsStart,\n end: rhsEnd,\n text: generateIIFE(originalExpr, schema, options),\n });\n }\n return edits;\n}\n\n/**\n * Remove the `compile` binding from `import { compile, ... } from \"zod-compiler\"` statements.\n * If `compile` is the only import, the entire import line is removed.\n */\nexport function removeCompileImport(code: string): string {\n return applyEdits(code, collectRemoveCompileImportEdits(code));\n}\n\n/** Edits stripping the `compile` binding from zod-compiler import statements. */\nfunction collectRemoveCompileImportEdits(code: string): Edit[] {\n // Match: import { ... } from \"zod-compiler\" or 'zod-compiler'\n const importPattern = /import\\s*\\{([^}]*)\\}\\s*from\\s*[\"']zod-compiler[\"'];?/g;\n const edits: Edit[] = [];\n for (const match of code.matchAll(importPattern)) {\n const imports = match[1] ?? \"\";\n const names = imports\n .split(\",\")\n .map((n) => n.trim())\n .filter(Boolean);\n const remaining = names.filter((n) => n !== \"compile\");\n const text =\n remaining.length === 0 ? \"\" : `import { ${remaining.join(\", \")} } from \"zod-compiler\";`;\n if (text !== match[0]) {\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