From 67ff94b70c0bd9a392a6b8941cfee61004b26970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Thu, 6 Jun 2024 15:51:35 +0900 Subject: [PATCH] fix(ssr): remove pure CSS dynamic import (#17371) --- .../src/node/plugins/importAnalysisBuild.ts | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 22a0d61ef3aae3..ed424d56c49102 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -318,7 +318,64 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { }, generateBundle({ format }, bundle) { - if (format !== 'es' || ssr || isWorker) { + if (format !== 'es') { + return + } + + if (ssr || isWorker) { + const removedPureCssFiles = removedPureCssFilesCache.get(config) + if (removedPureCssFiles && removedPureCssFiles.size > 0) { + for (const file in bundle) { + const chunk = bundle[file] + if (chunk.type === 'chunk' && chunk.code.includes('import')) { + const code = chunk.code + let imports!: ImportSpecifier[] + try { + imports = parseImports(code)[0].filter((i) => i.d > -1) + } catch (e: any) { + const loc = numberToPos(code, e.idx) + this.error({ + name: e.name, + message: e.message, + stack: e.stack, + cause: e.cause, + pos: e.idx, + loc: { ...loc, file: chunk.fileName }, + frame: generateCodeFrame(code, loc), + }) + } + + for (const imp of imports) { + const { + n: name, + s: start, + e: end, + ss: expStart, + se: expEnd, + } = imp + let url = name + if (!url) { + const rawUrl = code.slice(start, end) + if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`) + url = rawUrl.slice(1, -1) + } + if (!url) continue + + const normalizedFile = path.posix.join( + path.posix.dirname(chunk.fileName), + url, + ) + if (removedPureCssFiles.has(normalizedFile)) { + // remove with Promise.resolve({}) while preserving source map location + chunk.code = + chunk.code.slice(0, expStart) + + `Promise.resolve({${''.padEnd(expEnd - expStart - 19, ' ')}})` + + chunk.code.slice(expEnd) + } + } + } + } + } return }