Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 could tell you not only library size. With
--why
argument it cantell you why your library has this size and show real cost of all your
internal dependencies.
Who Uses Size Limit
25% of the size.
25% of the size.
33% of the size.
90% of the size.
How It Works
You can find more examples in Size Limit: Make the Web lighter article.
To be really specific, Size Limit creates an empty webpack project in memory.
Then, it adds your library as a dependency to the project and calculates
the real cost of your libraries, including all dependencies, webpack’s polyfills
for process, etc.
Usage
First, install
size-limit
:Add
size-limit
section topackage.json
andsize
script:The
path
option:to npm (usually the same value as the
main
field in thepackage.json
);webpack: false
(see theApplications section).
Here’s how you can get the size for your current project:
If your project size starts to look bloated,
run Webpack Bundle Analyzer
for analysis:
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 in
your
package.json
:"size-limit": [ { + "limit": "9 KB", "path": "index.js" } ],
Add the
size
script to your test suite:If you don’t have a continuous integration service running, don’t forget
to add one — start with Travis CI.
Config
Size Limits supports 3 ways to define config.
size-limit
section topackage.json
:or separated
.size-limit
config file:or more flexible
.size-limit.js
config file:Each section in config could have options:
It could be a path
"index.js"
, a pattern"dist/app-*.js"
or an array
["index.js", "dist/app-*.js"]
.path
option. It should be a stringwith a number and unit (
100 B
,10 KB
, etc).if you have multiple sections.
false
will disable webpack.false
will disable gzip compression.Applications
Webpack inside Size Limit is very useful for small open source library.
But if you want to use Size Limit for application, not open source library, you
could already have webpack to make bundle.
In this case you can disable internal webpack:
"size-limit": [ { "limit": "300 KB", + "webpack": false, "path": "public/app-*.js" } ],
JavaScript API
Comparison with
bundlesize
Main difference between Size Limit and
bundlesize
, that Size Limit useswebpack to build bundle. It has more accurate result and can show you
what and why causes the bloat.
--why
mode to run Webpack Bundle Analyzer — this way,you can see what went wrong in a nice graphical representation.
of libraries bloat — unexpected huge dependency.
process
or
path
usage, when webpack will add big unnecessary polyfill.to CI resources.