From 1891dd7238983a3660504e1f4082228cbf84ee4a Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 15 May 2024 14:47:26 +0200 Subject: [PATCH] fix(css): only use files the current bundle contains If using multiple output formats like `es` and `cjs` and running vite in watch mode, then `generateBundle` will be called once per output format. The bundle on each call only contains the files for the current output format, but our `pureCSSChunks` contain all files (from all output formats). So we need to filter the chunk list to only contain valid files, as otherwise `basename` will fail with: > The "path" argument must be of type string. Received undefined Signed-off-by: Ferdinand Thiessen --- 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 0e97c247cf01f8..45e35714ae9156 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -848,9 +848,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,