I understand the great work that was done on webpack 4. Specially on rewriting the code splitting plugin. However, and since it's still kinda new, I don't find good documentation on the new SplitChunksPlugin.
I struggle on the meaning of the terms chosen. For example:
chunks: there are 3 possible values "initial", "async" and "all". What does it mean? Initial chunks are the entries? Async the dynamic imported? all is the initial + async? If I use initial then my dynamic imported chunks won't leverage the code splitting? Eg. main.tsx dynamically imports about.tsx which does a normal import of lodash. Lodash wouldn't be extracted to the vendors bundle?
enforce: I see a lot of configs setting the enforce:true, what does it mean?
For a better context I'm posting an example of splitChunks configs.
optimization: {
splitChunks: {
cacheGroups: {
'commons': {
minChunks: 2,
chunks: 'all',
name: 'commons',
priority: 10,
enforce: true,
},
},
},
},
splitChunks.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 affect the resulting file name of the chunk. webpack.config.js module.
Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which can then be loaded on demand or in parallel.
Webpack injects some code into main. js which takes care of lazy loading async chunks and stops from loading same chunks again and again. When a route changes, React router calls a Webpack function to load a chunk file and Webpack after done loading runs it, which chunk then would internally ask React to do something.
Naming your Webpack chunks helps you understand the contents of each bundle of code, and keep consistency between deploys. In this post, we look at named outputs, including dynamic imports, split chunks, and common chunks. I recently wrote about code splitting with Webpack and using the SplitChunksPlugin.
Indeed, while actually there is some official documentation: https://webpack.js.org/plugins/split-chunks-plugin/
The following article might be more useful: https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
I particularly find the following very enlightening:
The new chunk graph introduces a new object: the
ChunkGroup
. AChunkGroup
contains aChunks
.At an entrypoint or an async splitpoint a single
ChunkGroup
is referenced, which means all containtedChunks
in parallel. AChunk
can be referenced in multipleChunkGroups
.There are no longer parent-child relationships between
Chunk
, instead this relationship now exists betweenChunkGroups
.Now “splitting” of
Chunks
can be expressed. The newChunk
is added to allChunkGroups
, which contain the originChunk
. This doesn’t affect parent relationships negatively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With