From 92f9c5a54ca0bdd4547b1422e78e6cdb0542acd5 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 3 May 2024 15:31:57 +0200 Subject: [PATCH] fix(css): ensure order of extracted CSS Without this the order of the extracted CSS rules is defined by the order `renderChunk` of the css plugin is called. So with this the order of CSS rules is always in order of the output chunks of the bundle. Signed-off-by: Ferdinand Thiessen --- packages/vite/src/node/plugins/css.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 0e97c247cf01f8..39b16bf15baea0 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -7,6 +7,7 @@ import postcssrc from 'postcss-load-config' import type { ExistingRawSourceMap, ModuleFormat, + OutputAsset, OutputChunk, RenderedChunk, RollupError, @@ -903,17 +904,19 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { .map((chunk) => [chunk.preliminaryFileName, chunk]), ) - function collect(fileName: string) { - const chunk = bundle[fileName] + function collect(chunk: OutputChunk | OutputAsset) { if (!chunk || chunk.type !== 'chunk' || collected.has(chunk)) return collected.add(chunk) - chunk.imports.forEach(collect) + chunk.imports.forEach((importName) => collect(bundle[importName])) css += chunkCSSMap.get(chunk.preliminaryFileName) ?? '' } - for (const chunkName of chunkCSSMap.keys()) - collect(prelimaryNameToChunkMap.get(chunkName)?.fileName ?? '') + for (const chunk of Object.values(bundle)) { + if (chunk.type === 'chunk' && (chunk as OutputChunk).isEntry) { + collect(chunk) + } + } return css }