fix(css-order): preserve import order for CSS chunks #21067
+72
−3
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.
Description
This PR fixes CSS link order in generated HTML when using
manualChunksto split CSS into separate chunks. Previously, when third-party CSS (likeanimate.css) was split into a separate chunk viamanualChunks, the resulting CSS links inindex.htmlwould appear in the wrong order (e.g.,index.cssbeforeanimate.css), even though the source code imported them in the correct order.Problem:
When pure CSS chunks are removed and their CSS files are merged into parent chunks'
viteMetadata.importedCss, the merged CSS was being added after the parent's existing CSS, causing the final HTML to have the wrong link order.Solution:
Modified the CSS merging logic in
packages/vite/src/node/plugins/css.tsto prepend CSS from pure CSS chunks before the parent's existing CSS, preserving the original import order. This ensures that when CSS is injected into HTML, dependencies appear before the entry CSS.Changes:
generateBundleincssPostPluginto merge CSS from pure CSS chunks in the correct orderplayground/css-order-manual-chunksto verify CSS link order respects import orderTesting:
dist/index.htmlappear in the correct order when usingmanualChunksFixes #20995