Change splitChunks() default: Use single runtime chunk #2708
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Webpacker v4 docs for using the splitChunks() API describe a use case for using multiple entry points on a single page.
In these cases, webpack encourages use of runtimeChunk 'single'.
In general, webpack discourages the use of multiple entry points per page:
Multiple entry points on a page can lead to surprising behavior, say from duplicated modules.
However, Rails developers seeking to keep their bundles small will be inclined to split their code into multiple packs, especially given prior experience with the Rails asset pipeline.
While documentation can be improved to encourage dynamic imports over multiple entry points as a form of code-splitting, it's reasonable to expect that Rails developers will continue to use multiple entry points per page.
Solution
This PR sets
runtimeChunk: 'single'
as the default when splitChunks() is enabled to ensure modules across multiple entry points are properly shared.I propose that this change will improve the "out-of-the-box" developer experience by preventing surprising bugs, i.e., keep Rails devs from shooting themselves in the foot.
The splitChunks optimization is confusing. It would be fair to expect that most developers would not be inclined to learn about its pros, cons, and caveats. Let's improve Webpacker's "conceptual compression" by fixing potential problems with the majority use case. "Pro" users will still be able to override the defaults to meet their needs.