MCPcopy
hub / github.com/ai/size-limit

github.com/ai/size-limit @12.1.0 sqlite

repository ↗ · DeepWiki ↗ · release 12.1.0 ↗
146 symbols 399 edges 177 files 0 documented · 0%
README

Size Limit Cult Of Martians

Size Limit logo by Anton Lovchikov

Size Limit is a performance budget tool for JavaScript. It checks every commit on CI, calculates the real cost of your JS for end-users and throws an error if the cost exceeds the limit.

  • ES modules and tree-shaking support.
  • Add Size Limit to GitHub Actions, Circle CI or another CI system to know if a pull request adds a massive dependency.
  • Modular to fit different use cases: big JS applications that use their own bundler or small npm libraries with many files.
  • Can calculate the time it would take a browser to download and execute your JS. Time is a much more accurate and understandable metric compared to the size in bytes. Additionally, you can customize time plugin via config for every check with network speed, latency and so on.
  • Calculations include all dependencies and polyfills used in your JS.

Size Limit CLI

With GitHub action Size Limit will post bundle size changes as a comment in pull request discussion.

Size Limit comment in pull request about bundle size changes

With --why, Size Limit can tell you why your library is of this size and show the real cost of all your internal dependencies. We are using Statoscope for this analysis.

Statoscope example

Sponsored by Evil Martians

Who Uses Size Limit

How It Works

  1. Size Limit contains a CLI tool, 3 plugins (file, webpack, time) and 3 plugin presets for popular use cases (app, big-lib, small-lib). A CLI tool finds plugins in package.json and loads the config.
  2. If you use the webpack plugin, Size Limit will bundle your JS files into a single file. It is important to track dependencies and webpack polyfills. It is also useful for small libraries with many small files and without a bundler.
  3. The webpack plugin creates an empty webpack project, adds your library and looks for the bundle size difference.
  4. The time plugin compares the current machine performance with that of a low-priced Android devices to calculate the CPU throttling rate.
  5. Then the time plugin runs headless Chrome (or desktop Chrome if it’s available) to track the time a browser takes to compile and execute your JS. Note that these measurements depend on available resources and might be unstable. See here for more details.

Usage

JS Applications

Suitable for applications that have their own bundler and send the JS bundle directly to a client (without publishing it to npm). Think of a user-facing app or website, like an email client, a CRM, a landing page or a blog with interactive elements, using React/Vue/Svelte lib or vanilla JS.

Show instructions

  1. Install the preset:

sh npm install --save-dev size-limit @size-limit/file

  1. Add the size-limit section and the size script to your package.json:

diff + "size-limit": [ + { + "path": "dist/app-*.js" + } + ], "scripts": { "build": "webpack ./webpack.config.js", + "size": "npm run build && size-limit", "test": "vitest && eslint ." }

  1. Here’s how you can get the size for your current project:

```sh $ npm run size

 Package size: 30.08 kB with all dependencies, minified and brotlied

```

  1. Now, let’s set the limit. Add 25% to the current total size and use that as the limit in your package.json:

diff "size-limit": [ { + "limit": "35 kB", "path": "dist/app-*.js" } ],

  1. Add the size script to your test suite:

diff "scripts": { "build": "webpack ./webpack.config.js", "size": "npm run build && size-limit", - "test": "vitest && eslint ." + "test": "vitest && eslint . && npm run size" }

  1. If you don’t have a continuous integration service running, don’t forget to add one — start with GitHub Actions.

JS Application and Time-based Limit

File size limit (in kB) is not the best way to describe your JS application cost for developers. Developers will compare the size of the JS bundle with the size of images. But browsers need much more time to parse 100 kB of JS than 100 kB of an image since JS compilers are very complex.

This is why Size Limit support time-based limit. It runs headless Chrome to track the time a browser takes to compile and execute your JS.

Show instructions

  1. Install the preset:

sh npm install --save-dev size-limit @size-limit/preset-app

  1. Add the size-limit section and the size script to your package.json:

diff + "size-limit": [ + { + "path": "dist/app-*.js" + } + ], "scripts": { "build": "webpack ./webpack.config.js", + "size": "npm run build && size-limit", "test": "vitest && eslint ." }

  1. Here’s how you can get the size for your current project:

```sh $ npm run size

 Package size: 30.08 kB with all dependencies, minified and brotlied
 Loading time: 602 ms   on slow 3G
 Running time: 214 ms   on Snapdragon 410
 Total time:   815 ms

```

  1. Now, let’s set the limit. Add 25% to the current total time and use that as the limit in your package.json:

diff "size-limit": [ { + "limit": "1 s", "path": "dist/app-*.js" } ],

  1. Add the size script to your test suite:

diff "scripts": { "build": "webpack ./webpack.config.js", "size": "npm run build && size-limit", - "test": "vitest && eslint ." + "test": "vitest && eslint . && npm run size" }

  1. If you don’t have a continuous integration service running, don’t forget to add one — start with GitHub Actions.

Big Libraries

JS libraries > 10 kB in size.

This preset includes headless Chrome, and will measure your lib’s execution time. You likely don’t need this overhead for a small 2 kB lib, but for larger ones the execution time is a more accurate and understandable metric that the size in bytes. Libraries like React are good examples for this preset.

Show instructions

  1. Install preset:

sh npm install --save-dev size-limit @size-limit/preset-big-lib

  1. Add the size-limit section and the size script to your package.json:

diff + "size-limit": [ + { + "path": "dist/react.production-*.js" + } + ], "scripts": { "build": "webpack ./scripts/rollup/build.js", + "size": "npm run build && size-limit", "test": "vitest && eslint ." }

  1. If you use ES modules you can test the size after tree-shaking with import option:

diff "size-limit": [ { "path": "dist/react.production-*.js", + "import": "{ createComponent }" } ],

  1. Here’s how you can get the size for your current project:

```sh $ npm run size

 Package size: 30.08 kB with all dependencies, minified and brotlied
 Loading time: 602 ms   on slow 3G
 Running time: 214 ms   on Snapdragon 410
 Total time:   815 ms

```

  1. Now, let’s set the limit. Add 25% to the current total time and use that as the limit in your package.json:

diff "size-limit": [ { + "limit": "1 s", "path": "dist/react.production-*.js" } ],

  1. Add a size script to your test suite:

diff "scripts": { "build": "rollup ./scripts/rollup/build.js", "size": "npm run build && size-limit", - "test": "vitest && eslint ." + "test": "vitest && eslint . && npm run size" }

  1. If you don’t have a continuous integration service running, don’t forget to add one — start with GitHub Actions.
  2. Add the library size to docs, it will help users to choose your project:

```diff # Project Name

 Short project description

 * **Fast.** 10% faster than competitor.
    • Small. 15 kB (minified and brotlied).
  • Size Limit controls the size. ```

Small Libraries

JS libraries < 10 kB in size.

This preset will only measure the size, without the execution time, so it’s suitable for small libraries. If your library is larger, you likely want the Big Libraries preset above. Nano ID or Storeon are good examples for this preset.

Show instructions

  1. First, install size-limit:

sh npm install --save-dev size-limit @size-limit/preset-small-lib

  1. Add the size-limit section and the size script to your package.json:

diff + "size-limit": [ + { + "path": "index.js" + } + ], "scripts": { + "size": "size-limit", "test": "vitest && eslint ." }

  1. Here’s how you can get the size for your current project:

```sh $ npm run size

 Package size: 177 B with all dependencies, minified and brotlied

```

  1. If your project size starts to look bloated, run --why for analysis:

sh npm run size -- --why

We use Statoscope as bundle analyzer.

  1. Now, let’s set the limit. Determine the current size of your library, add just a little bit (a kilobyte, maybe) and use that as the limit in your package.json:

diff "size-limit": [ { + "limit": "9 kB", "path": "index.js" } ],

  1. Add the size script to your test suite:

diff "scripts": { "size": "size-limit", - "test": "vitest && eslint ." + "test": "vitest && eslint . && npm run size" }

  1. If you don’t have a continuous integration service running, don’t forget to add one — start with GitHub Actions.
  2. Add the library size to docs, it will help users to choose your project:

```diff # Project Name

 Short project description

 * **Fast.** 10% faster than competitor.
    • Small. 500 bytes (minified and brotlied). No dependencies.
  • Size Limit controls the size. ```

Reports

Size Limit has a GitHub action that comments and rejects pull requests based on Size Limit output.

  1. Install and configure Size Limit as shown above.
  2. Add the following action inside .github/workflows/size-limit.yml
name: 'size'
on:
  pull_request:
    branches:
      - master
jobs:
  size:
    runs-on: ubuntu-latest
    env:
      CI_JOB_NUMBER: 1
    steps:
      - uses: actions/checkout@v1
      - uses: andresz1/size-limit-action@v1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

Config

Plugins and Presets

Plugins or plugin presets will be loaded automatically from package.json. For example, if you want to use @size-limit/webpack, you can just use npm install --save-dev @size-limit/webpack, or you can use our preset @size-limit/preset-big-lib.

Plugins:

  • @size-limit/file checks the size of files with Brotli (default), Gzip or without compression.
  • @size-limit/webpack adds your library to empty webpack project and prepares bundle file for file plugin.
  • @size-limit/webpack-why adds reports for webpack plugin about your library is of this size to show the cost of all your dependencies.
  • @size-limit/webpack-css adds css support for webpack plugin.
  • @size-limit/esbuild is like webpack plugin, but uses esbuild to be faster and use less space in node_modules

Extension points exported contracts — how you extend this code

Check (Interface)
(no doc)
packages/size-limit/index.d.ts
TimeOptions (Interface)
(no doc)
packages/size-limit/index.d.ts

Core symbols most depended-on inside this repo

has
called by 25
packages/size-limit/load-plugins.js
isValidFilename
called by 16
packages/esbuild-why/valid-filename.js
print
called by 14
packages/size-limit/create-reporter.js
getRunningTime
called by 11
packages/time/get-running-time.js
getReportName
called by 8
packages/esbuild-why/report.js
print
called by 6
packages/size-limit/create-help.js
hasArg
called by 5
packages/size-limit/run.js
toAbsolute
called by 5
packages/size-limit/get-config.js

Shape

Function 137
Class 4
Method 3
Interface 2

Languages

TypeScript100%

Modules by API surface

packages/size-limit/test/run.test.js14 symbols
packages/size-limit/get-config.js11 symbols
packages/webpack/index.js8 symbols
packages/size-limit/create-reporter.js8 symbols
packages/esbuild/index.js7 symbols
packages/size-limit/test/get-config.test.js6 symbols
packages/size-limit/load-plugins.js6 symbols
packages/size-limit/create-help.js6 symbols
packages/webpack/test/index.test.js4 symbols
packages/time/get-running-time.js4 symbols
packages/size-limit/run.js4 symbols
packages/size-limit/calc.js4 symbols

Dependencies from manifests, versioned

@logux/oxc-configs0.3.2 · 1×
@size-limit/esbuildworkspace:^ · 1×
@size-limit/esbuild-whyworkspace:^ · 1×
@size-limit/fileworkspace:^ · 1×
@size-limit/preset-appworkspace:^ · 1×
@size-limit/preset-big-libworkspace:^ · 1×
@size-limit/preset-small-libworkspace:^ · 1×
@size-limit/timeworkspace:^ · 1×
@size-limit/webpackworkspace:^ · 1×
@size-limit/webpack-cssworkspace:^ · 1×
@size-limit/webpack-whyworkspace:^ · 1×
@statoscope/webpack-plugin5.29.0 · 1×

For agents

$ claude mcp add size-limit \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact