This is how the CommonsChunkPlugin works.
A common chunk “receives” the modules shared by several entry chunks.
A good example of a complex configuration can be found in the Webpack repository.
The CommonsChunkPlugin is run during the optimization phase of Webpack, which means that it operates in memory, just before the chunks are sealed and written to the disk.
When several common chunks are defined, they are processed in order. In your case 3, it is like running the plugin twice. But please note that the CommonsChunkPlugin can have a more complex configuration (minSize, minChunks, etc) that impacts the way modules are moved.
CASE 1:
- There are 3
entrychunks (entry1,entry2andvendors). - The configuration sets the
commonschunk as a common chunk. - The plugin processes the
commonscommon chunk (since the chunk does not exist, it is created):- It collects the modules that are used more than once in the other chunks:
entry1,entry2andvendorsusejqueryso the module is removed from these chunks and is added to thecommonschunk. - The
commonschunk is flagged as anentrychunk while theentry1,entry2andvendorschunks are unflagged asentry.
- It collects the modules that are used more than once in the other chunks:
- Finally, since the
commonschunk is anentrychunk it contains the runtime and thejquerymodule.
CASE 2:
- There are 3
entrychunks (entry1,entry2andvendors). - The configuration sets the
vendorschunk as a common chunk. - The plugin processes the
vendorscommon chunk:- It collects the modules that are used more than once in the other chunks:
entry1andentry2usejqueryso the module is removed from these chunks (note that it is not added to thevendorschunk because thevendorschunk already contains it). - The
vendorschunk is flagged as anentrychunk while theentry1andentry2chunks are unflagged asentry.
- It collects the modules that are used more than once in the other chunks:
- Finally, since the
vendorschunk is anentrychunk, it contains the runtime and thejquery/jquery_pluginmodules.
CASE 3:
- There are 3
entrychunks (entry1,entry2andvendors). - The configuration sets the
vendorschunk and themanifestchunk as common chunks. - The plugin creates the
manifestchunk as it does not exist. - The plugin processes the
vendorscommon chunk:- It collects the modules that are used more than once in the other chunks:
entry1andentry2usejqueryso the module is removed from these chunks (note that it is not added to thevendorschunk because thevendorschunk already contains it). - The
vendorschunk is flagged as anentrychunk while theentry1andentry2chunks are unflagged asentry.
- It collects the modules that are used more than once in the other chunks:
- The plugin processes the
manifestcommon chunk (since the chunk does not exist, it is created):- It collects the modules that are used more than once in the other chunks: as there are no modules used more than once, no module is moved.
- The
manifestchunk is flagged asentrychunk while theentry1,entry2andvendorsare unflagged asentry.
- Finally, since the
manifestchunk is anentrychunk it contains the runtime.