Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 40 additions & 32 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,6 @@ async function compileCSS(
): Promise<{
code: string
map?: SourceMapInput
ast?: PostCSS.Result
modules?: Record<string, string>
deps?: Set<string>
}> {
Expand All @@ -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 }
}

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))
return { code, map: preprocessorMap ?? null, deps }
}

// 3. postcss
// postcss
const atImportResolvers = getAtImportResolvers(
environment.getTopLevelConfig(),
)
const postcssOptions = postcssConfig?.options ?? {}
const postcssPlugins = postcssConfig?.plugins.slice() ?? []

if (needInlineImport) {
Expand Down Expand Up @@ -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 || hasUrl)
) {
postcssPlugins.push(
UrlRewritePostcssPlugin({
replacer: urlReplacer,
Expand All @@ -1396,6 +1399,8 @@ async function compileCSS(
)
}

let modules: Record<string, string> | undefined

if (isModule) {
postcssPlugins.unshift(
(await importPostcssModules()).default({
Expand Down Expand Up @@ -1429,7 +1434,11 @@ async function compileCSS(
)
}

if (!postcssPlugins.length) {
const postcssOptions = postcssConfig?.options ?? {}
const postcssParser =
lang === 'sss' ? loadSss(config.root) : postcssOptions.parser

if (!postcssPlugins.length && !postcssParser) {
return {
code,
map: preprocessorMap,
Expand All @@ -1441,10 +1450,11 @@ async function compileCSS(
try {
const source = removeDirectQuery(id)
const postcss = await importPostcss()

// postcss is an unbundled dep and should be lazy imported
postcssResult = await postcss.default(postcssPlugins).process(code, {
...postcssOptions,
parser: lang === 'sss' ? loadSss(config.root) : postcssOptions.parser,
parser: postcssParser,
to: source,
from: source,
...(devSourcemap
Expand Down Expand Up @@ -1510,7 +1520,6 @@ async function compileCSS(

if (!devSourcemap) {
return {
ast: postcssResult,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ast was not used anywhere.

code: postcssResult.css,
map: { mappings: '' },
modules,
Expand All @@ -1528,7 +1537,6 @@ async function compileCSS(
)

return {
ast: postcssResult,
code: postcssResult.css,
map: combineSourcemapsIfExists(cleanUrl(id), postcssMap, preprocessorMap),
modules,
Expand Down Expand Up @@ -2150,8 +2158,8 @@ function loadSassPackage(root: string): {
}
}

let cachedSss: any
function loadSss(root: string) {
let cachedSss: PostCSS.Syntax
function loadSss(root: string): PostCSS.Syntax {
if (cachedSss) return cachedSss

const sssPath = loadPreprocessorPath(PostCssDialectLang.sss, root)
Expand Down
8 changes: 4 additions & 4 deletions playground/css-sourcemap/__tests__/css-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ describe.runIf(isServe)('serve', () => {
const map = extractSourcemap(css)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"ignoreList": [],
"mappings": "AAGE;EACE,UCJM",
"mappings": "AAGE;EACE,OCJM",
"sourceRoot": "",
"sources": [
"/root/imported.sass",
"/root/imported-nested.sass",
Expand Down Expand Up @@ -186,7 +186,7 @@ describe.runIf(isServe)('serve', () => {
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"ignoreList": [],
"mappings": "AACE;EACE",
"mappings": "AACE,SAAC;EACC",
"sources": [
"/root/imported.less",
],
Expand All @@ -209,7 +209,7 @@ describe.runIf(isServe)('serve', () => {
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"ignoreList": [],
"mappings": "AACE;EACE,cAAM",
"mappings": "AACE;EACE,OAAM,QAAN",
"sources": [
"/root/imported.styl",
],
Expand Down
Loading