From 690a36ffdb7d6f6568f35a304b4904e7aa475f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Thu, 5 Dec 2024 17:30:17 +0900 Subject: [PATCH] fix: remove CSS import in CJS correctly in some cases (#18885) --- .../vite/src/node/__tests__/plugins/css.spec.ts | 17 ++++++++++++++++- packages/vite/src/node/plugins/css.ts | 15 +++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/css.spec.ts b/packages/vite/src/node/__tests__/plugins/css.spec.ts index 1d2428951e281f..6d2cf1a93af786 100644 --- a/packages/vite/src/node/__tests__/plugins/css.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/css.spec.ts @@ -309,6 +309,7 @@ require("other-module");` const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs') const newCode = replacer(code) + expect(newCode.length).toBe(code.length) expect(newCode).toMatchInlineSnapshot( `"require("some-module"),/* empty css */require("other-module");"`, ) @@ -316,14 +317,28 @@ require("other-module");` expect(newCode).not.toContain('pure_css_chunk.js') }) + test('replaces require call in minified code that uses comma operator 2', () => { + const code = 'require("pure_css_chunk.js"),console.log();' + const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs') + const newCode = replacer(code) + expect(newCode.length).toBe(code.length) + expect(newCode).toMatchInlineSnapshot( + `"/* empty css */console.log();"`, + ) + expect(newCode).not.toContain('pure_css_chunk.js') + }) + test('replaces require call in minified code that uses comma operator followed by assignment', () => { const code = 'require("some-module"),require("pure_css_chunk.js");const v=require("other-module");' const replacer = getEmptyChunkReplacer(['pure_css_chunk.js'], 'cjs') - expect(replacer(code)).toMatchInlineSnapshot( + const newCode = replacer(code) + expect(newCode.length).toBe(code.length) + expect(newCode).toMatchInlineSnapshot( `"require("some-module");/* empty css */const v=require("other-module");"`, ) + expect(newCode).not.toContain('pure_css_chunk.js') }) }) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 34f3460ef82b76..5ae6a93b7ad462 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -1116,10 +1116,17 @@ export function getEmptyChunkReplacer( code.replace( emptyChunkRE, // remove css import while preserving source map location - (m) => - outputFormat === 'es' - ? `/* empty css ${''.padEnd(m.length - 15)}*/` - : `${m.at(-1)}/* empty css ${''.padEnd(m.length - 16)}*/`, + (m, p1, p2) => { + if (outputFormat === 'es') { + return `/* empty css ${''.padEnd(m.length - 15)}*/` + } + if (p2 === ';') { + // if it ends with `;`, move it before and remove the leading `,` + return `${p2}/* empty css ${''.padEnd(m.length - 16)}*/` + } + // if it ends with `,`, remove it but keep the leading `,` if exists + return `${p1}/* empty css ${''.padEnd(m.length - 15 - p1.length)}*/` + }, ) }