Skip to content

fix: import zod as a namespace so bundlers can tree-shake it - #291

Merged
benbrandt merged 1 commit into
agentclientprotocol:mainfrom
eeeggg:fix/zod-namespace-import
Oct 19, 2026
Merged

benbrandt merged 1 commit into
agentclientprotocol:mainfrom
eeeggg:fix/zod-namespace-import

Conversation

@eeeggg

@eeeggg eeeggg commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

src/schema-deserialize.ts imports zod with import { z } from "zod/v4". In zod, that named z is a namespace object re-exported by name (zod/v4/classic/index.js: import * as z from "./external.js"; export { z }). esbuild cannot tell which members of a namespace reached that way are used, so it keeps everything the namespace references, including locales. Consumers who bundle the SDK with esbuild therefore ship all 48 zod locales, even though only English is used.

  • Change that one import to import * as z from "zod/v4", the form scripts/generate.js already emits for every generated schema file and the form zod's docs use.
  • No behavior or type change: the namespace import exposes the same bindings (z.unknown, z.preprocess, z.array, z.NEVER, and the types).

Measurement

A minimal consumer (import { ClientSideConnection } from "./dist/acp.js") bundled with esbuild 0.28.2 (--bundle --minify --platform=node --format=esm):

Bundle zod locales
Before 506,785 bytes 263,831 bytes
After 149,981 bytes 2,688 bytes (English only)

Both bundles load and run.

Validation

  • npm run generate:check, lint, format:check, build, test (976 tests passed), and docs:ts:verify passed. typos was run locally via uvx typos --config ./typos.toml.
  • git diff --check passed.

No tests or documentation were added, per repository instructions.

schema-deserialize.ts was the only shipped source file importing the named
`z` export from "zod/v4". That export is itself a namespace object re-exported
by name, which esbuild cannot tree-shake, so consumers bundling the SDK with
esbuild received every zod locale (about 264 KB minified). The generated
schema files already use `import * as z from "zod/v4"`; this matches them.
No behavior change: both forms expose the same bindings.

@benbrandt benbrandt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for doing all of this testing! I'm sure there is more that we can improve, but this is already a big win! ❤️

@benbrandt
benbrandt enabled auto-merge (squash) October 19, 2026 12:11
@benbrandt
benbrandt merged commit 60619a4 into agentclientprotocol:main Oct 19, 2026
2 checks passed
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.

2 participants