Skip to content

A zod schema put together from other modules is read: imported constants and schemas, barrels, packages of the same repository - #25

Merged
mertcanaltin merged 2 commits into
mainfrom
feat/zod-cross-module
Nov 15, 2026
Merged

mertcanaltin merged 2 commits into
mainfrom
feat/zod-cross-module

Conversation

@mertcanaltin

Copy link
Copy Markdown
Member

Measured on the application folders of six open-source projects that use zod 4 (langfuse, inbox-zero, rallly, Cap, midday, openpanel, cloned 2026-11-15, their workspace packages linked as an install links them), with scan: main compiles 50 of 167 zodResolver calls. Most of the rest are schemas put together from other modules, which the build did not read: it read the constants of the file, and a schema imported whole from a module made of zod and its own constants.

What is read now. A name a schema uses, or the schema itself, may be imported by name: a constant (export const ROLES = [...] as const), a schema, or a schema derived from an imported one (account.pick(...)). Each module on the way is read as text and never run; it has to hold the name as a const built from zod and constants in turn. export { x } from and export * from are followed, a cast (as const, as T, satisfies, !) is cut from what is read, and each module's part is written as a function of zod holding that module's own constants, so two modules may use one name. Every module has to build with the same copy of zod.

Where modules are found. The build's own resolver first (project files, tsconfig aliases). Then two more, in src/workspace-resolve.js: a relative import inside a module already read, and a package of the same repository, found in a node_modules folder above the importing file and taken only where that entry links to source of the repository (never an installed dependency), by its exports map, a * pattern included, never outside the package's folder.

The import that is no longer needed leaves the file. A part nothing else in the file uses is taken out of its import, or its module, and zod with it, would stay on the page for a name nothing reads. That also takes the dependency out of the bundler's sight, so every module read is named to it with addWatchFile (and addDependency under the Turbopack loader): a change to one rebuilds the file in a watch build.

Result on the six projects: zodResolver 50 to 62 of 167 calls (30% to 37%), safeParse 46 of 291 to 56 of 307 schemas (the count moved because more names are now recognised as schemas). The scan plans each schema and holds it to zod; the applications were not built.

What is still left, of the 105 zodResolver calls not compiled: 29 a module not found (20 of them @langfuse/shared, which exports built CommonJS; generated Prisma enums that are not in a clone), 28 a schema built from a prop, a hook or a translation function, 15 a function in the schema, 7 nullable, 6 a default.

How it was checked:

  • test/zod-resolver.test.js: a form put together from three modules through a barrel, with a cast, a namespace import of zod and one name used by two modules, built and compared with zodResolver; the module nothing reads any more is out of the bundle; an imported constant the file still uses stays imported; a function, a hook, modules that import each other, a missing module and an installed package each keep the call with a reason.
  • test/workspace-resolve.test.js: the resolver on a fixture repository with linked packages (entry, subpath pattern, condition, an installed dependency, a path that leads out of the package), a form built from a package's parts, and the modules named to the bundler.
  • npm test: 410 tests, 408 pass, 2 skipped.

Not covered: a dev server was not driven to see the rebuild after a change to a part; the six applications were scanned, not built.

… and schemas imported by name, through barrels, from the project and from packages of the same repository
…, so a watch build rebuilds the file that no longer imports it
@mertcanaltin
mertcanaltin merged commit 0a4dda2 into main Nov 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant