Skip to content

perf(build): bundle viem once and minify dist (11.7MB → 6.8MB) - #640

Merged
VickyXAI merged 1 commit into
mainfrom
perf/dist-size
Nov 23, 2026
Merged

VickyXAI merged 1 commit into
mainfrom
perf/dist-size

Conversation

@VickyXAI

@VickyXAI VickyXAI commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

dist/index.js had reached 11.75MB against smoke-dist's 12MB ceiling. Two changes:

1. viem was bundled twice (−1.75MB). The Polymarket SDKs (builder-relayer-client, builder-abstract-signer) are CommonJS and require("viem") / require("viem/chains"). esbuild resolves require() through the "require" export condition, so it inlined viem's _cjs build (and ox's and abitype's) beside the _esm build our source imports. That's 2.8MB of second copy that can't be tree-shaken, including every chain definition, plus two module identities on the order-signing path. A small esbuild plugin in tsup.config.ts re-resolves those require() calls with the import condition, so they land on the ESM build that's already in the bundle.

2. Whitespace + syntax minification (−3.1MB). Identifiers are kept, so stack traces users paste still name our functions (.map files aren't published). Full minify would save another ~1.7MB but mangles every name, so I didn't use it.

main this PR
dist/index.js 11.75MB 6.84MB
dist/cli.js 12.0MB 6.96MB
npm tarball (packed / unpacked) 4.66 / 25.0MB 3.38 / 14.2MB

smoke-dist. Minification strips esbuild's // node_modules/... comments, which the stale-ClawRouter and Solana single-copy checks were reading. The sourcemap can't replace them because it maps viem _cjs and _esm to the same src/ files. Both checks now read dist/metafile-esm.json (emitted by tsup and excluded from the tarball). A new check fails the build if any _cjs file of viem/ox/abitype is bundled. The ceiling drops from 12MB to 9MB. I saw each guard fire: a build without the plugin fails the CJS check, and injected metafile inputs fail the stale-copy and duplicate-@solana/signers checks.

Verification

  • Polymarket SDK harness (Safe/proxy wallet derivation, multisend + proxy encoding, ViemSigner message and EIP-712 signatures): byte-identical between the old CJS bundle and the ESM-redirected, minified one.
  • x402 signing through the built dist against a local mock gateway: Base EIP-3009 signature verifies; Solana transfer carries a valid ed25519 signature from the payer with the fee-payer slot left for the facilitator. Same result as main's dist.
  • test/test-e2e.mjs against dist: 4/4. typecheck, prettier, vitest (load-induced 5s timeouts locally were re-run with a longer timeout: all pass).

No version bump, so nothing publishes until the next release.

Summary by CodeRabbit

  • Performance
    • Production builds now apply additional minification, which can reduce the size of distributed bundles.
  • Bug Fixes
    • Improved compatibility when packaging shared dependencies for different module formats.
    • Strengthened build checks for oversized bundles and duplicate dependency copies, helping catch packaging issues earlier.

The Polymarket SDKs are CommonJS and require("viem"), so esbuild inlined
viem's _cjs build (plus ox's and abitype's) beside the _esm one our source
imports: 2.8MB of untree-shakeable second copy, every chain definition
included, and two module identities on the order-signing path. An
esbuild plugin re-resolves those require() calls with the import
condition. Then whitespace + syntax minification, identifiers kept so
user stack traces stay readable.

dist/index.js 11.75 -> 6.84MB, dist/cli.js 12.0 -> 6.96MB; npm tarball
4.66 -> 3.38MB packed, 25.0 -> 14.2MB unpacked.

smoke-dist read esbuild's // node_modules/ comments, which minification
strips, and the sourcemap maps viem _cjs and _esm to the same src/ files.
Its duplicate checks (stale ClawRouter copy, Solana single-copy) now read
dist/metafile-esm.json (not published), plus a new check that no _cjs
file of viem/ox/abitype is bundled. Ceiling 12MB -> 9MB. Each guard was
seen to fire: build without the plugin, and injected metafile inputs.

Verified: Polymarket SDK harness (Safe/proxy derivation, multisend and
proxy encoding, message + EIP-712 signatures) byte-identical with and
without the redirect; x402 Base EIP-3009 and Solana transfer signatures
through the built dist against a mock gateway verify, same as main's
dist; e2e against dist passes; typecheck, prettier, tests.
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The build redirects selected viem, ox, and abitype CommonJS resolutions to import conditions. It emits a metafile, excludes metafile files from published output, and updates the distribution smoke checks to inspect bundled inputs and enforce a lower size limit.

Changes: Bundle build and validation

Layer / File(s) Summary
Configure bundle resolution and output
tsup.config.ts, package.json
The esbuild plugin resolves matching require() requests with import conditions. The build enables whitespace and syntax minification and emits a metafile. The published-file list excludes dist/metafile-*.json.
Validate bundle inputs and size
scripts/smoke-dist.mjs
The smoke checks use metafile input paths to detect stale package bundles, duplicate CommonJS builds, and Solana package copies. They report unreadable or incomplete metafile data and lower the bundle-size limit from 12 MiB to 9 MiB.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant T as tsup.config.ts
  participant E as esbuild
  participant P as esmOnlyDualPackages
  participant M as dist/metafile-esm.json
  participant S as smoke-dist.mjs
  T->>E: Configure plugin and metafile generation
  E->>P: Resolve matching require request
  P->>E: Resolve request with import conditions
  E->>M: Write bundled input metadata
  S->>M: Read output inputs for smoke checks
Loading

Suggested reviewers: killerqueen-z

Merge Risk: Minimal · up to 0e2f9 · assessed changes only

Path filters excluded changed files. This rating applies only to the assessed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two main changes: bundling viem once and minifying the dist output, with the size reduction. The raw summary confirms an esbuild plugin that redirects viem, ox, and abitype require…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two main changes: bundling viem once and minifying the dist output, with the size reduction. The raw summary confirms an esbuild plugin that redirects viem, ox, and abitype require() resolutions, plus whitespace and syntax minification. The title is specific and matches the primary change. The conventional commit prefix is acceptable.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 2f50719 and 0e2f9dd.

Files ignored due to path filters: 6

  • dist/cli.js is excluded by !**/dist/**
  • dist/cli.js.map is excluded by !**/dist/**, !**/*.map
  • dist/index.js is excluded by !**/dist/**
  • dist/index.js.map is excluded by !**/dist/**, !**/*.map
  • dist/router/index.js is excluded by !**/dist/**
  • dist/router/index.js.map is excluded by !**/dist/**, !**/*.map
Files selected for processing: 3

  • package.json
  • scripts/smoke-dist.mjs
  • tsup.config.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@VickyXAI
VickyXAI merged commit 5bfa959 into main Nov 23, 2026
5 checks passed
@VickyXAI
VickyXAI deleted the perf/dist-size branch November 23, 2026 21:44
VickyXAI pushed a commit that referenced this pull request Nov 23, 2026
Kimi K3.1 Pro reasoning-math affinity measured (router-core#15, pin 3b2904a);
Dependabot updates (#639); viem bundled once, minified dist (#640).
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