-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: tree shake named exports #2850
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
Hi, thanks for this PR. I wanted to understand if there's any plan on reviewing and/or merging it - @colinhacks |
Bumping this up as this is a really wanted feature! 🤞😇 |
I'd imagine |
A note: the last commit "make util tree shakeable" is a breaking change. I should probably remove it or make a separate PR for it. |
This is some really great work but I was afraid to merge this in a minor release. There's also much more to be done to improve treeshaking that I'll be doing as part of a big Zod 4 refactory. I'd like to merge this as a starting point — @olehmisar can you un-archive your fork so I can merge? |
6b1692b
to
4e6c85f
Compare
@colinhacks i rebased the branch on latest master. Also, i removed the |
4e6c85f
to
ef919cd
Compare
Thanks @olehmisar! I ended up merging it with the util refactor — it was long overdue as well. I appreciate it a lot. |
@colinhacks what about this note?
Are you open for a PR for this feature? This PR only improved treeshaking of |
* fix: tree shake named exports * docs: explain why wrapper functions * fix: make coerce tree shakeable * fix: make util tree shakeable * Fix merge conflicts --------- Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
This PR allows vite/esbuild to tree shake unused exports if user imports schemas using named exports:
Before this PR, vite will bundle the whole library regardless what schemas you use. Now, it will bundle only what is needed. E.g., if you import only
number
, vite will not includestring
in the final bundle.NOTE: Compiling ESM with
tsc
would also treeshakeimport { z } from 'zod'
. This is not included in this PR but I believe this would be highly desirable.Closes #2596.
How this is fixed
static create = (...) => { ... }
is not treeshakeable by vite/esbuild, butstatic create(...) { ... }
is.const numberType = ZodNumber.create
is not treeshakeable butconst numberType = (...args) => ZodNumber.create(...args)
is.coerce
to a separate file and used named export for each schema instead ofexport const coerce
.export namespace
exports because the code that is generated by typescript is not treeshakeable.To further improve tree shaking
ZodType
. Even with this PR merged, a singlenumber()
produces a 1600 line file. It includesZodArray
because of.array()
,ZodOptional
because of.optional()
, etc.ZodType
still produces 900 lines of code in final bundle (vs 1600 lines with convenience methods). Most of that code (600 lines) is related toZodError