CssChunkingPlugin
is a plugin specifically designed for CSS code splitting. It ensures that the loading order of styles matches the import order in your source code, avoiding UI issues caused by incorrect CSS order.
This plugin is inspired by Next.js's CSS Chunking feature, thanks to the Next.js team for their innovation.
After enabling CssChunkingPlugin, SplitChunksPlugin will no longer process CSS modules.
This means that options like optimization.splitChunks
will no longer handle CSS modules, and all CSS module code splitting logic will be handled entirely by CssChunkingPlugin.
boolean
false
Whether to strictly preserve the import order of CSS modules.
The minimum size (in bytes) for a generated chunk.
The maximum size (in bytes) for a generated chunk. Chunks larger than maxSize will be split into smaller parts, each with a size of at least minSize.
a.css
and b.css
are imported in foo.js
and bar.js
with different sequences:
Regular Mode (strict: false): Considers a.css
and b.css
to have no dependency relationship and doesn't enforce execution order, thus merging them into the same chunk.
Strict Mode (strict: true): Strictly maintains execution order consistent with import sequence, therefore packaging a.css
and b.css
into separate chunks
SplitChunksPlugin
does not consider the import order of modules when splitting code.
For JavaScript modules, this is not an issue because execution order is determined at runtime via function calls.
However, for CSS modules, the execution order is entirely determined by their order in the output files and cannot be controlled at runtime. If the order changes, it may cause style issues.
For example, importing the following CSS modules:
SplitChunksPlugin
may split them into the following chunks to satisfy maxSize
or other constraints:
This results in the execution order being b.css → a.css → c.css, which violates the original import order and may cause style errors.
CssChunkingPlugin
first splits all CSS modules, then determines which can be merged based on the import order in the source code. Modules that cannot be merged are split into separate chunks to ensure style correctness.
Because CssChunkingPlugin prioritizes preserving style execution order, it may split more chunks than SplitChunksPlugin.