@@ -83,11 +83,6 @@ export const normalizeCssLoaderOptions = (
83
83
return options ;
84
84
} ;
85
85
86
- const userPostcssrcCache = new Map <
87
- string ,
88
- PostCSSOptions | Promise < PostCSSOptions >
89
- > ( ) ;
90
-
91
86
// Create a new config object,
92
87
// ensure isolation of config objects between different builds
93
88
const clonePostCSSConfig = ( config : PostCSSOptions ) => ( {
@@ -100,8 +95,11 @@ const getCSSSourceMap = (config: NormalizedEnvironmentConfig): boolean => {
100
95
return typeof sourceMap === 'boolean' ? sourceMap : sourceMap . css ;
101
96
} ;
102
97
103
- async function loadUserPostcssrc ( root : string ) : Promise < PostCSSOptions > {
104
- const cached = userPostcssrcCache . get ( root ) ;
98
+ async function loadUserPostcssrc (
99
+ root : string ,
100
+ postcssrcCache : PostcssrcCache ,
101
+ ) : Promise < PostCSSOptions > {
102
+ const cached = postcssrcCache . get ( root ) ;
105
103
106
104
if ( cached ) {
107
105
return clonePostCSSConfig ( await cached ) ;
@@ -119,10 +117,10 @@ async function loadUserPostcssrc(root: string): Promise<PostCSSOptions> {
119
117
throw err ;
120
118
} ) ;
121
119
122
- userPostcssrcCache . set ( root , promise ) ;
120
+ postcssrcCache . set ( root , promise ) ;
123
121
124
122
return promise . then ( ( config : PostCSSOptions ) => {
125
- userPostcssrcCache . set ( root , config ) ;
123
+ postcssrcCache . set ( root , config ) ;
126
124
return clonePostCSSConfig ( config ) ;
127
125
} ) ;
128
126
}
@@ -136,9 +134,11 @@ const isPostcssPluginCreator = (
136
134
const getPostcssLoaderOptions = async ( {
137
135
config,
138
136
root,
137
+ postcssrcCache,
139
138
} : {
140
139
config : NormalizedEnvironmentConfig ;
141
140
root : string ;
141
+ postcssrcCache : PostcssrcCache ;
142
142
} ) : Promise < PostCSSLoaderOptions > => {
143
143
const extraPlugins : AcceptedPlugin [ ] = [ ] ;
144
144
@@ -148,7 +148,7 @@ const getPostcssLoaderOptions = async ({
148
148
} ,
149
149
} ;
150
150
151
- const userOptions = await loadUserPostcssrc ( root ) ;
151
+ const userOptions = await loadUserPostcssrc ( root , postcssrcCache ) ;
152
152
153
153
// init the plugins array
154
154
userOptions . plugins ||= [ ] ;
@@ -254,9 +254,13 @@ const getCSSLoaderOptions = ({
254
254
return cssLoaderOptions ;
255
255
} ;
256
256
257
+ type PostcssrcCache = Map < string , PostCSSOptions | Promise < PostCSSOptions > > ;
258
+
257
259
export const pluginCss = ( ) : RsbuildPlugin => ( {
258
260
name : 'rsbuild:css' ,
259
261
setup ( api ) {
262
+ const postcssrcCache : PostcssrcCache = new Map ( ) ;
263
+
260
264
api . modifyBundlerChain ( {
261
265
order : 'pre' ,
262
266
handler : async ( chain , { target, isProd, CHAIN_ID , environment } ) => {
@@ -327,6 +331,7 @@ export const pluginCss = (): RsbuildPlugin => ({
327
331
const postcssLoaderOptions = await getPostcssLoaderOptions ( {
328
332
config,
329
333
root : api . context . rootPath ,
334
+ postcssrcCache,
330
335
} ) ;
331
336
332
337
// enable postcss-loader if using PostCSS plugins
0 commit comments