From 055f1c16e55b527543e7af0e65e820b245b12d2e Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 2 Jul 2024 17:49:19 +0800 Subject: [PATCH] fix(lib): remove pure CSS dynamic import (#17601) --- packages/vite/src/node/plugins/importAnalysisBuild.ts | 4 +++- playground/lib/__tests__/lib.spec.ts | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index fbadacaa9c4757..9af262a70544a9 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -403,7 +403,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { return } - if (ssr || isWorker) { + // If preload is not enabled, we parse through each imports and remove any imports to pure CSS chunks + // as they are removed from the bundle + if (!insertPreload) { const removedPureCssFiles = removedPureCssFilesCache.get(config) if (removedPureCssFiles && removedPureCssFiles.size > 0) { for (const file in bundle) { diff --git a/playground/lib/__tests__/lib.spec.ts b/playground/lib/__tests__/lib.spec.ts index 021d7d1bc636b5..af144de44e9c46 100644 --- a/playground/lib/__tests__/lib.spec.ts +++ b/playground/lib/__tests__/lib.spec.ts @@ -67,6 +67,14 @@ describe.runIf(isBuild)('build', () => { expect(code).toMatch(/await import\("\.\/message-[-\w]{8}.js"\)/) }) + test('Library mode does not have any reference to pure CSS chunks', async () => { + const code = readFile('dist/lib/dynamic-import-message.es.mjs') + + // Does not import pure CSS chunks and replaced by `Promise.resolve({})` instead + expect(code).not.toMatch(/await import\("\.\/dynamic-[-\w]{8}.js"\)/) + expect(code).toMatch(/await Promise.resolve\(\{.*\}\)/) + }) + test('@import hoist', async () => { serverLogs.forEach((log) => { // no warning from esbuild css minifier