Chunking
Chunking is the process that decides which modules are placed into which bundles, and the relationship between these bundles.
For this process a few intermediate concepts are used:
ChunkingContext
: A context trait which controls the process.ChunkItem
: A derived object from aModule
which combines the module with theChunkingContext
.ChunkableModule
: A trait which defines how a specificModule
can be converted into aChunkItem
.ChunkableModuleReference
: A trait which defines how a specificModuleReference
will interact with chunking.ChunkType
: A trait which defines how to create aChunk
fromChunkItem
s.Chunk
: A trait which represents a bundle ofChunkItem
s.
graph TB convert{{convert to chunk item}} ty{{get chunk type}} create{{create chunk}} EcmascriptModule -. has trait .-> Module CssModule -. has trait .-> Module Module -. has trait .-> ChunkableModule ChunkableModule === convert ChunkingContext --- convert convert ==> ChunkItem ChunkItem ==== ty EcmascriptChunkType -. has trait ..-> ChunkType CssChunkType -. has trait ..-> ChunkType ty ==> ChunkType ChunkType ==== create create ==> Chunk EcmascriptChunk -. has trait ..-> Chunk CssChunk -. has trait ..-> Chunk BrowserChunkingContext -. has trait .-> ChunkingContext NodeJsChunkingContext -. has trait .-> ChunkingContext
A Module
need to implement the ChunkableModule
trait to be considered for chunking. All references of these module that should be considered for chunking need to implement the ChunkableModuleReference
trait.
The chunking algorithm walks the module graph following these chunkable references to find all modules that should be bundled. These modules are converted into ChunkItem
s and a special algorithm decides which ChunkItem
s are placed into which Chunk
s.
One factor is for example the ChunkType
of each ChunkItem
since only ChunkItem
s with the same ChunkType
can be placed into the same Chunk
. Other factors are directory structure and chunk size based.
Once the ChunkItem
s that should be placed together are found, a Chunk
is created for each group of ChunkItem
s by using the method on ChunkType
.
An OutputAsset
is then created for each Chunk
which is eventually emitted to disk. The output format decides how a Chunk
is transformed into an OutputAsset
. These OutputAsset
s that are loaded together are called a chunk group.
The chunking also collects all modules into a AvailableModules
struct which is e. g. used for nested chunk groups to avoid duplicating modules that are already in the parent chunk group.