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.

Date Created:
2 462

References



Notes


  • The following best practices should help, whether you're running build scripts in development or production.
  1. Stay up to date with the latest webpack version
    1. Staying up-to-date with Node.js can also help with performance. Also, keeping your package manager (npm or yarn) up-to-date can also help.
  2. 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',
},
],
},
};
  1. Each additional loader/plugin has a bootup time. Try to use as few tools as possible.
  2. The following steps can increase resolving speed:
    1. Minimize the number of items in resolve.modules, resolve.extensions, resolve.mainFiles, resolve.descriptionFiles, as they increase the number of filesystem calls.
    2. Set resolve.symlinks: false if you don't use symlinks (e.g. npm link or yarn link)
    3. Set resolve.cacheWithContext: false if you use custom resolving plugins, that are not context specific.
  3. 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.
  4. Decrease the size of the compilation process to increase performance. Try to keep chunks small.
    1. Use fewer/smaller libraries.
    2. Use the SplitChunksPlugin in Multi-Page Applications.
    3. Use the SplitChunksPlugin in async mode in Multi-Page Applications.
    4. Remove unused code.
    5. Only compile the part of the code you are currently developing on.
  5. The thread-loader can be used to offload expensive loaders to a worker pool.
  6. Use cache option in webpack's configuration. Clear cache directory on postintall in package.json
  7. It is possible to shorten build times by removing ProgressPlugin from webpack's configuration.
  8. The following steps are especially useful in production:
    1. Source maps are really expensive. Do you really need them?
  9. The following tools have certain problems that can degrade build performance:
    1. Babel
      1. Minimize the number of preset/plugins.
    2. Typescript
      1. Use the fork-ts-checker-webpack-plugin for type checking in a separate process.
      2. Configure loaders to skip typechecking
      3. Use the ts-loader in happyPackMode: true / transpileOnly: tue
    3. Sass
      1. node-sass has a bug which blocks threads from the Node.js thread pool. When using it with the thread-loader set workerParallelJobs: 2


Comments

You have to be logged in to add a comment

User Comments

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language

Insert Chart

ESC

Use the search box below

Upload Previous Version of Article State

ESC