Originally, chunks (and modules imported inside them) were connected by a parent-child relationship in the internal webpack graph. The CommonsChunkPlugin
was used to avoid duplicated dependencies across them, but further optimizations were not possible.
Since webpack v4, the CommonsChunkPlugin
was removed in favor of optimization.splitChunks
.
Out of the box SplitChunksPlugin
should work well for most users.
By default it only affects on-demand chunks, because changing initial chunks would affect the script tags the HTML file should include to run the project.
webpack will automatically split chunks based on these conditions:
node_modules
folderWhen trying to fulfill the last two conditions, bigger chunks are preferred.
webpack provides a set of options for developers that want more control over this functionality.
The default configuration was chosen to fit web performance best practices, but the optimal strategy for your project might differ. If you're changing the configuration, you should measure the impact of your changes to ensure there's a real benefit.
optimization.splitChunks
This configuration object represents the default behavior of the SplitChunksPlugin
.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { chunks: 'async', minSize: 30000, maxSize: 0, minChunks: 1, maxAsyncRequests: 5, maxInitialRequests: 3, automaticNameDelimiter: '~', automaticNameMaxLength: 30, name: true, cacheGroups: { vendors: { test: /[\\/]node_modules[\\/]/, priority: -10 }, default: { minChunks: 2, priority: -20, reuseExistingChunk: true } } } } };
splitChunks.automaticNameDelimiter
string
By default webpack will generate names using origin and name of the chunk (e.g. vendors~main.js
). This option lets you specify the delimiter to use for the generated names.
splitChunks.automaticNameMaxLength
number = 109
Allows setting a maximum character count for chunk names that are generated by the SplitChunksPlugin
.
splitChunks.chunks
function (chunk)
string
This indicates which chunks will be selected for optimization. When a string is provided, valid values are all
, async
, and initial
. Providing all
can be particularly powerful, because it means that chunks can be shared even between async and non-async chunks.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { // include all types of chunks chunks: 'all' } } };
Alternatively, you may provide a function for more control. The return value will indicate whether to include each chunk.
module.exports = { //... optimization: { splitChunks: { chunks (chunk) { // exclude `my-excluded-chunk` return chunk.name !== 'my-excluded-chunk'; } } } };
You can combine this configuration with the HtmlWebpackPlugin. It will inject all the generated vendor chunks for you.
splitChunks.maxAsyncRequests
number
Maximum number of parallel requests when on-demand loading.
splitChunks.maxInitialRequests
number
Maximum number of parallel requests at an entry point.
splitChunks.minChunks
number
Minimum number of chunks that must share a module before splitting.
splitChunks.minSize
number
Minimum size, in bytes, for a chunk to be generated.
splitChunks.maxSize
number
Using maxSize
(either globally optimization.splitChunks.maxSize
per cache group optimization.splitChunks.cacheGroups[x].maxSize
or for the fallback cache group optimization.splitChunks.fallbackCacheGroup.maxSize
) tells webpack to try to split chunks bigger than maxSize
bytes into smaller parts. Parts will be at least minSize
(next to maxSize
) in size. The algorithm is deterministic and changes to the modules will only have local impact. So that it is usable when using long term caching and doesn't require records. maxSize
is only a hint and could be violated when modules are bigger than maxSize
or splitting would violate minSize
.
When the chunk has a name already, each part will get a new name derived from that name. Depending on the value of optimization.splitChunks.hidePathInfo
it will add a key derived from the first module name or a hash of it.
maxSize
option is intended to be used with HTTP/2 and long term caching. It increases the request count for better caching. It could also be used to decrease the file size for faster rebuilding.
maxSize
takes higher priority thanmaxInitialRequest/maxAsyncRequests
. Actual priority ismaxInitialRequest/maxAsyncRequests < maxSize < minSize
.
splitChunks.name
boolean = true
function (module, chunks, cacheGroupKey) => string
string
Also available for each cacheGroup: splitChunks.cacheGroups.{cacheGroup}.name
.
The name of the split chunk. Providing true
will automatically generate a name based on chunks and cache group key.
Providing a string or a function allows you to use a custom name. Specifying either a string or a function that always returns the same string will merge all common modules and vendors into a single chunk. This might lead to bigger initial downloads and slow down page loads.
If you choose to specify a function, you may find the chunk.name
and chunk.hash
properties (where chunk
is an element of the chunks
array) particularly useful in choosing a name for your chunk.
If the splitChunks.name
matches an entry point name, the entry point will be removed.
It is recommended to set
splitChunks.name
tofalse
for production builds so that it doesn't change names unnecessarily.
main.js
import _ from 'lodash'; console.log(_.join(['Hello', 'webpack'], ' '));
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { commons: { test: /[\\/]node_modules[\\/]/, // cacheGroupKey here is `commons` as the key of the cacheGroup name(module, chunks, cacheGroupKey) { const moduleFileName = module.identifier().split('/').reduceRight(item => item); const allChunksNames = chunks.map((item) => item.name).join('~'); return `${cacheGroupKey}-${allChunksNames}-${moduleFileName}`; }, chunks: 'all' } } } } };
Running webpack with following splitChunks
configuration would also output a chunk of the group common with next name: commons-main-lodash.js.e7519d2bb8777058fa27.js
(hash given as an example of real world output).
When assigning equal names to different split chunks, all vendor modules are placed into a single shared chunk, though it's not recommend since it can result in more code downloaded.
splitChunks.cacheGroups
Cache groups can inherit and/or override any options from splitChunks.*
; but test
, priority
and reuseExistingChunk
can only be configured on cache group level. To disable any of the default cache groups, set them to false
.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { default: false } } } };
splitChunks.cacheGroups.{cacheGroup}.priority
number
A module can belong to multiple cache groups. The optimization will prefer the cache group with a higher priority
. The default groups have a negative priority to allow custom groups to take higher priority (default value is 0
for custom groups).
splitChunks.cacheGroups.{cacheGroup}.reuseExistingChunk
boolean
If the current chunk contains modules already split out from the main bundle, it will be reused instead of a new one being generated. This can impact the resulting file name of the chunk.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { vendors: { reuseExistingChunk: true } } } } };
splitChunks.cacheGroups.{cacheGroup}.test
function (module, chunk) => boolean
RegExp
string
Controls which modules are selected by this cache group. Omitting it selects all modules. It can match the absolute module resource path or chunk names. When a chunk name is matched, all modules in the chunk are selected.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { vendors: { test(module, chunks) { //... return module.type === 'javascript/auto'; } } } } } };
splitChunks.cacheGroups.{cacheGroup}.filename
string
Allows to override the filename when and only when it's an initial chunk. All placeholders available in output.filename
are also available here.
This option can also be set globally in
splitChunks.filename
, but this isn't recommended and will likely lead to an error ifsplitChunks.chunks
is not set to'initial'
. Avoid setting it globally.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { vendors: { filename: '[name].bundle.js' } } } } };
splitChunks.cacheGroups.{cacheGroup}.enforce
boolean = false
Tells webpack to ignore splitChunks.minSize
, splitChunks.minChunks
, splitChunks.maxAsyncRequests
and splitChunks.maxInitialRequests
options and always create chunks for this cache group.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { vendors: { enforce: true } } } } };
// index.js import('./a'); // dynamic import
// 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:
node_modules
react
is bigger than 30kbWhat's the reasoning behind this? react
probably won't change as often as your application code. By moving it into a separate chunk this chunk can be cached separately from your app code (assuming you are using chunkhash, records, Cache-Control or other long term cache approach).
// entry.js // dynamic imports 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:
helpers
is bigger than 30kbPutting the content of helpers
into each chunk will result into its code being downloaded twice. By using a separate chunk this will only happen once. We pay the cost of an additional request, which could be considered a tradeoff. That's why there is a minimum size of 30kb.
Create a commons
chunk, which includes all code shared between entry points.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { commons: { name: 'commons', chunks: 'initial', minChunks: 2 } } } } };
This configuration can enlarge your initial bundles, it is recommended to use dynamic imports when a module is not immediately needed.
Create a vendors
chunk, which includes all code from node_modules
in the whole application.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { commons: { test: /[\\/]node_modules[\\/]/, name: 'vendors', chunks: 'all' } } } } };
This might result in a large chunk containing all external packages. It is recommended to only include your core frameworks and utilities and dynamically load the rest of the dependencies.
Create a custom vendor
chunk, which contains certain node_modules
packages matched by RegExp
.
webpack.config.js
module.exports = { //... optimization: { splitChunks: { cacheGroups: { vendor: { test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/, name: 'vendor', chunks: 'all', } } } } };
This will result in splitting
react
andreact-dom
into a separate chunk. If you're not sure what packages have been included in a chunk you may refer to Bundle Analysis section for details.
© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://v4.webpack.js.org/plugins/split-chunks-plugin