|
| 1 | +--- |
| 2 | +name: size-limit |
| 3 | +description: Set up and maintain size-limit checks for browser-targeted, tree-shakeable library packages — .size-limit.json per package, Brotli entries with optional low-noise Gzip twins, limits pinned on a 0.05 kB step to neutralize compression noise, and a rebuild-and-re-pin routine after changes. Apply when adding size checks to a package or updating limits after code changes. |
| 4 | +license: MIT |
| 5 | +compatibility: |
| 6 | + - Claude Code |
| 7 | + - Codex |
| 8 | + - Cursor |
| 9 | + - Gemini CLI |
| 10 | + - GitHub Copilot |
| 11 | + - Windsurf |
| 12 | + - Cline |
| 13 | + - Roo Code |
| 14 | + - Goose |
| 15 | + - Continue |
| 16 | + - OpenCode |
| 17 | + - Amp |
| 18 | + - universal |
| 19 | +metadata: |
| 20 | + author: dangreen |
| 21 | + tags: |
| 22 | + - size-limit |
| 23 | + - bundle-size |
| 24 | + - library |
| 25 | + - ci |
| 26 | +--- |
| 27 | + |
| 28 | +# Size Limit |
| 29 | + |
| 30 | +Pin bundle size as a tested contract with [size-limit](https://github.com/ai/size-limit): every package with a public surface gets a `.size-limit.json`, and any regression fails `npx size-limit` instead of silently shipping. |
| 31 | + |
| 32 | +Size-limit only makes sense for libraries that target the browser and are designed for tree-shaking (see the `tree-shaking` skill): the measured number is what a consumer's bundler ships over the wire. For Node-only tools, CLIs, or server-side code, bundle size is not the constraint — skip it there. |
| 33 | + |
| 34 | +## Setup |
| 35 | + |
| 36 | +Per package with a public surface: |
| 37 | + |
| 38 | +- dev dependencies: `size-limit` + `@size-limit/preset-small-lib`; |
| 39 | +- a script: `"test:size": "size-limit"`; |
| 40 | +- a `.size-limit.json` next to `package.json`. |
| 41 | + |
| 42 | +Each entry measures one bundle (`path` + optional `import` subset) against a pinned `limit`: |
| 43 | + |
| 44 | +```json |
| 45 | +[ |
| 46 | + { |
| 47 | + "name": "All publics", |
| 48 | + "path": "dist/index.js", |
| 49 | + "import": "*", |
| 50 | + "limit": "2.75 kB" |
| 51 | + }, |
| 52 | + { |
| 53 | + "name": "Signal", |
| 54 | + "path": "dist/index.js", |
| 55 | + "import": "{ signal }", |
| 56 | + "limit": "1.35 kB" |
| 57 | + } |
| 58 | +] |
| 59 | +``` |
| 60 | + |
| 61 | +Brotli is size-limit's default compression, so entries need no flag and no suffix in the name — the `(Gzip)`/`(Brotli)` suffixes appear only in the doubled variant below. |
| 62 | + |
| 63 | +For tree-shakeable libraries, measure several import subsets of the same dist file (minimal, popular, all publics via `import: "*"`) — see the `tree-shaking` skill. A regression then shows up as a specific entry blowing its limit, and the delta between subsets is the size of the subsystem being measured as droppable. |
| 64 | + |
| 65 | +## Noise, and how limits are pinned |
| 66 | + |
| 67 | +Brotli output is noisy at small sizes: an unrelated one-line change can move the compressed size by a few bytes in either direction, and deciding from that noise is hard. Two mechanisms compensate: |
| 68 | + |
| 69 | +**Limits are pinned on a 0.05 kB step.** Take the measured size and round it **up to the next multiple of 50 bytes** — no extra headroom on top. The step absorbs compression jitter, so a limit only moves when the code actually grew. Values under 1000 B are written in bytes (`600 B`), the rest in kB (`4.35 kB`). |
| 70 | + |
| 71 | +**Optional Gzip twins.** Gzip is noisier-proof than Brotli at small sizes, so a Gzip entry for the same bundle gives a second, steadier signal when a Brotli number moves ambiguously. This doubling is opt-in — only for projects that track size strictly. When used, each entry exists twice, `<name> (Gzip)` with `"gzip": true` and `<name> (Brotli)` without it, so both numbers are visible for the same bundle: |
| 72 | + |
| 73 | +```json |
| 74 | +[ |
| 75 | + { |
| 76 | + "name": "Signal (Gzip)", |
| 77 | + "gzip": true, |
| 78 | + "path": "dist/index.js", |
| 79 | + "import": "{ signal }", |
| 80 | + "limit": "1.45 kB" |
| 81 | + }, |
| 82 | + { |
| 83 | + "name": "Signal (Brotli)", |
| 84 | + "path": "dist/index.js", |
| 85 | + "import": "{ signal }", |
| 86 | + "limit": "1.35 kB" |
| 87 | + } |
| 88 | +] |
| 89 | +``` |
| 90 | + |
| 91 | +## Keeping limits current |
| 92 | + |
| 93 | +**After a change that can affect the output, rebuild and re-pin the limits.** How often depends on the user's workflow — at most after every edit, at minimum before every commit run the build and update the configs. In a monorepo, base packages are bundled into dependents at measure time, so a change in a base package moves the numbers of the whole chain, not just its own package — rebuild the chain first: |
| 94 | + |
| 95 | +```sh |
| 96 | +pnpm -r --filter './packages/*' build # sequentially, never --parallel |
| 97 | +# then, per package with a .size-limit.json — via the package script if it has one: |
| 98 | +pnpm test:size # or: npx size-limit --json |
| 99 | +``` |
| 100 | + |
| 101 | +Re-pin each changed entry from the measured size using the 0.05 kB rounding rule above. A limit that went **up** is a size regression — surface it to the user rather than silently raising the number. |
0 commit comments