Repository navigation
Description
Follows #9, which analyses how inlining affects Vite's CSS.
Problem
In a build, Vite keeps a style module's CSS outside the module's code (css.ts).
The code Rolldown sees is empty for a plain CSS file. For a CSS module, it is the class-name map.
Rolldown lists an inlined module in the modules of every chunk that carries a copy of it.
Vite builds each chunk's stylesheet from chunk.modules (css.ts).
So the CSS of an inlined chunk is copied into the stylesheet of every chunk that carries it.
maxSize counts only the code Rolldown sees, so this CSS is copied however large it is.
To avoid the copies, the docs tell Vite users to exclude style modules (inline-common-chunks.md).
With that exclude, a common chunk that holds any style module is not inlined, its JS included.
This hits apps whose components import CSS modules. The open PR cloudflare/vinext#4726 uses this exclude, and chunks holding CSS modules are among the extra JS files its pages load compared with Next.js' webpack build.
Proposal
With build.cssCodeSplit: true, Vite's default, copying the CSS is the expected behavior.
It saves the request for the shared stylesheet, at the cost of duplicated bytes.
The copies can also change which rule wins. A shared Button imports button.module.css, and pages B and C, in different chunks, both use it:
/* button.module.css */
.button { background: red; }
/* c.module.css: C passes .c to Button to override its background */
.c { background: green; }When Button's chunk is inlined, the stylesheets of B and C each get a copy of .button.
When C is shown and B is loaded later, B's copy of .button comes after .c, so C's button turns red.
webpack copies small shared CSS the same way and has the same effect (webpack/mini-css-extract-plugin#555).
For this to be a defined behavior, maxSize has to count the CSS, and the single stylesheet of cssCodeSplit: false must not get the copies.
An alternative is for Vite to emit a copied module's CSS once, as a stylesheet that every copy loads, as Turbopack does. That keeps the CSS output unchanged, at the cost of that stylesheet's request. It can be added later as an option.
Tasks
Rolldown: let a plugin declare a module's size
load and transform can return the module's size, used instead of the size of its transformed code:
interface SourceDescription {
// existing fields...
declaredModuleSize?: number
}A later hook's value replaces an earlier one, as with moduleSideEffects.
inlineCommonChunks.maxSize and the size options of codeSplitting.groups (minSize, maxSize, minModuleSize and maxModuleSize) use this size.
Groups that hold style modules then count their CSS, so their output can change even without inlineCommonChunks.
Vite: declare the CSS size of style modules
With cssCodeSplit: true, Vite's transform returns the byte length of a style module's code plus its CSS as declaredModuleSize.
With cssCodeSplit: false, the CSS is not copied (next task), so Vite declares nothing.
Vite: one copy per module in the single stylesheet
With cssCodeSplit: false, all CSS goes into one file.
Vite builds it by appending each chunk's CSS (css.ts), so the CSS of an inlined module is appended once for every chunk that carries it.
That saves no request, since there is only one file, and the later copies can change which rule wins.
Vite should append each module's CSS once, where it first appears. Builds without inlineCommonChunks have each module in one chunk, so their output doesn't change.
Docs
Replace the exclude advice for Vite with the behavior above: the CSS is copied, the example of which rule wins, how to keep the copies from changing which rule wins (@layer, more specific selectors), and how to prevent the copies (exclude).
Rolldown: mark the copied modules in a chunk's properties
A chunk lists the modules it carries copies of in modules and moduleIds, but nothing marks them as copies.
A chunk's properties should mark them, so a user who finds copied CSS in a stylesheet can tell which inlined chunk it came from.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
feat: introduce `declaredModuleSize` to allow plugins to declare a mo…