Skip to content

feat: zodResolver reads a schema imported from another module - #13

Closed
mertcanaltin wants to merge 2 commits into
feat/zod-resolverfrom
feat/zod-resolver-imports
Closed

mertcanaltin wants to merge 2 commits into
feat/zod-resolverfrom
feat/zod-resolver-imports

Conversation

@mertcanaltin

Copy link
Copy Markdown
Member

Stacked on #12; the diff here is the one commit on top of it.

#12 reads a schema declared in the file of the zodResolver call. In a corpus of 150 public files that is 80 of 156 calls; 64 import the schema by name from another module (import { loginSchema } from '@/lib/validations/auth'). This reads those.

The module is read as text and never run, so the build takes no side effect from it: it is resolved with the build's own resolver over the scanned sources (relative paths, tsconfig and jsconfig paths, the usual @/ and ~/), parsed, and the exported constant is cut out with the constants it is made of, under the same rule as in #12: zod under one name, nothing but zod and constants of that module, no function. The plan, the check against zod and the replaced call are #12's. In the file of the call the schema's name leaves the import; where the file still infers a type from it, it stays as a type, and a statement left with only types becomes import type, so the module is not loaded for them.

Kept as written: a schema module that takes parts from another module, builds the schema with a function, or is not a file of the project; and an imported schema the file also uses as a value.

Measured, Next.js 16.4.6, the five-field form of #12 with its schema in lib/schema.js imported through @/lib/schema, scripts beyond the home page, gzip 9: 43,450 to 15,039 B under Turbopack. Under webpack it needs #9 (the source scan has to finish before the first file is rewritten): with #9 merged in, 37,288 to 15,562 B; without it the call stays and the page is 37,288 B.

Tests: three more cases in test/zod-resolver.test.js (11 in all): an imported schema with a type import beside it; two calls on two exports, one written as export { a as b }, with an inferred type kept; three shapes that must keep the call. npm test: 258 tests, 256 pass, 2 skipped.

Not covered: a schema re-exported through an index file, a default export, and a module that splits a schema over several files.

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