@@ -166,10 +166,10 @@ export const removedPureCssFilesCache = new WeakMap<
166
166
Map < string , RenderedChunk >
167
167
> ( )
168
168
169
- const postcssConfigCache : Record <
170
- string ,
171
- WeakMap < ResolvedConfig , PostCSSConfigResult | null >
172
- > = { }
169
+ const postcssConfigCache = new WeakMap <
170
+ ResolvedConfig ,
171
+ PostCSSConfigResult | null | Promise < PostCSSConfigResult | null >
172
+ > ( )
173
173
174
174
function encodePublicUrlsInCSS ( config : ResolvedConfig ) {
175
175
return config . command === 'build'
@@ -188,6 +188,9 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
188
188
extensions : [ ] ,
189
189
} )
190
190
191
+ // warm up cache for resolved postcss config
192
+ resolvePostcssConfig ( config )
193
+
191
194
return {
192
195
name : 'vite:css' ,
193
196
@@ -803,7 +806,7 @@ async function compileCSS(
803
806
const needInlineImport = code . includes ( '@import' )
804
807
const hasUrl = cssUrlRE . test ( code ) || cssImageSetRE . test ( code )
805
808
const lang = id . match ( CSS_LANGS_RE ) ?. [ 1 ] as CssLang | undefined
806
- const postcssConfig = await resolvePostcssConfig ( config , getCssDialect ( lang ) )
809
+ const postcssConfig = await resolvePostcssConfig ( config )
807
810
808
811
// 1. plain css that needs no processing
809
812
if (
@@ -884,14 +887,6 @@ async function compileCSS(
884
887
// 3. postcss
885
888
const postcssOptions = ( postcssConfig && postcssConfig . options ) || { }
886
889
887
- // for sugarss change parser
888
- if ( lang === 'sss' ) {
889
- postcssOptions . parser = loadPreprocessor (
890
- PostCssDialectLang . sss ,
891
- config . root ,
892
- )
893
- }
894
-
895
890
const postcssPlugins =
896
891
postcssConfig && postcssConfig . plugins ? postcssConfig . plugins . slice ( ) : [ ]
897
892
@@ -974,6 +969,10 @@ async function compileCSS(
974
969
. default ( postcssPlugins )
975
970
. process ( code , {
976
971
...postcssOptions ,
972
+ parser :
973
+ lang === 'sss'
974
+ ? loadPreprocessor ( PostCssDialectLang . sss , config . root )
975
+ : postcssOptions . parser ,
977
976
to : source ,
978
977
from : source ,
979
978
...( devSourcemap
@@ -1139,16 +1138,10 @@ interface PostCSSConfigResult {
1139
1138
1140
1139
async function resolvePostcssConfig (
1141
1140
config : ResolvedConfig ,
1142
- dialect = 'css' ,
1143
1141
) : Promise < PostCSSConfigResult | null > {
1144
- postcssConfigCache [ dialect ] ??= new WeakMap <
1145
- ResolvedConfig ,
1146
- PostCSSConfigResult | null
1147
- > ( )
1148
-
1149
- let result = postcssConfigCache [ dialect ] . get ( config )
1142
+ let result = postcssConfigCache . get ( config )
1150
1143
if ( result !== undefined ) {
1151
- return result
1144
+ return await result
1152
1145
}
1153
1146
1154
1147
// inline postcss config via vite config
@@ -1164,9 +1157,7 @@ async function resolvePostcssConfig(
1164
1157
} else {
1165
1158
const searchPath =
1166
1159
typeof inlineOptions === 'string' ? inlineOptions : config . root
1167
- try {
1168
- result = await postcssrc ( { } , searchPath )
1169
- } catch ( e ) {
1160
+ result = postcssrc ( { } , searchPath ) . catch ( ( e ) => {
1170
1161
if ( ! / N o P o s t C S S C o n f i g f o u n d / . test ( e . message ) ) {
1171
1162
if ( e instanceof Error ) {
1172
1163
const { name, message, stack } = e
@@ -1178,11 +1169,15 @@ async function resolvePostcssConfig(
1178
1169
throw new Error ( `Failed to load PostCSS config: ${ e } ` )
1179
1170
}
1180
1171
}
1181
- result = null
1182
- }
1172
+ return null
1173
+ } )
1174
+ // replace cached promise to result object when finished
1175
+ result . then ( ( resolved ) => {
1176
+ postcssConfigCache . set ( config , resolved )
1177
+ } )
1183
1178
}
1184
1179
1185
- postcssConfigCache [ dialect ] . set ( config , result )
1180
+ postcssConfigCache . set ( config , result )
1186
1181
return result
1187
1182
}
1188
1183
@@ -1977,7 +1972,3 @@ const preProcessors = Object.freeze({
1977
1972
function isPreProcessor ( lang : any ) : lang is PreprocessLang {
1978
1973
return lang && lang in preProcessors
1979
1974
}
1980
-
1981
- function getCssDialect ( lang : CssLang | undefined ) : string {
1982
- return lang === 'sss' ? 'sss' : 'css'
1983
- }
0 commit comments