I get the general meaning that CommonsChunkPlugin looks at all entry points, checks to see if there are common packages / dependencies between them, and splits them into its own package.
So let's say I have the following configuration:
... enrty : { entry1 : 'entry1.js', //which has 'jquery' as a dependency entry2 : 'entry2.js', //which has 'jquery as a dependency vendors : [ 'jquery', 'some_jquery_plugin' //which has 'jquery' as a dependency ] }, output: { path: PATHS.build, filename: '[name].bundle.js' } ...
If I contact without using CommonsChunkPlugin
As a result, I will have 3 new package files:
entry1.bundle.js , which contains the complete code from entry1.js and jquery and contains its own runtimeentry2.bundle.js , which contains the complete code from entry2.js and jquery and contains its own runtimevendors.bundle.js which contains the complete code from jquery and some_jquery_plugin and contains its own runtime
This is obviously bad because I will potentially load jquery 3 times per page, so we donโt want this.
If I contact using CommonsChunkPlugin
Depending on which arguments are passed to CommonsChunkPlugin , the following will happen:
CASE 1: If I pass { name : 'commons' } , I will have the following package files:
entry1.bundle.js , which contains the complete code from entry1.js , is a requirement for jquery and does not contain runtimeentry2.bundle.js , which contains the complete code from entry2.js , is a requirement for jquery and does not contain runtimevendors.bundle.js , which contains the complete code from some_jquery_plugin , is a requirement for jquery and does not contain runtimecommons.bundle.js which contains the full code from jquery and contains runtime
This way we get a few small packages in general, and the runtime is contained in the commons package. Pretty normal, but not perfect.
CASE 2: If I go through { name : 'vendors' } , I will have the following package files:
entry1.bundle.js , which contains the complete code from entry1.js , is a requirement for jquery and does not contain runtimeentry2.bundle.js , which contains the complete code from entry2.js , is a requirement for jquery and does not contain runtimevendors.bundle.js , which contains the complete code from jquery and some_jquery_plugin and contains runtime.
Thus, we again receive several small packages, but the runtime is now contained in the vendors package. This is slightly worse than in the previous case, since the runtime is now in the vendors package.
CASE 3: If I go through { names : ['vendors', 'manifest'] } , I will have the following package files:
entry1.bundle.js , which contains the complete code from entry1.js , is a requirement for jquery and does not contain runtimeentry2.bundle.js , which contains the complete code from entry2.js , is a requirement for jquery and does not contain runtimevendors.bundle.js , which contains the complete code from jquery and some_jquery_plugin and does not contain a runtimemanifest.bundle.js , which contains requirements for every other package and contains runtime
Thus, we end up with small packages in general, and the runtime is contained in the manifest package. This is an ideal case.
What I don't understand / I'm not sure I understand
In CASE 2, why did we end up with a vendors package containing both generic code ( jquery ) and all that remains of the vendors ( some_jquery_plugin )? In my opinion, the CommonsChunkPlugin here was that it compiled the generic code ( jquery ), and since we forced it to output it to the vendors package, it kind of โcombinedโ the generic code into the vendors bundle (which now only contains code from some_jquery_plugin ) Please confirm or explain.
In CASE 3, I donโt understand what happened when we passed { names : ['vendors', 'manifest'] } plugin. Why / how the vendors package vendors kept intact, containing both jquery and some_jquery_plugin , when jquery clearly a common dependency, and why the generated manifest.bundle.js file was created the way it was created (requiring all other packages and containing runtime) ?
javascript webpack bundle webpack-plugin commonschunkplugin
Dimitris Karagiannis Sep 17 '16 at 2:53 on 2016-09-17 14:53
source share