Skip to content

Commit 58e99b6

Browse files
authored
perf(css): improve postcss config resolve (#12484)
1 parent 1502617 commit 58e99b6

File tree

1 file changed

+22
-31
lines changed
  • packages/vite/src/node/plugins

1 file changed

+22
-31
lines changed

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

+22-31
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ export const removedPureCssFilesCache = new WeakMap<
166166
Map<string, RenderedChunk>
167167
>()
168168

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+
>()
173173

174174
function encodePublicUrlsInCSS(config: ResolvedConfig) {
175175
return config.command === 'build'
@@ -188,6 +188,9 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
188188
extensions: [],
189189
})
190190

191+
// warm up cache for resolved postcss config
192+
resolvePostcssConfig(config)
193+
191194
return {
192195
name: 'vite:css',
193196

@@ -803,7 +806,7 @@ async function compileCSS(
803806
const needInlineImport = code.includes('@import')
804807
const hasUrl = cssUrlRE.test(code) || cssImageSetRE.test(code)
805808
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)
807810

808811
// 1. plain css that needs no processing
809812
if (
@@ -884,14 +887,6 @@ async function compileCSS(
884887
// 3. postcss
885888
const postcssOptions = (postcssConfig && postcssConfig.options) || {}
886889

887-
// for sugarss change parser
888-
if (lang === 'sss') {
889-
postcssOptions.parser = loadPreprocessor(
890-
PostCssDialectLang.sss,
891-
config.root,
892-
)
893-
}
894-
895890
const postcssPlugins =
896891
postcssConfig && postcssConfig.plugins ? postcssConfig.plugins.slice() : []
897892

@@ -974,6 +969,10 @@ async function compileCSS(
974969
.default(postcssPlugins)
975970
.process(code, {
976971
...postcssOptions,
972+
parser:
973+
lang === 'sss'
974+
? loadPreprocessor(PostCssDialectLang.sss, config.root)
975+
: postcssOptions.parser,
977976
to: source,
978977
from: source,
979978
...(devSourcemap
@@ -1139,16 +1138,10 @@ interface PostCSSConfigResult {
11391138

11401139
async function resolvePostcssConfig(
11411140
config: ResolvedConfig,
1142-
dialect = 'css',
11431141
): 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)
11501143
if (result !== undefined) {
1151-
return result
1144+
return await result
11521145
}
11531146

11541147
// inline postcss config via vite config
@@ -1164,9 +1157,7 @@ async function resolvePostcssConfig(
11641157
} else {
11651158
const searchPath =
11661159
typeof inlineOptions === 'string' ? inlineOptions : config.root
1167-
try {
1168-
result = await postcssrc({}, searchPath)
1169-
} catch (e) {
1160+
result = postcssrc({}, searchPath).catch((e) => {
11701161
if (!/No PostCSS Config found/.test(e.message)) {
11711162
if (e instanceof Error) {
11721163
const { name, message, stack } = e
@@ -1178,11 +1169,15 @@ async function resolvePostcssConfig(
11781169
throw new Error(`Failed to load PostCSS config: ${e}`)
11791170
}
11801171
}
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+
})
11831178
}
11841179

1185-
postcssConfigCache[dialect].set(config, result)
1180+
postcssConfigCache.set(config, result)
11861181
return result
11871182
}
11881183

@@ -1977,7 +1972,3 @@ const preProcessors = Object.freeze({
19771972
function isPreProcessor(lang: any): lang is PreprocessLang {
19781973
return lang && lang in preProcessors
19791974
}
1980-
1981-
function getCssDialect(lang: CssLang | undefined): string {
1982-
return lang === 'sss' ? 'sss' : 'css'
1983-
}

0 commit comments

Comments
 (0)