Webpack Build Performance
I want to improve the speed with which webpack builds client side code. I am going over old frontend code that I wrote to improve it, and I am tired of waiting a few seconds for the build to complete.
References
Notes
- The following best practices should help, whether you're running build scripts in development or production.
- Stay up to date with the latest webpack version
- Staying up-to-date with Node.js can also help with performance. Also, keeping your package manager (
npm
oryarn
) up-to-date can also help.
- Staying up-to-date with Node.js can also help with performance. Also, keeping your package manager (
- Apply loaders to the minimal number of modules necessary. Instead of:
module.exports = {
//...
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
},
],
},
};
- Use the
include
field to only apply the loader modules that actually need to be transformed by it:
const path = require('path');
module.exports = {
//...
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
loader: 'babel-loader',
},
],
},
};
- Each additional loader/plugin has a bootup time. Try to use as few tools as possible.
- The following steps can increase resolving speed:
- Minimize the number of items in
resolve.modules
,resolve.extensions
,resolve.mainFiles
,resolve.descriptionFiles
, as they increase the number of filesystem calls. - Set
resolve.symlinks: false
if you don't use symlinks (e.g.npm link
oryarn link
) - Set
resolve.cacheWithContext: false
if you use custom resolving plugins, that are not context specific.
- Minimize the number of items in
- Use the
DllPlugin
to move code that is changed less often into a separate compilation. This will improve the application's compilation speed, although it does increase complexity of the build process. - Decrease the size of the compilation process to increase performance. Try to keep chunks small.
- Use fewer/smaller libraries.
- Use the
SplitChunksPlugin
in Multi-Page Applications. - Use the
SplitChunksPlugin
inasync
mode in Multi-Page Applications. - Remove unused code.
- Only compile the part of the code you are currently developing on.
- The
thread-loader
can be used to offload expensive loaders to a worker pool. - Use
cache
option in webpack's configuration. Clear cache directory onpostintall
inpackage.json
- It is possible to shorten build times by removing
ProgressPlugin
from webpack's configuration. - The following steps are especially useful in production:
- Source maps are really expensive. Do you really need them?
- The following tools have certain problems that can degrade build performance:
- Babel
- Minimize the number of preset/plugins.
- Typescript
- Use the
fork-ts-checker-webpack-plugin
for type checking in a separate process. - Configure loaders to skip typechecking
- Use the
ts-loader
inhappyPackMode: true
/transpileOnly: tue
- Use the
- Sass
node-sass
has a bug which blocks threads from the Node.js thread pool. When using it with thethread-loader
setworkerParallelJobs: 2
Comments
You have to be logged in to add a comment
User Comments
There are currently no comments for this article.