From 15a6ebb414e3155583e3e9ad970afbdb598b0609 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 28 May 2024 08:02:26 +0200 Subject: [PATCH] fix(css): only use files the current bundle contains (#16684) --- packages/vite/src/node/plugins/css.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 4fd0bdf7f1f044..47276079f9e224 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -884,9 +884,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { .map((chunk) => [chunk.preliminaryFileName, chunk.fileName]), ) - const pureCssChunkNames = [...pureCssChunks].map( - (pureCssChunk) => prelimaryNameToChunkMap[pureCssChunk.fileName], - ) + // When running in watch mode the generateBundle is called once per output format + // in this case the `bundle` is not populated with the other output files + // but they are still in `pureCssChunks`. + // So we need to filter the names and only use those who are defined + const pureCssChunkNames = [...pureCssChunks] + .map((pureCssChunk) => prelimaryNameToChunkMap[pureCssChunk.fileName]) + .filter(Boolean) const replaceEmptyChunk = getEmptyChunkReplacer( pureCssChunkNames,