Code Monkey home page Code Monkey logo

size-limit's Introduction

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

Size Limit could tell you not only library size. With --why argument it can tell you why your library has this size and show real cost of all your internal dependencies.

Bundle Analyzer example

To be really accurate, Size Limit create empty webpack project in memory. Then it adds your library as dependency to this project and calculate real cost of your libraries including all dependencies and webpack’s polyfills for process, etc.

Sponsored by Evil Martians

Who Uses Size Limit

Usage

First, install size-limit:

$ npm install --save-dev size-limit

Add sizeLimit section to package.json and size script:

+ "sizeLimit": [
+   {
+     "path": "index.js",
+     "limit": "9 KB"
+   }
+ ],
  "scripts": {
+   "size": "size-limit",
    "test": "jest && eslint ."
  }

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

$ npm run size

  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

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:

 "sizeLimit": [
    {
+     "limit": "9 KB",
      "path": "index.js"
    }
 ],

Add the size script to your test suite:

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

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

Main difference between Size Limit and bundlesize, that Size Limit uses webpack to build bundle. It has more accurate result and can show you what and why causes the bloat.

  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. Instead of bundlesize, Size Limit prevents the most popular source of libraries bloat — unexpected huge dependency.
  3. Also Size Limit prevents increasing library size because of wrong process or path usage, when webpack will add big unnecessary polyfill.
  4. Size Limit runs only on first CI job, so it is more respectful to CI resources.

size-limit's People

Contributors

ai avatar madyankin avatar maraisr avatar oliviertassinari avatar pgrekovich avatar yaroslav avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.