@@ -118,8 +118,7 @@ export const chunkToEmittedCssFileMap = new WeakMap<
118
118
*/
119
119
export function cssPlugin ( config : ResolvedConfig ) : Plugin {
120
120
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 > >
123
122
124
123
const resolveUrl = config . createResolver ( {
125
124
preferRelative : true ,
@@ -135,6 +134,12 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
135
134
server = _server
136
135
} ,
137
136
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
+
138
143
async transform ( raw , id ) {
139
144
if ( ! cssLangRE . test ( id ) || commonjsProxyRE . test ( id ) ) {
140
145
return
@@ -225,24 +230,30 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
225
230
* Plugin applied after user plugins
226
231
*/
227
232
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 >
231
235
232
236
// when there are multiple rollup outputs and extracting CSS, only emit once,
233
237
// since output formats have no effect on the generated CSS.
234
- const outputToExtractedCSSMap = new Map < NormalizedOutputOptions , string > ( )
238
+ let outputToExtractedCSSMap : Map < NormalizedOutputOptions , string >
235
239
let hasEmitted = false
236
240
237
241
return {
238
242
name : 'vite:css-post' ,
239
243
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
+
240
251
transform ( css , id , ssr ) {
241
252
if ( ! cssLangRE . test ( id ) || commonjsProxyRE . test ( id ) ) {
242
253
return
243
254
}
244
255
245
- const modules = moduleCache . get ( id )
256
+ const modules = cssModulesCache . get ( config ) ! . get ( id )
246
257
const modulesCode =
247
258
modules && dataToEsm ( modules , { namedExports : true , preferConst : true } )
248
259
0 commit comments