Skip to content

perf(css): only run postcss when needed - #19061

Merged
sapphi-red merged 6 commits into
vitejs:mainfrom
sapphi-red:perf/css-only-run-postcss-when-needed
Jan 24, 2025
Merged

sapphi-red merged 6 commits into
vitejs:mainfrom
sapphi-red:perf/css-only-run-postcss-when-needed

Conversation

@sapphi-red

Copy link
Copy Markdown
Member

Description

@sapphi-red sapphi-red added feat: css p2-nice-to-have Not breaking anything but nice to have (priority) labels Dec 24, 2024
Comment on lines +1310 to 1319
// postcss processing is not needed
if (
lang === 'css' &&
lang !== 'sss' &&
!postcssConfig &&
!isModule &&
!needInlineImport &&
!hasUrl
) {
return { code, map: null }
return { code, map: preprocessorMap ?? null, deps }
}

Copy link
Copy Markdown
Member Author

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)

Comment thread packages/vite/src/node/plugins/css.ts Outdated
Comment on lines 1386 to 1395
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))
) {
postcssPlugins.push(
UrlRewritePostcssPlugin({
replacer: urlReplacer,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

UrlRewritePostcssPlugin was always pushed, so postcss was ran always.

Comment thread packages/vite/src/node/plugins/css.ts Outdated
}

if (!postcssPlugins.length) {
if (lang !== 'sss' && !postcssPlugins.length) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This lang !== 'sss' was not needed because !postcssPlugins.length was always false.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 postcssPlugins.length should be at least one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's because sugarss is a parser and not a plugin.

parser: lang === 'sss' ? loadSss(config.root) : postcssOptions.parser,

Now that you mentioned about it, I think we should change this condition to lang !== 'sss' && !postcssOptions.parser && !postcssPlugins.length.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 parser seems better too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated 👍


if (!devSourcemap) {
return {
ast: postcssResult,

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The sourcemaps changed because postcss does not run on these files anymore.

@sapphi-red

Copy link
Copy Markdown
Member Author

/ecosystem-ci run

@pkg-pr-new

pkg-pr-new Bot commented Dec 25, 2024 •

Copy link
Copy Markdown

Open in Stackblitz

npm i https://pkg.pr.new/vite@19061

commit: d00ce43

@vite-ecosystem-ci

Copy link
Copy Markdown

📝 Ran ecosystem CI on 4c777ac: Open

suite result latest scheduled
histoire ❌ failure ❌ failure
ladle ❌ failure ✅ success
redwoodjs ❌ failure ❌ failure
remix ❌ failure ❌ failure
vite-plugin-svelte ❌ failure ❌ failure
vitest ❌ failure ✅ success
waku ❌ failure ❌ failure

✅ analogjs, astro, laravel, marko, nuxt, previewjs, quasar, qwik, rakkas, storybook, sveltekit, unocss, vike, vite-environment-examples, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-plugin-vue, vite-setup-catalogue, vitepress, vuepress

@sapphi-red

Copy link
Copy Markdown
Member Author

ladle fail is not related to this PR. vitest is failing because of the change in #19004. So it should be fine.

Comment thread packages/vite/src/node/plugins/css.ts Resolved
sapphi-red and others added 3 commits January 7, 2025 17:27
bluwy
bluwy previously approved these changes Jan 7, 2025
patak-cat
patak-cat previously approved these changes Jan 7, 2025
@sapphi-red
sapphi-red dismissed stale reviews from patak-cat and bluwy via d00ce43 January 24, 2025 03:07
@sapphi-red sapphi-red added this to the 6.1 milestone Jan 24, 2025
@sapphi-red
sapphi-red merged commit 30194fa into vitejs:main Jan 24, 2025
@sapphi-red
sapphi-red deleted the perf/css-only-run-postcss-when-needed branch January 24, 2025 03:32
moonlitusun pushed a commit to moonlitusun/vite that referenced this pull request May 25, 2025
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: css p2-nice-to-have Not breaking anything but nice to have (priority) trigger: preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants