zod icon indicating copy to clipboard operation
zod copied to clipboard

[v4 mini] All locales are included in the bundle

Open dartess opened this issue 6 months ago • 7 comments

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:

No default locale

Actual result:

Image

dartess avatar May 15 '25 06:05 dartess

Can you provide a minimal repro?

It kind of looks like nothing was tree-shaken at all.

I suggest trying import * as z

colinhacks avatar May 15 '25 07:05 colinhacks

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):

Image

Can you provide a minimal repro?

sure, I'll try to make a minimal reproduction.

dartess avatar May 15 '25 08:05 dartess

UPD on another project (rspack without nextjs, i.e. a completely different stack) the result is the same.

dartess avatar May 15 '25 08:05 dartess

UPD repl with rspack

https://github.com/dartess/zod-repl-bundle-size/tree/main

npm i
npm run build -- --analyze

dartess avatar May 15 '25 08:05 dartess

Thanks, looking into this.

colinhacks avatar May 15 '25 18:05 colinhacks

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.

colinhacks avatar May 16 '25 02:05 colinhacks

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.

dartess avatar May 16 '25 05:05 dartess

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

WtfJoke avatar May 25 '25 21:05 WtfJoke

Fixed in #4569, landed in https://github.com/colinhacks/zod/releases/tag/v3.25.42

colinhacks avatar May 30 '25 07:05 colinhacks

@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?

dartess avatar Jun 16 '25 14:06 dartess

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

PaulieRGlover avatar Jun 19 '25 09:06 PaulieRGlover