From d334b3de8cfc968481189643a7ad9baba0c7c36a Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 8 Sep 2023 15:07:46 +0800 Subject: [PATCH] fix(css): reset render cache on renderStart (#14326) --- packages/vite/src/node/plugins/css.ts | 2 +- playground/lib/__tests__/serve.ts | 6 +++ playground/lib/src/main-multiple-output.js | 6 +++ playground/lib/src/sub-multiple-output.js | 6 +++ playground/lib/vite.multiple-output.config.js | 39 +++++++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 playground/lib/src/main-multiple-output.js create mode 100644 playground/lib/src/sub-multiple-output.js create mode 100644 playground/lib/vite.multiple-output.config.js diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 36ca03fe5a676a..f1ae186932d4ee 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -406,7 +406,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { return { name: 'vite:css-post', - buildStart() { + renderStart() { // Ensure new caches for every build (i.e. rebuilding in watch mode) pureCssChunks = new Set() outputToExtractedCSSMap = new Map() diff --git a/playground/lib/__tests__/serve.ts b/playground/lib/__tests__/serve.ts index 44e1728ffd6086..e07efe526eaf00 100644 --- a/playground/lib/__tests__/serve.ts +++ b/playground/lib/__tests__/serve.ts @@ -61,6 +61,12 @@ export async function serve(): Promise<{ close(): Promise }> { configFile: path.resolve(__dirname, '../vite.dyimport.config.js'), }) + await build({ + root: rootDir, + logLevel: 'warn', // output esbuild warns + configFile: path.resolve(__dirname, '../vite.multiple-output.config.js'), + }) + await build({ root: rootDir, logLevel: 'warn', // output esbuild warns diff --git a/playground/lib/src/main-multiple-output.js b/playground/lib/src/main-multiple-output.js new file mode 100644 index 00000000000000..78e22283ccbc1b --- /dev/null +++ b/playground/lib/src/main-multiple-output.js @@ -0,0 +1,6 @@ +// import file to test css build handling +import './index.css' + +export default async function message(sel) { + document.querySelector(sel).textContent = 'success' +} diff --git a/playground/lib/src/sub-multiple-output.js b/playground/lib/src/sub-multiple-output.js new file mode 100644 index 00000000000000..78e22283ccbc1b --- /dev/null +++ b/playground/lib/src/sub-multiple-output.js @@ -0,0 +1,6 @@ +// import file to test css build handling +import './index.css' + +export default async function message(sel) { + document.querySelector(sel).textContent = 'success' +} diff --git a/playground/lib/vite.multiple-output.config.js b/playground/lib/vite.multiple-output.config.js new file mode 100644 index 00000000000000..e986221ad6ca9b --- /dev/null +++ b/playground/lib/vite.multiple-output.config.js @@ -0,0 +1,39 @@ +import path from 'node:path' +import { defineConfig } from 'vite' + +const root = process.env.VITEST + ? path.resolve(__dirname, '../../playground-temp/lib') + : __dirname + +export default defineConfig({ + build: { + lib: { + // set multiple entrypoint to trigger css chunking + entry: { + main: path.resolve(__dirname, 'src/main-multiple-output.js'), + sub: path.resolve(__dirname, 'src/sub-multiple-output.js'), + }, + name: 'MyLib', + }, + outDir: 'dist/multiple-output', + rollupOptions: { + // due to playground-temp, the `dir` needs to be relative to the resolvedRoot + output: [ + { + dir: path.resolve(root, 'dist/multiple-output/es'), + format: 'es', + entryFileNames: 'index.mjs', + assetFileNames: 'assets/mylib.css', + }, + { + dir: path.resolve(root, 'dist/multiple-output/cjs'), + format: 'cjs', + entryFileNames: 'index.cjs', + assetFileNames: 'assets/mylib.css', + }, + ], + }, + cssCodeSplit: true, + }, + cacheDir: 'node_modules/.vite-multiple-output', +})