webpack: Webpack 4 splitChunks causes reused chunks to not be executed
Bug report
I am working in a complicated app that has a bunch of bundles on every page. Most of these are “global” bundles and appear on every page. So, I have configured webpack to prevent any modules that appear in these bundles from appearing in any other bundle via splitChunks. More context on this here: https://stackoverflow.com/questions/49163684/how-to-configure-webpack-4-to-prevent-chunks-from-list-of-entry-points-appearing
What is the current behavior?
It seems that when I configure splitChunks in this way, it causes the global bundles to not be executed.
If the current behavior is a bug, please provide the steps to reproduce.
Here’s a branch on a repo that reproduces this issue: https://github.com/lencioni/webpack-splitchunks-playground/tree/splitchunks-execution-problem
In this repro case, it currently logs the following to the console:
core module
core module b
non-core module
non-core b2
non-core module b
In the debugging I’ve done, it seems that this splitChunks configuration causes the executeModules
part of the bundle that the runtime chunk is looking for to be undefined
.
What is the expected behavior?
The code in the “global” bundles should be executed, which should cause the console log to look more like this:
core module
core bundle
core module b
core bundle b
core bundle c
non-core module
non-core b2
non-core module b
Other relevant information: webpack version: 4.8.1 Node.js version: 8.9.1 Operating System: Mac Additional tools:
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 33
- Comments: 26 (3 by maintainers)
Commits related to this issue
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to lencioni/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
- Add preserveEntrypoint option to splitChunks cacheGroups I am working in a complicated app that has a bunch of bundles on every page. Most of these are "global" bundles and appear on every page. So, ... — committed to JanJakes/webpack by lencioni 6 years ago
This seems to maybe be happening intentionally here: https://github.com/webpack/webpack/blob/2986ab1a18e895ac3f2546ac0c70bf73c0fdb679/lib/optimize/SplitChunksPlugin.js#L559-L565
However it isn’t clear to me from the code or the commit message (added by https://github.com/webpack/webpack/commit/18ae73dad4bb761593a9f5529a182601e1b57520) why this behavior exists.
If I disable this entrypoint removal, I get the build that I expect and my program runs as expected.
It seems that this logic should either be removed or perhaps there should be an option added to cacheGroups that allows a cacheGroup to also be an entrypoint.
@IanVS here you go:
The workaround is to get rid of
vendor-entry
entry chunk, and import it from your real entry point(s):@amakhrov if you got it working, do you mind sharing your config? I am still struggling to convert to webpack 4 because of this issue.
@snapwich DllPlugin /DllReferencePlugin provided by webpack should give you more control over code splitting. You could use them with ExecutableDllPlugin to execute particular entry points within the resulted bundle.
You could use DllPlugin and DllReferencePlugin for code splitting and ExecutableDllPlugin to execute a bundle with shared modules.
Finally, i could get a single vendors bundle that contains all vendor code while keeping the code of top-level entries in their own bundle. Also, my library(ProjectLib) is in its own bundle instead of vendors.
It depends on how you define ‘vendor’ modules.
See the test function in my example above. The function takes the name of the module and the array of chunks which have this module included.
If you want to extract all modules used by your vendor-entry file, you would do smth like
@expressiveco your config looks ok to me. It should create a
vendor.js
chunk (in addition to thevendor-entry.js
andmain.js
entry chunks), containing modules that are shared across the both entry chunks.What doesn’t go as expected here for you?
I came across the same issue. In my old webpack config the ‘common’ chunk served both as a chunk with shared modules and an entry point. In webpack 4 it no longer works.
As a workaround I ended up having two chunks: ‘common’ and ‘common-entry’. They are always included on all pages together - so there is no good reason for them not to be a single file.