Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 4 Split Chunks Terms

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,
        },
      },
    },
  },
like image 203
user3299310 Avatar asked May 22 '18 22:05

user3299310


People also ask

What is split chunks in webpack?

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.

Does webpack do code splitting?

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.

How does chunking work webpack?

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.

What is webpack chunk name?

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.


1 Answers

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. A ChunkGroup contains a Chunks.

At an entrypoint or an async splitpoint a single ChunkGroup is referenced, which means all containted Chunks in parallel. A Chunk can be referenced in multiple ChunkGroups.

There are no longer parent-child relationships between Chunk, instead this relationship now exists between ChunkGroups.

Now “splitting” of Chunks can be expressed. The new Chunk is added to all ChunkGroups, which contain the origin Chunk. This doesn’t affect parent relationships negatively.

like image 146
Fernando Espinosa Avatar answered Oct 09 '22 19:10

Fernando Espinosa