The CommonsChunkPlugin
is an opt-in feature that creates a separate file (known as a chunk), consisting of common modules shared between multiple entry points.
The CommonsChunkPlugin has been removed in webpack v4 legato. To learn how chunks are treated in the latest version, check out the SplitChunksPlugin.
By separating common modules from bundles, the resulting chunked file can be loaded once initially, and stored in cache for later use. This results in page speed optimizations as the browser can quickly serve the shared code from cache, rather than being forced to load a larger bundle whenever a new page is visited.
new webpack.optimize.CommonsChunkPlugin(options);
Options
{
name: string,
names: string[],
filename: string,
minChunks: number|Infinity|function(module, count) => boolean,
chunks: string[],
children: boolean,
deepChildren: boolean,
async: boolean|string,
minSize: number,
}
Examples Commons chunk for entries
Generate an extra chunk, which contains common modules shared between entry points.
new webpack.optimize.CommonsChunkPlugin({
name: 'commons',
filename: 'commons.js',
});
You must load the generated chunk before the entry point:
<script src="commons.js" charset="utf-8"></script>
<script src="entry.bundle.js" charset="utf-8"></script>
Explicit vendor chunk
Split your code into vendor and application.
module.exports = {
entry: {
vendor: ['jquery', 'other-lib'],
app: './entry',
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: Infinity,
}),
],
};
<script src="vendor.js" charset="utf-8"></script>
<script src="app.js" charset="utf-8"></script>
tip
In combination with long term caching you may need to use the ChunkManifestWebpackPlugin
to avoid the vendor chunk changes. You should also use records to ensure stable module ids, e.g. using NamedModulesPlugin
or HashedModuleIdsPlugin
.
With Code Splitting, multiple child chunks of an entry chunk can have common dependencies. To prevent duplication these can be moved into the parent. This reduces overall size, but does have a negative effect on the initial load time. If it is expected that users will need to download many sibling chunks, i.e. children of the entry chunk, then this should improve load time overall.
new webpack.optimize.CommonsChunkPlugin({
children: true,
});
Extra async commons chunk
Similar to the above one, but instead of moving common modules into the parent (which increases initial load time) a new async-loaded additional commons chunk is used. This is automatically downloaded in parallel when the additional chunk is downloaded.
new webpack.optimize.CommonsChunkPlugin({
name: 'app',
names: ['app', 'subPageA'],
children: true,
async: true,
minChunks: 3,
});
Passing the minChunks
property a function
You also have the ability to pass the minChunks
property a function. This function is called by the CommonsChunkPlugin
and calls the function with module
and count
arguments.
The module
argument represents each module in the chunks you have provided via the name
/names
property. module
has the shape of a NormalModule, which has two particularly useful properties for this use case:
module.context
: The directory that stores the file. For example: '/my_project/node_modules/example-dependency'
module.resource
: The name of the file being processed. For example: '/my_project/node_modules/example-dependency/index.js'
The count
argument represents how many chunks the module
is used in.
This option is useful when you want to have fine-grained control over how the CommonsChunk algorithm determines where modules should be moved to.
new webpack.optimize.CommonsChunkPlugin({
name: 'my-single-lib-chunk',
filename: 'my-single-lib-chunk.js',
minChunks: function (module, count) {
return module.resource && /somelib/.test(module.resource) && count === 3;
},
});
As seen above, this example allows you to move only one lib to a separate file if and only if all conditions are met inside the function.
This concept may be used to obtain implicit common vendor chunks:
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
return module.context && module.context.includes('node_modules');
},
});
Manifest file
To extract the webpack bootstrap logic into a separate file, use the CommonsChunkPlugin
on a name
which is not defined as entry
. Commonly the name manifest
is used. See the caching guide for details.
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
minChunks: Infinity,
});
Combining implicit common vendor chunks and manifest file
Since the vendor
and manifest
chunk use a different definition for minChunks
, you need to invoke the plugin twice:
[
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
return module.context && module.context.includes('node_modules');
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
minChunks: Infinity,
}),
];
More Examples
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4