Repository navigation
perf(build): bundle viem once and minify dist (11.7MB → 6.8MB) - #640
Conversation
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.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
dist/index.jshad 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 andrequire("viem")/require("viem/chains"). esbuild resolvesrequire()through the"require"export condition, so it inlined viem's_cjsbuild (and ox's and abitype's) beside the_esmbuild 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 intsup.config.tsre-resolves thoserequire()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 (
.mapfiles aren't published). Fullminifywould save another ~1.7MB but mangles every name, so I didn't use it.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_cjsand_esmto the samesrc/files. Both checks now readdist/metafile-esm.json(emitted by tsup and excluded from the tarball). A new check fails the build if any_cjsfile 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/signerschecks.Verification
ViemSignermessage and EIP-712 signatures): byte-identical between the old CJS bundle and the ESM-redirected, minified one.test/test-e2e.mjsagainst 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