size-limit is a CI-integrated tool that measures a JavaScript bundle's compressed size on every pull request and fails the build when it exceeds a configured numeric budget. A common way a bundle balloons is a single dependency version bump: a patch release pulls in a new transitive dependency that neither the PR author nor the reviewer notices in a diff full of otherwise-unrelated package.json changes. A human review misses it because bundle size isn't something a diff makes visible, and a CI check measures it on every push.
Setting up size-limit
npm install --save-dev size-limit @size-limit/preset-app
// package.json
{
"size-limit": [
{
"name": "Main bundle",
"path": "dist/assets/index-*.js",
"limit": "180 KB"
},
{
"name": "Vendor bundle",
"path": "dist/assets/vendor-*.js",
"limit": "120 KB"
}
],
"scripts": {
"size": "size-limit"
}
}
Each entry names a specific output chunk and its budget. Bundle budget is a numeric ceiling, expressed in kilobytes, that a specific build output is not allowed to exceed without failing a CI check. Splitting the budget by chunk (main app code versus vendor/dependency code) rather than one combined number makes a regression easier to diagnose from the CI failure alone, "vendor bundle exceeded its limit" points straight at a dependency change, where a single combined budget would just say "something got bigger" without narrowing down where. Which preset you install decides what gets measured. @size-limit/preset-app is the file and time plugins and doesn't bundle anything, so it compresses the files your build already wrote to dist/ and reports that size. @size-limit/preset-small-lib, which the fixture further down uses, bundles the entry together with its dependencies (through Rolldown in 14.x) and on size-limit 14.1.0 reported "with all dependencies, minified and brotlied" unless told otherwise. Either way Brotli is the default compression, so set the one you want in the config.
The GitHub Actions workflow
# .github/workflows/size-check.yml
name: Bundle Size Check
on:
pull_request:
branches: [main]
jobs:
size:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npm run build
- name: Check bundle size
run: npx size-limit
npx size-limit exits with a non-zero code if any chunk exceeds its budget, which fails the CI job and blocks the PR from being merged (assuming this check is set as required in the repo's branch protection rules). The failure message names the specific chunk and by how much it exceeded the limit, giving the PR author a concrete number to investigate instead of a vague "the bundle got bigger" complaint days later.
To confirm the check is catching regressions before a change ships, and isn't only informational:
- Open the repo's branch protection settings for
mainand check that "Bundle Size Check" is listed under required status checks. - Push a throwaway PR that imports a large, unused dependency and confirm the check fails red, not just runs and passes silently.
- Confirm the merge button is disabled while the check is red, some teams add the workflow without ever marking it required, which quietly defeats the whole setup.
A size-limit workflow that runs in CI but isn't marked as a required status check in branch protection settings can still fail red on every regression while the merge button stays fully clickable, which defeats the entire point of adding the check in the first place.
Size diff as a PR comment
The size-limit GitHub Action posts a comparison comment directly on the PR, showing how each size changed, which is more actionable during review than a pass/fail check alone:
# Add this step to the same size-check.yml workflow
- uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
This surfaces a table like Main bundle: 174.2 KB (+3.1 KB, +1.8%) directly in the PR conversation, so a reviewer sees the size impact of the change without needing to run anything locally or dig through a separate CI log. A size delta is the difference between a chunk's size on the PR branch and its size on the base branch, and surfacing it as a signed number (plus or minus, in both kilobytes and percent) is what makes a two-line dependency bump legible as a 3.1KB regression instead of an invisible line in a lockfile diff. In the lodash-es fixture further down, one changed import line would show up as roughly +29 kB gzipped, which is a number no reviewer scrolls past without asking a question.
Choosing the budget number
npx size-limit
# Main bundle: 162.4 KB / 180 KB (90%)
Run this against your current main branch first to get a real baseline, then set the budget at roughly 110-120% of that measured number, not a round figure picked without reference to the actual codebase. A budget set with no headroom fails on routine, legitimate growth (a new feature that genuinely needs a few more KB) and trains the team to bump the limit reflexively rather than treating a failure as a signal worth investigating. A budget with too much headroom, on the other hand, catches nothing until the bundle has already grown far past where anyone would want it. A library's rewrite can be a good reason to check: moving between dnd-kit packages changes the gzipped cost by more than 20 KB. Treat 10-20 percent as a starting point rather than a measured optimum: the aim is a budget tight enough that a large regression fails loudly and loose enough that a normal two-kilobyte feature addition doesn't trigger a false alarm.
What it caught on a lodash-es fixture
I built a throwaway fixture to see the failure message instead of describing it: size-limit 14.1.0, @size-limit/preset-small-lib, lodash-es 4.18.1 and date-fns 4.4.0. The baseline file imported debounce by name and format from date-fns, with an 8 kB budget. The "regressed" file made one change, import _ from 'lodash-es' instead of the named import.
| Entry | Gzip | Brotli | Budget 8 kB |
|---|---|---|---|
Named import ({ debounce }) | 6.39 kB | 5.78 kB | pass |
Default import (_) | 35.49 kB | 31.33 kB | fail |
One import line took the bundle from 6.4 kB to 35.5 kB gzipped, and it would have looked like a harmless style change in a diff. The default-import form pulled the whole lodash-es namespace into the bundle, while the named form let the bundler drop the rest. size-limit's output for the failing entry was short and specific:
Package size limit has exceeded by 27.49 kB
Size limit: 8 kB
Size: 35.49 kB with all dependencies, minified and gzipped
The process exited with code 1, which is all a CI step needs to turn a pull request red. I'd call that the entire value proposition. Before that exit code existed, the only way to notice was someone opening a bundle analyzer, and nobody does that on a Tuesday.
The same run turned up a config trap. My first config set "brotli": false, expecting that to mean gzip. It didn't: the entry reported 21.55 kB, which is the minified size with no compression at all. Gzip only turned on once I added "gzip": true. The --json flag prints size, sizeLimit and passed per entry, which is how I caught it.
What Doesn't size-limit Catch?
One cheap way to blow a budget is a name lookup such as Icons[name] from an icon package, which keeps every icon in the main chunk; in a five-library icon measurement it added between 33 KB and 1,050 KB gzip. A per-chunk budget catches growth in the chunks you've configured, it won't catch a new chunk appearing that wasn't in the config at all (a route added without route-level code-splitting, landing entirely inside the main bundle instead of its own chunk), or a regression in a chunk you haven't budgeted. Review the size-limit config itself periodically as the app's chunk structure evolves, an outdated config silently stops protecting the parts of the bundle that changed shape since it was last written. Per the size-limit repository's own documentation, the tool only measures paths matching the glob patterns you've listed, so a build output rename, say from dist/assets/index-*.js to a different hash pattern after a bundler upgrade, can silently produce zero matched files and a check that always passes for the wrong reason.
Bundle analyzer, Lighthouse CI or size-limit
| Approach | When it catches a regression | Effort to check |
|---|---|---|
| Manual bundle analyzer | Whenever someone remembers to run it | Manual, easy to skip |
| Lighthouse CI performance score | After deploy, aggregated with other metrics | Automated, but not bundle-specific |
| size-limit in CI | At PR review, before merge | Automated, zero ongoing effort once configured |