-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Expected Behavior / Situation
Here's my context:
In a large application, I want to bundle related features into a single chunk.
I have a way to know if a module should be part of a specific chunk and I'm forcing them with a manualChunk function in rollup configuration.
Let's say I'm forcing moduleA1, moduleA2 into chunkA; moduleB1 and moduleB2 into chunkB.
Those modules have dependencies, some unique to them (say privateA) and some shared (say shared1).
What I would like to have, is rollup doing its usual magic dependency graph splitting after I've manually merged some modules.
In my example: privateA is used only by moduleA1 and moduleA2, so it can be merged into chunkA as well. shared1 having dependent in both chunkA and chunkB, it should go into a new chunkShared that is imported by both.
This is basically the same thing as rollup would do if I didn't use manualChunk.
Actual Behavior / Situation
As pointed out in the documentation, something slightly different happens.
When moduleA is put into chunkA, all its dependencies that are not already into another chunk are brought along and put into chunkA. This includes privateA and shared1.
This is simple but not efficient at runtime, and defeats code splitting.
When moduleB1 is later processed, it sees shared1 was already put into chunkA, so chunkA becomes a dependency of chunkB.
Consider this situation:
moduleA1 and moduleA2 are large; shared1 is very small.
When the application dynamically imports moduleB1; it needs to import the complete, large, chunkA just because it needs the small shared1 module.
In the end, instead of loading just what was needed, everything had to be fetched, including the large, unused moduleA1. The benefit of code splitting is lost.
Modification Proposal
I would like manualChunk algorithm to change -- always, or opted into with an option to avoid breaking behavior of existing builds, or even a brand new alternative config.
It should be a first pass that merges some modules forcefully, but just those modules, no dependencies.
Then the regular rollup algorithm should run as usual and decide how to best split the whole graph.