Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: What is the difference between "all" and "initial" options in optimization.splitChunks.chunks

I'm looking for a clear explanation of the difference between these two options in webpack. I've read the description here but the difference is not clear. Quoting the description:

Setting the optimization.splitChunks.chunks option to "all" initial chunks will get affected by it (even the ones not imported dynamically). This way chunks can even be shared between entry points and on-demand loading.

like image 755
quantdaddy Avatar asked May 02 '18 04:05

quantdaddy


People also ask

What is webpack splitChunks?

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 effects.

What is optimization in webpack?

Tells webpack to determine and flag chunks which are subsets of other chunks in a way that subsets don't have to be loaded when the bigger chunk has been already loaded. By default optimization. flagIncludedChunks is enabled in production mode and disabled elsewise.

What is runtime chunk in webpack?

Each of the runtime files contains code that enables loading of your chunks. If you open any of those runtime files, you will see code that loads your chunks via Jsonp. Since you have asked webpack to split chunks, you are now free to load any chunk any time.

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.


1 Answers

Trying for the simplest explanation. This is the file that will be transpiled and bundled:

//app.js
import "my-static-module";

if(some_condition_is_true){
  import ("my-dynamic-module")
}
console.log("My app is running")

Now see how different optimization types will treat it.

async (default):

Two files will be created.

  1. bundle.js (includes app.js + my-static-module)
  2. chunk.js (includes my-dynamic-module only)

initial:

Three files will be created

  1. app.js (includes only app.js)
  2. bundle.js (includes only my-static-module)
  3. chunk.js (includes only my-dynamic-module)

all:

Two files will be created

  1. app.js (includes app.js only)
  2. bundle.js (includes my-static-module + my-dynamic-module)

"all" will have the smallest overall size.

like image 124
Sujaan Singh Avatar answered Sep 19 '22 20:09

Sujaan Singh