Measuring Bundle Size in CI
You'll learn how to automatically guard your bundle size by measuring it on every pull request—before performance regressions sneak into production.
Why Measure in CI?
Bundle size is like weight for a website. Without a scale, it creeps up unnoticed. A new dependency here, a forgotten import there—suddenly your users wait an extra second for your app to load. Measuring bundle size in continuous integration (CI) acts as an automated guardrail: if a PR increases the bundle beyond your threshold, the build fails and the team gets alerted before merging.
Tools That Do the Job
Two popular tools handle this:
bundlesize: Simple JSON config defining size limits per chunk. Fails CI if any chunk exceeds its limit.size-limit: More sophisticated—simulates download time on 3G, measures JavaScript parse cost, and can compare against the base branch.
Basic Setup Flow
- Run your production build in CI (the same
npm run buildyou'd deploy) - Measure the output using your chosen tool
- Compare to limits you've defined (e.g.,
main.jsmust stay under 150 KB) - Fail the build if any file exceeds its budget
You configure thresholds in a config file (like .size-limit.json or package.json), and the tool runs as a CI step—just like tests.
The Benefit
This prevents the "death by a thousand cuts" problem. Each PR might add only 5 KB, but over months that compounds into bloat. Automated checks enforce discipline: if you must exceed the budget, you'll at least do it consciously and document why.
Key Takeaway:
Set up bundlesize or size-limit in CI to automatically block PRs that bloat your bundle—catching regressions before they reach users.