Repository navigation
A zod schema put together from other modules is read: imported constants and schemas, barrels, packages of the same repository - #25
Merged
Conversation
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 167zodResolvercalls. 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 aconstbuilt from zod and constants in turn.export { x } fromandexport * fromare 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 anode_modulesfolder above the importing file and taken only where that entry links to source of the repository (never an installed dependency), by itsexportsmap, 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(andaddDependencyunder the Turbopack loader): a change to one rebuilds the file in a watch build.Result on the six projects:
zodResolver50 to 62 of 167 calls (30% to 37%),safeParse46 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
zodResolvercalls 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, 7nullable, 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 withzodResolver; 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.