Skip to content

Commit fa53580

Browse files
authored
fix: postcssrc cache may cause memory leak (#4604)
1 parent d284314 commit fa53580

File tree

1 file changed

+15
-10
lines changed
  • packages/core/src/plugins

1 file changed

+15
-10
lines changed

packages/core/src/plugins/css.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ export const normalizeCssLoaderOptions = (
8383
return options;
8484
};
8585

86-
const userPostcssrcCache = new Map<
87-
string,
88-
PostCSSOptions | Promise<PostCSSOptions>
89-
>();
90-
9186
// Create a new config object,
9287
// ensure isolation of config objects between different builds
9388
const clonePostCSSConfig = (config: PostCSSOptions) => ({
@@ -100,8 +95,11 @@ const getCSSSourceMap = (config: NormalizedEnvironmentConfig): boolean => {
10095
return typeof sourceMap === 'boolean' ? sourceMap : sourceMap.css;
10196
};
10297

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);
105103

106104
if (cached) {
107105
return clonePostCSSConfig(await cached);
@@ -119,10 +117,10 @@ async function loadUserPostcssrc(root: string): Promise<PostCSSOptions> {
119117
throw err;
120118
});
121119

122-
userPostcssrcCache.set(root, promise);
120+
postcssrcCache.set(root, promise);
123121

124122
return promise.then((config: PostCSSOptions) => {
125-
userPostcssrcCache.set(root, config);
123+
postcssrcCache.set(root, config);
126124
return clonePostCSSConfig(config);
127125
});
128126
}
@@ -136,9 +134,11 @@ const isPostcssPluginCreator = (
136134
const getPostcssLoaderOptions = async ({
137135
config,
138136
root,
137+
postcssrcCache,
139138
}: {
140139
config: NormalizedEnvironmentConfig;
141140
root: string;
141+
postcssrcCache: PostcssrcCache;
142142
}): Promise<PostCSSLoaderOptions> => {
143143
const extraPlugins: AcceptedPlugin[] = [];
144144

@@ -148,7 +148,7 @@ const getPostcssLoaderOptions = async ({
148148
},
149149
};
150150

151-
const userOptions = await loadUserPostcssrc(root);
151+
const userOptions = await loadUserPostcssrc(root, postcssrcCache);
152152

153153
// init the plugins array
154154
userOptions.plugins ||= [];
@@ -254,9 +254,13 @@ const getCSSLoaderOptions = ({
254254
return cssLoaderOptions;
255255
};
256256

257+
type PostcssrcCache = Map<string, PostCSSOptions | Promise<PostCSSOptions>>;
258+
257259
export const pluginCss = (): RsbuildPlugin => ({
258260
name: 'rsbuild:css',
259261
setup(api) {
262+
const postcssrcCache: PostcssrcCache = new Map();
263+
260264
api.modifyBundlerChain({
261265
order: 'pre',
262266
handler: async (chain, { target, isProd, CHAIN_ID, environment }) => {
@@ -327,6 +331,7 @@ export const pluginCss = (): RsbuildPlugin => ({
327331
const postcssLoaderOptions = await getPostcssLoaderOptions({
328332
config,
329333
root: api.context.rootPath,
334+
postcssrcCache,
330335
});
331336

332337
// enable postcss-loader if using PostCSS plugins

0 commit comments

Comments
 (0)