JSPM

  • Created
  • Published
  • Downloads 446780
  • Score
    100M100P100Q231162F
  • License MIT

Show package size and return error if it is bigger than limits allow

Package Exports

  • size-limit

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (size-limit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Size Limit

Size Limit is a tool to prevent JavaScript libraries bloat. With it, you know exactly for how many kilobytes your JS library increases the user bundle.

You can add Size Limit to your continuous integration service (such as Travis CI) and set the limit. If you accidentally add a massive dependency, Size Limit will throw an error.

Size Limit example

Sponsored by Evil Martians

Usage

First, install size-limit:

$ npm install --save-dev size-limit

Here's how you can get the size for your current project:

$ ./node_modules/bin/size-limit

  Package size: 8.46 KB
  With all dependencies, minified and gzipped

If your project size starts to look bloated, run Webpack Bundle Analyzer for analysis:

./node_modules/bin/size-limit --why

Bundle Analyzer example

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 a limit when adding the script to package.json:

  "scripts": {
    "test": "jest && eslint .",
+    "size": "size-limit 9KB"
  }

Add the size script to your test suite:

  "scripts": {
-    "test": "jest && eslint .",
+    "test": "jest && eslint . && npm run size",
    "size": "size-limit 9KB"
  }

If you don't have a continuous integration service running, don’t forget to add one — start with Travis CI.

JavaScript API

const getSize = require('size-limit')

const index = path.join(__dirname, 'index.js')
const extra = path.join(__dirname, 'extra.js')

getSize([index, extra]).then(size => {
  if (size > 1 * 1024 * 1024) {
    console.error('Project is now larger than 1MB!')
  }
})

Comparison with bundlesize

Size Limit is a full-featured library that helps you to get into the detail of what and why causes the bloat; it also works offline.

In short,

  1. Size Limit has the --why mode to run Webpack Bundle Analyzer — this way, you can see what went wrong in a nice graphical representation.
  2. Size Limit doesn’t use any external APIs and works offline.