-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(css): only run postcss when needed #19061
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1272,7 +1272,6 @@ async function compileCSS( | |||||
): Promise<{ | ||||||
code: string | ||||||
map?: SourceMapInput | ||||||
ast?: PostCSS.Result | ||||||
modules?: Record<string, string> | ||||||
deps?: Set<string> | ||||||
}> { | ||||||
|
@@ -1281,51 +1280,49 @@ async function compileCSS( | |||||
return compileLightningCSS(id, code, environment, urlReplacer) | ||||||
} | ||||||
|
||||||
const lang = CSS_LANGS_RE.exec(id)?.[1] as CssLang | undefined | ||||||
const deps = new Set<string>() | ||||||
|
||||||
// pre-processors: sass etc. | ||||||
let preprocessorMap: ExistingRawSourceMap | undefined | ||||||
if (isPreProcessor(lang)) { | ||||||
const preprocessorResult = await compileCSSPreprocessors( | ||||||
environment, | ||||||
id, | ||||||
lang, | ||||||
code, | ||||||
workerController, | ||||||
) | ||||||
code = preprocessorResult.code | ||||||
preprocessorMap = preprocessorResult.map | ||||||
preprocessorResult.deps?.forEach((dep) => deps.add(dep)) | ||||||
} | ||||||
|
||||||
const { modules: modulesOptions, devSourcemap } = config.css | ||||||
const isModule = modulesOptions !== false && cssModuleRE.test(id) | ||||||
// although at serve time it can work without processing, we do need to | ||||||
// crawl them in order to register watch dependencies. | ||||||
const needInlineImport = code.includes('@import') | ||||||
const hasUrl = cssUrlRE.test(code) || cssImageSetRE.test(code) | ||||||
const lang = CSS_LANGS_RE.exec(id)?.[1] as CssLang | undefined | ||||||
const postcssConfig = await resolvePostcssConfig( | ||||||
environment.getTopLevelConfig(), | ||||||
) | ||||||
|
||||||
// 1. plain css that needs no processing | ||||||
// postcss processing is not needed | ||||||
if ( | ||||||
lang === 'css' && | ||||||
lang !== 'sss' && | ||||||
!postcssConfig && | ||||||
!isModule && | ||||||
!needInlineImport && | ||||||
!hasUrl | ||||||
) { | ||||||
return { code, map: null } | ||||||
return { code, map: preprocessorMap ?? null, deps } | ||||||
} | ||||||
|
||||||
let modules: Record<string, string> | undefined | ||||||
const deps = new Set<string>() | ||||||
|
||||||
// 2. pre-processors: sass etc. | ||||||
let preprocessorMap: ExistingRawSourceMap | undefined | ||||||
if (isPreProcessor(lang)) { | ||||||
const preprocessorResult = await compileCSSPreprocessors( | ||||||
environment, | ||||||
id, | ||||||
lang, | ||||||
code, | ||||||
workerController, | ||||||
) | ||||||
code = preprocessorResult.code | ||||||
preprocessorMap = preprocessorResult.map | ||||||
preprocessorResult.deps?.forEach((dep) => deps.add(dep)) | ||||||
} | ||||||
|
||||||
// 3. postcss | ||||||
// postcss | ||||||
const atImportResolvers = getAtImportResolvers( | ||||||
environment.getTopLevelConfig(), | ||||||
) | ||||||
const postcssOptions = postcssConfig?.options ?? {} | ||||||
const postcssPlugins = postcssConfig?.plugins.slice() ?? [] | ||||||
|
||||||
if (needInlineImport) { | ||||||
|
@@ -1387,7 +1384,13 @@ async function compileCSS( | |||||
) | ||||||
} | ||||||
|
||||||
if (urlReplacer) { | ||||||
if ( | ||||||
urlReplacer && | ||||||
// if there's an @import, we need to add this plugin | ||||||
// regradless of whether it contains url() or image-set(), | ||||||
// because we don't know the content referenced by @import | ||||||
(needInlineImport || cssUrlRE.test(code) || cssImageSetRE.test(code)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It seems like we can reuse the variable here |
||||||
) { | ||||||
postcssPlugins.push( | ||||||
UrlRewritePostcssPlugin({ | ||||||
replacer: urlReplacer, | ||||||
Comment on lines
+1387
to
1396
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
@@ -1396,6 +1399,8 @@ async function compileCSS( | |||||
) | ||||||
} | ||||||
|
||||||
let modules: Record<string, string> | undefined | ||||||
|
||||||
if (isModule) { | ||||||
postcssPlugins.unshift( | ||||||
(await importPostcssModules()).default({ | ||||||
|
@@ -1429,7 +1434,7 @@ async function compileCSS( | |||||
) | ||||||
} | ||||||
|
||||||
if (!postcssPlugins.length) { | ||||||
if (lang !== 'sss' && !postcssPlugins.length) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused about this part. Isn't sugarss support added as a postcss plugin, so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because sugarss is a parser and not a plugin. vite/packages/vite/src/node/plugins/css.ts Line 1447 in da0caf5
Now that you mentioned about it, I think we should change this condition to lang !== 'sss' && !postcssOptions.parser && !postcssPlugins.length .
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I didn't know it's a parser. Yeah I think checking for |
||||||
return { | ||||||
code, | ||||||
map: preprocessorMap, | ||||||
|
@@ -1441,6 +1446,8 @@ async function compileCSS( | |||||
try { | ||||||
const source = removeDirectQuery(id) | ||||||
const postcss = await importPostcss() | ||||||
const postcssOptions = postcssConfig?.options ?? {} | ||||||
|
||||||
// postcss is an unbundled dep and should be lazy imported | ||||||
postcssResult = await postcss.default(postcssPlugins).process(code, { | ||||||
...postcssOptions, | ||||||
|
@@ -1510,7 +1517,6 @@ async function compileCSS( | |||||
|
||||||
if (!devSourcemap) { | ||||||
return { | ||||||
ast: postcssResult, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
code: postcssResult.css, | ||||||
map: { mappings: '' }, | ||||||
modules, | ||||||
|
@@ -1528,7 +1534,6 @@ async function compileCSS( | |||||
) | ||||||
|
||||||
return { | ||||||
ast: postcssResult, | ||||||
code: postcssResult.css, | ||||||
map: combineSourcemapsIfExists(cleanUrl(id), postcssMap, preprocessorMap), | ||||||
modules, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this PR, If the preprocessor removed all
@import
, we might be able to return at this line. (previously postcss was run)