Skip to content

Commit 07ad2b4

Browse files
authored
fix: ensure new CSS modules cache at build start (#3516)
1 parent c9da635 commit 07ad2b4

File tree

1 file changed

+18
-7
lines changed
  • packages/vite/src/node/plugins

1 file changed

+18
-7
lines changed

packages/vite/src/node/plugins/css.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ export const chunkToEmittedCssFileMap = new WeakMap<
118118
*/
119119
export function cssPlugin(config: ResolvedConfig): Plugin {
120120
let server: ViteDevServer
121-
const moduleCache = new Map<string, Record<string, string>>()
122-
cssModulesCache.set(config, moduleCache)
121+
let moduleCache: Map<string, Record<string, string>>
123122

124123
const resolveUrl = config.createResolver({
125124
preferRelative: true,
@@ -135,6 +134,12 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
135134
server = _server
136135
},
137136

137+
buildStart() {
138+
// Ensure a new cache for every build (i.e. rebuilding in watch mode)
139+
moduleCache = new Map<string, Record<string, string>>()
140+
cssModulesCache.set(config, moduleCache)
141+
},
142+
138143
async transform(raw, id) {
139144
if (!cssLangRE.test(id) || commonjsProxyRE.test(id)) {
140145
return
@@ -225,24 +230,30 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
225230
* Plugin applied after user plugins
226231
*/
227232
export function cssPostPlugin(config: ResolvedConfig): Plugin {
228-
const styles = new Map<string, string>()
229-
const pureCssChunks = new Set<string>()
230-
const moduleCache = cssModulesCache.get(config)!
233+
let styles: Map<string, string>
234+
let pureCssChunks: Set<string>
231235

232236
// when there are multiple rollup outputs and extracting CSS, only emit once,
233237
// since output formats have no effect on the generated CSS.
234-
const outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>()
238+
let outputToExtractedCSSMap: Map<NormalizedOutputOptions, string>
235239
let hasEmitted = false
236240

237241
return {
238242
name: 'vite:css-post',
239243

244+
buildStart() {
245+
// Ensure new caches for every build (i.e. rebuilding in watch mode)
246+
styles = new Map<string, string>()
247+
pureCssChunks = new Set<string>()
248+
outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>()
249+
},
250+
240251
transform(css, id, ssr) {
241252
if (!cssLangRE.test(id) || commonjsProxyRE.test(id)) {
242253
return
243254
}
244255

245-
const modules = moduleCache.get(id)
256+
const modules = cssModulesCache.get(config)!.get(id)
246257
const modulesCode =
247258
modules && dataToEsm(modules, { namedExports: true, preferConst: true })
248259

0 commit comments

Comments
 (0)