Skip to content

Instantly share code, notes, and snippets.

@sokra
Last active May 23, 2024 16:10
Show Gist options
  • Save sokra/1522d586b8e5c0f5072d7565c2bee693 to your computer and use it in GitHub Desktop.
Save sokra/1522d586b8e5c0f5072d7565c2bee693 to your computer and use it in GitHub Desktop.

RIP CommonsChunkPlugin

webpack 4 removes the CommonsChunkPlugin in favor of two new options (optimization.splitChunks and optimization.runtimeChunk). Here is how it works.

Defaults

By default it now does some optimizations that should work great for most users.

Note: The defaults only affect on-demand chunks, because changing initial chunks would affect the script tags in the HTML. If you can handle this (i. e. when generating the script tags from the entrypoints in stats) you can enable these default optimizations for initial chunks too with optimization.splitChunks.chunks: "all".

webpack automatically splits chunks based on these conditions:

  • New chunk can be shared OR modules are from the node_modules folder
  • New chunk would be bigger than 30kb (before min+gz)
  • Maximum number of parallel request when loading chunks on demand would be lower or equal to 5
  • Maximum number of parallel request at initial page load would be lower or equal to 3

When trying to fullfill the last two conditions, bigger chunks are preferred.

Let's take a look at some examples.

Example 1

// entry.js
import("./a");
// a.js
import "react";
// ...

Result: A separate chunk would be created containing react. At the import call this chunk is loaded in parallel to the original chunk containing ./a.

Why:

  • Condition 1: The chunk contains modules from node_modules
  • Condition 2: react is bigger than 30kb
  • Condition 3: Number of parallel requests at the import call is 2
  • Condition 4: Doesn't affect request at initial page load

Why does this make sense?

react probably doesn't change very often compared to your application code. By moving it into a separate chunk this chunk can be cached separately from your app code (assuming you are using Long Term Caching: chunkhash, records, Cache-Control).

Example 2

// entry.js
import("./a");
import("./b");
// a.js
import "./helpers"; // helpers is 40kb in size
// ...
// b.js
import "./helpers";
import "./more-helpers"; // more-helpers is also 40kb in size
// ...

Result: A separate chunk would be created containing ./helpers and all dependencies of it. At the import calls this chunk is loaded in parallel to the original chunks.

Why:

  • Condition 1: The chunk is shared between both import calls
  • Condition 2: helpers is bigger than 30kb
  • Condition 3: Number of parallel requests at the import calls is 2
  • Condition 4: Doesn't affect request at initial page load

Why does this make sense?

Putting the helpers code into each chunk may means it need to downloaded twice by the user. By using a separate chunk it's only downloaded once. Actually it's a tradeoff, because now we pay the cost of an additional request. That's why there is a minimum size of 30kb.


With optimizations.splitChunks.chunks: "all" the same would happend for initial chunks. Chunks can even be shared between entrypoints and on-demand loading.

Configuration

For these people that like to have more control over this functionality, there are a lot of options to fit it to your needs.

Disclaimer: Don't try to optimize manually without measuring. The defaults are choosen to fit best practices of web performance.

Cache Groups

The optimization assigns modules to cache groups.

The defaults assigns all modules from node_modules to a cache group called vendors and all modules duplicated in at least 2 chunks to a change group default.

A module can be assigned to multiple cache groups. The optimization then prefers the cache group with the higher priority (priority option) or that one that forms bigger chunks.

Conditions

Modules from the same chunks and cache group will form a new chunk when all conditions are fullfilled.

There are 4 options to configure the conditions:

  • minSize (default: 30000) Minimum size for a chunk.
  • minChunks (default: 1) Minimum number of chunks that share a module before splitting
  • maxInitialRequests (default 3) Maximum number of parallel requests at an entrypoint
  • maxAsyncRequests (default 5) Maximum number of parallel requests at on-demand loading

Naming

To control the chunk name of the split chunk the name option can be used.

Note: When assigning equal names to different split chunks they are merged together. This can be used i. e. to put all vendor modules into a single chunk shared by all other entrypoints/splitpoints, but I don't recommend doing so. This can lead to more code downloaded than needed.

The magic value true automatically chooses a name based on chunks and cache group key. Elsewise a string or function can be passed.

When the name matches an entrypoint name, the entrypoint is removed.

Select chunks

With the chunks option the selected chunks can be configured. There are 3 values possible "initial", "async" and "all". When configured the optimization only selects initial chunks, on-demand chunks or all chunks.

The option reuseExistingChunk allows to reuse existing chunks instead of creating a new one when modules match exactly.

This can be controlled per cache group.

Select modules

The test option controls which modules are selected by this cache group. Omitting it selects all modules. It can be a RegExp, string or function.

It can match the absolute module resource path or chunk names. When a chunk name is matched, all modules in this chunk are selected.

Configurate cache groups

This is the default configuration:

splitChunks: {
	chunks: "async",
	minSize: 30000,
	minChunks: 1,
	maxAsyncRequests: 5,
	maxInitialRequests: 3,
	name: true,
	cacheGroups: {
		default: {
			minChunks: 2,
			priority: -20
			reuseExistingChunk: true,
		},
		vendors: {
			test: /[\\/]node_modules[\\/]/,
			priority: -10
		}
	}
}

By default cache groups inherit options from splitChunks.*, but test, priority and reuseExistingChunk can only be configured on cache group level.

cacheGroups is an object where keys are cache group keys and values are options:

Otherwise all options from the options listed above are possible: chunks, minSize, minChunks, maxAsyncRequests, maxInitialRequests, name.

To disable the default groups pass false: optimization.splitChunks.cacheGroups.default: false

The priority of the default groups are negative so any custom cache group takes higher priority (default 0).

Here are some examples and their effect:

splitChunks: {
	cacheGroups: {
		commons: {
			name: "commons",
			chunks: "initial",
			minChunks: 2
		}
	}
}

Create a commons chunk, which includes all code shared between entrypoints.

Note: This downloads more code than neccessary.

splitChunks: {
	cacheGroups: {
		commons: {
			test: /[\\/]node_modules[\\/]
			name: "vendors",
			chunks: "all"
		}
	}
}

Create a vendors chunk, which includes all code from node_modules in the whole application.

Note: This downloads more code than neccessary.

optimization.runtimeChunk

optimization.runtimeChunk: true adds an additonal chunk to each entrypoint containing only the runtime.

@IanVS
Copy link

IanVS commented May 9, 2018

I'm really struggling right now to duplicate the behavior that CommonsChunkPlugin gave for injecting code into all entries, by giving it a name which was the same as an entry point. It seems the behavior now for SplitChunksPlugin is to throw away the entry?! @simonjoom, I'm basically trying to do the same as you, were you able to figure it out?

@arthow4n
Copy link

Here's a side-by-side guide for folks trying to reproduce the "vendor bundle" chunking behavior in Webpack 4, this worked for me.
The key point is that you can pass function into chunks and test (undocumented in the doc of SplitChunksPlugin). After knowing this, all you need is insert a function with debugger statement into it, trial-and-error, look at the output of webpack-bundle-analyzer, and find out what works for you.
I couldn't figure this out without reading the webpack config schema. https://github.com/webpack/webpack/blob/master/schemas/WebpackOptions.json

@budarin might be interested in this?

Good Old Webpack 3

// vendor.js
import moment from 'moment';
moment.locale('zh-tw');
// application.js
console.log('application');
<!-- index.html -->
<script src="vendor.js"></script>
<script src="application.js"></script>
// webpack.config.js
module.exports = {
  entry: {
    vendor: 'src/vendor.js',
    application: 'src/application.js',
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      chunks: ['application'],
      minChunks: (module) => {
        const context = module.context;
        return context && context.indexOf('node_modules') >= 0;
      },
    }),
  ],
};

Upgrading to Webpack 4

The vendor bundle doesn't have to be modified.

// vendor.js
import moment from 'moment';
moment.locale('zh-tw');

You must import the vendor bundle explicitly in Webpack 4 even if you have placed both of the bundle script tags in order on the HTML.

// application.js
import './vendor';
console.log('application');

The HTML doesn't have to be modified.

<!-- index.html -->
<script src="vendor.js"></script>
<script src="application.js"></script>

Update your Webpack config:

// webpack.config.js
module.exports = {
  entry: {
    vendor: 'src/vendor.js',
    application: 'src/application.js',
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        vendor: {
          name: 'vendor',
          reuseExistingChunk: true,
          chunks: chunk => ['vendor', 'application'].includes(chunk.name),
          test: module => /[\\/]node_modules[\\/]/.test(module.context),
          minChunks: 1,
          minSize: 0,
        },
      },
    },
  },
};

@petrdanecek
Copy link

Regards the vendor.js without the vendor.css (the node_module related CSS is not extracted to independent vendor.css, but the CSS is kept in the app.css instead of), this would be the solution:

  optimization: {
    runtimeChunk: 'single',
    splitChunks: {
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/].*(?<!\.(sa|sc|c)ss)$/,
          name: 'vendor',
          chunks: 'all'
        }
      }
    }
  },

Test matches all files in node_modules expect the SCSS|SASS|CSS. Both app and vendor styles are extracted into app.css.

@magicdawn
Copy link

typo
image

@ufo1792
Copy link

ufo1792 commented Jan 8, 2019

i tried many times.. but frustrated..
i'm poor at english.. how can i solve this code? very very thank you
very simple code..
i want to make file name is 'vendor.bundle.js' please help
i know that using optimization, splitchunks because difference of webpack versions but i tried many but i'm poor in this.
anyone please help me.....

plugins:[
new webpack.optimize.CommonsChunkPlugin('vendor','vendor.bundle.js')
i think this code need to be modyfied

default

@vsTianhao
Copy link

vsTianhao commented May 10, 2019

There's a mistake.

无标题

comma error

@hanweiit007
Copy link

hanweiit007 commented Sep 18, 2019

I would like to ask how to modify my configuration.
CommonsChunkPlugin change to splitChunks.

entry: {
	main: ['./app/js/common/vendor.js', './app/js/common/common.js', './app/index.js'],
	ie8: './app/js/polyfills/ie8-polyfill.js',
	ie9: './app/js/polyfills/ie9-polyfill.js'
},
new webpack.optimize.CommonsChunkPlugin({
	name: 'app-common',
	chunks: chunksForAppCommon,
	children: false,
	minChunks: function(module, count) {
	        if (module.context &&
		        module.context.indexOf('node_modules') !== -1 &&
		        module.context.indexOf('i18n-messages') === -1 && count > 0) {
		        console.log("app-common 1: " + count + "\t" + module.resource);
		        return true;
	        }
	        if (!module.resource) {
		        console.log("app-common 2: " + count + "\t" + module.resource + "\t" + module.context);
		        return true;
	        }
	        if (module.context &&
		        module.context.indexOf('i18n-messages') === -1 && count > 0 && count === chunksForAppCommon.length) {
		        console.log("app-common 3: " + count + "\t" + module.resource);
		        return true;
	        }
	        console.log("app-common 9: " + count + "\t" + module.resource);
	        return false;
        }
}),
new webpack.optimize.CommonsChunkPlugin({
	name: 'runtime',
	minChunks: Infinity
}),

@carldiegelmann
Copy link

carldiegelmann commented Dec 19, 2019

How can I write this Chunk configuration with Webpack 4:

config.plugins.push( new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', // make sure that only the explicitly defined entries are included in the bundle minChunks: Infinity, }) ); config.plugins.push( new webpack.optimize.CommonsChunkPlugin({ name: 'ct-core', chunks: Object.keys(moduleEntries), minChunks: 2, }) );

@tonix-tuft
Copy link

Where can I find a guide and or tutorial explaining all this stuff but more in detail and in a friendlier way? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment