zod
zod copied to clipboard
[v4 mini] All locales are included in the bundle
version: 3.25.0-beta.20250515T020858
code:
import { z } from 'zod/v4-mini';
export const result = z.string().safeParse('');
I use nextjs @15.2.3, page router, assembly via webpack.
Expected result:
Actual result:
Can you provide a minimal repro?
It kind of looks like nothing was tree-shaken at all.
I suggest trying import * as z
I suggest trying import * as z
The same result.
And I've just migrated from @zod/mini, there is better result with the same input code (still curious about en.js and en-GB.js but it much better):
Can you provide a minimal repro?
sure, I'll try to make a minimal reproduction.
UPD on another project (rspack without nextjs, i.e. a completely different stack) the result is the same.
UPD repl with rspack
https://github.com/dartess/zod-repl-bundle-size/tree/main
npm i
npm run build -- --analyze
Thanks, looking into this.
Treeshaking is working for me in rollup and bundlejs.com: https://bundlejs.com/?q=zod%40next%2Fv4-mini&treeshake=%5B*%5D&text=%22const+result+%3D+z.string%28%29.safeParse%28%27asfd%27%29%3B%5Cnconsole.log%28result%29%3B%22&config=%7B%22analysis%22%3Atrue%7D
It also seems to be working fine in your setup when I actually write the output to dist/main.js. So I'm not sure that bundle analyzer is behaving in the way you (or I) expect.
Initially, the problem appeared in a strong excess of the bundle size in the pipeline. Can you tell me how much space zod mini should ~approximately take up in the most minimal variation after tree-shaking? No gzip or brotli, raw size. This way I can figure out if I even have a problem and if I need to investigate it further.
Treeshaking is working for me in rollup and bundlejs.com: https://bundlejs.com/?q=zod%40next%2Fv4-mini&treeshake=%5B*%5D&text=%22const+result+%3D+z.string%28%29.safeParse%28%27asfd%27%29%3B%5Cnconsole.log%28result%29%3B%22&config=%7B%22analysis%22%3Atrue%7D
Maybe im overlooking something, but when taking your link and change esbuild setting minify=false you can see in the output that treeshaking does not work and all locales (such as de.js, en.js and unused methods) are part of the bundle:
https://bundlejs.com/?q=zod%40next%2Fv4-mini&treeshake=%5B*%5D&text=%22const+result+%3D+z.string%28%29.safeParse%28%27asfd%27%29%3B%5Cnconsole.log%28result%29%3B%22&config=%7B%22analysis%22%3Atrue%2C%22esbuild%22%3A%7B%22minify%22%3Afalse%7D%7D
Fixed in #4569, landed in https://github.com/colinhacks/zod/releases/tag/v3.25.42
@colinhacks the documentation says to use import import { z } from 'zod/v4-mini';, but in this case treeshaking doesn't work for me. it's important to use exactly import * as z from 'zod/v4-mini'; as you say above. maybe it's worth making changes to the documentation?
Had the same issue using @angular/cli v20; it was fixed by changing the import to import * as z from 'zod/v4-mini'; Agree with @dartess, this could be mentioned in documentation