I get the general gist that the CommonsChunkPlugin
looks at all the entry points, checks to see if there are common packages/dependencies between them and separates them into their own bundle.
So, let's assume 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 bundle without using CommonsChunkPlugin
I will end up with 3 new bundle files:
entry1.bundle.js
which contains the complete code fromentry1.js
andjquery
and contains its own runtimeentry2.bundle.js
which contains the complete code fromentry2.js
andjquery
and contains its own runtimevendors.bundle.js
which contains the complete code fromjquery
andsome_jquery_plugin
and contains its own runtime
This is obviously bad because I will potentially load jquery
3 times in the page, so we don't want that.
If I bundle using CommonsChunkPlugin
Depending on what arguments I pass to CommonsChunkPlugin
any of the following will happen:
CASE 1 : If I pass
{ name : 'commons' }
I will end up with the following bundle files:entry1.bundle.js
which contains the complete code fromentry1.js
, a requirement forjquery
and does not contain the runtimeentry2.bundle.js
which contains the complete code fromentry2.js
, a requirement forjquery
and does not contain the runtimevendors.bundle.js
which contains the complete code fromsome_jquery_plugin
, a requirement forjquery
and does not contain the runtimecommons.bundle.js
which contains the complete code fromjquery
and contains the runtime
This way we end up with some smaller bundles overall and the runtime is contained in the
commons
bundle. Pretty ok but not ideal.CASE 2 : If I pass
{ name : 'vendors' }
I will end up with the following bundle files:entry1.bundle.js
which contains the complete code fromentry1.js
, a requirement forjquery
and does not contain the runtimeentry2.bundle.js
which contains the complete code fromentry2.js
, a requirement forjquery
and does not contain the runtimevendors.bundle.js
which contains the complete code fromjquery
andsome_jquery_plugin
and contains the runtime.
This way, again, we end up with some smaller bundles overall but the runtime is now contained in the
vendors
bundle. It's a little worse than the previous case, since the runtime is now in thevendors
bundle.CASE 3 : If I pass
{ names : ['vendors', 'manifest'] }
I will end up with the following bundle files:entry1.bundle.js
which contains the complete code fromentry1.js
, a requirement forjquery
and does not contain the runtimeentry2.bundle.js
which contains the complete code fromentry2.js
, a requirement forjquery
and does not contain the runtimevendors.bundle.js
which contains the complete code fromjquery
andsome_jquery_plugin
and does not contain the runtimemanifest.bundle.js
which contains requirements for every other bundle and contains the runtime
This way we end up with some smaller bundles overall and the runtime is contained in the
manifest
bundle. This is the ideal case.
What I do not understand/I am not sure I understand
In CASE 2 why did we end up with the
vendors
bundle containing both the common code (jquery
) and whatever remained from thevendors
entry (some_jquery_plugin
)? From my understanding, what theCommonsChunkPlugin
did here was that it gathered the common code (jquery
), and since we forced it to output it to thevendors
bundle, it kind of "merged" the common code into thevendors
bundle (which now only contained the code fromsome_jquery_plugin
). Please confirm or explain.In CASE 3 I do not understand what happened when we passed
{ names : ['vendors', 'manifest'] }
to the plugin. Why/how was thevendors
bundle kept intact, containing bothjquery
andsome_jquery_plugin
, whenjquery
is clearly a common dependency, and why was the generatedmanifest.bundle.js
file created the way it was created (requiring all other bundles and containing the runtime) ?