fix(css): preserve css order when merging pure css chunks#21588
Open
DukeDeSouth wants to merge 1 commit intovitejs:mainfrom
Open
fix(css): preserve css order when merging pure css chunks#21588DukeDeSouth wants to merge 1 commit intovitejs:mainfrom
DukeDeSouth wants to merge 1 commit intovitejs:mainfrom
Conversation
When a pure CSS chunk (created via `manualChunks` or code splitting) is removed from `chunk.imports`, its CSS files were appended to the importing chunk's `importedCss` Set via `.add()`. Since Sets maintain insertion order and the chunk's own CSS was already in the Set, the pure CSS chunk's styles ended up AFTER the importing chunk's styles in the cascade. This broke the expected CSS order: dependency styles should appear before the importing module's styles so the importing module can override them with same-specificity selectors. The fix saves the chunk's own CSS before merging, collects CSS from removed pure CSS chunks in their original import order, then rebuilds `importedCss` with the correct ordering: pure CSS chunk styles first, then the chunk's own styles. Fixes vitejs#3924 Fixes vitejs#6375 Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Human View
Description
Fixes #3924
Fixes #6375
When Vite creates pure CSS chunks (via
manualChunks, code splitting, or when a chunk contains only CSS imports), it removes them fromchunk.importsin thegenerateBundlehook and merges their CSS into the importing chunk'sviteMetadata.importedCss.The bug: CSS files from removed pure CSS chunks were appended to
importedCssviaSet.add(). Since the chunk's own CSS was already in the Set, the dependency's CSS ended up after the chunk's own CSS in the cascade. This inverted the expected order — a page's CSS could no longer override a shared component's CSS with same-specificity selectors.Root Cause
In
packages/vite/src/node/plugins/css.ts, inside thegenerateBundlehook:Since
importedCssis aSetand the chunk's own CSS entries are already present,.add()places the pure CSS chunk's styles at the end — after the chunk's own styles. This breaks the cascade: dependency styles should come first so the importing module can override them.Fix
The fix saves the chunk's own CSS before the merge, collects CSS from pure CSS chunks in import order, then rebuilds the Set with correct ordering:
Reproduction
base.css) with.el { color: red }page.css) with.el { color: green }— same specificity, intended overridemanualChunksto splitbase.cssinto its own chunkTest Plan
playground/css-dynamic-importthat creates a pure CSS chunk viamanualChunksand verifies the cascade order is correctThis is a long-standing issue (opened 2021, 600+ reactions) affecting anyone using CSS Modules, dynamic imports, or
manualChunkswhere cascade order matters.AI View (DCCE Protocol v1.0)
Metadata
AI Contribution Summary
Verification Steps Performed
Human Review Guidance
chunk.imports,packages/vite/src/node/plugins/css.ts,base.cssMade with M7 Cursor