diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 85ba5eea8e5b89..d9ebbb83220390 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -739,10 +739,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { .map((file) => path.basename(file)) .join('|') .replace(/\./g, '\\.') + // require and import calls might be chained by minifier using the comma operator + // in this case we have to keep one comma + // if a next require is chained or add a semicolon to terminate the chain. const emptyChunkRE = new RegExp( opts.format === 'es' - ? `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];\n?` - : `\\brequire\\(\\s*["'][^"']*(?:${emptyChunkFiles})["']\\);\n?`, + ? `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];` + : `(\\b|,\\s*)require\\(\\s*["'][^"']*(?:${emptyChunkFiles})["']\\)(;|,)`, 'g', ) for (const file in bundle) { @@ -769,7 +772,10 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { chunk.code = chunk.code.replace( emptyChunkRE, // remove css import while preserving source map location - (m) => `/* empty css ${''.padEnd(m.length - 15)}*/`, + (m) => + opts.format === 'es' + ? `/* empty css ${''.padEnd(m.length - 15)}*/` + : `${m.at(-1)}/* empty css ${''.padEnd(m.length - 16)}*/`, ) } }