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

Improve performance of @tailwindcss/postcss and @tailwindcss/vite #15226

Merged
merged 16 commits into from
Nov 29, 2024

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Nov 28, 2024

This PR improves the performance of the @tailwindcss/postcss and @tailwindcss/vite implementations.

The issue is that in some scenarios, if you have multiple .css files, then all of the CSS files are ran through the Tailwind CSS compiler. The issue with this is that in a lot of cases, the CSS files aren't even related to Tailwind CSS at all.

E.g.: in a Next.js project, if you use the next/font/local tool, then every font you used will be in a separate CSS file. This means that we run Tailwind CSS in all these files as well.

That said, running Tailwind CSS on these files isn't the end of the world because we still need to handle @import in case @tailwind utilities is being used. However, we also run the auto source detection logic for every CSS file in the system. This part is bad.

To solve this, this PR introduces an internal features to collect what CSS features are used throughout the system (@import, @plugin, @apply, @tailwind utilities, etc…)

The @tailwindcss/postcss and @tailwindcss/vite plugin can use that information to decide if they can take some shortcuts or not.


Overall, this means that we don't run the slow parts of Tailwind CSS if we don't need to.

Whenever we return `WalkAction.Stop` we would stop the _current_ loop
only, but if your parent had more siblings, we would still loop over all
of its siblings.

Our contract with the `walk(…)` function looks like this:

- `WalkAction.Continue` (default) — continue walking
- `WalkAction.Skip` — skip the current node (aka, don't visit your child nodes) but continue your sibling nodes.
- `WalkAction.Stop` — skip walking entirely.

This commit fixes that by forwarding the `WalkAction.Stop` up the call
stack to ensure parent `walk(…)` invocations _also_ stop. This ensures
that we don't go into any sibling nodes of your parent (or grand parent)
node(s).
It could be that the Tailwind compiler was invoked with a CSS function
that doesn't use Tailwind at all. This can happen in places like the
PostCSS or Vite plugin where _every_ CSS file is processed.

The type of the CSS file can be any of 3 types. The exact naming is to
be determined. But the idea is:

1. `none` — nothing related to Tailwind CSS is being used. This means
   that we can skip handling this file.
2. `static` — Some Tailwind CSS features are used such as `@apply` or
   the `theme(…)` function. Tailwind still has to handle these
   situations. The important part here is that `@tailwind utilities` was
   not used. This means that we don't even have to scan for candidates
   at all.
4. `full` — `@tailwind utilities` was used, this means that we have to
   scan for candidates and do a lot more I/O.
used

If you have a lot of files (e.g.: Vue `<style>` blocks), then we would
also inject the license header but that's not necessary for _every_
file. Only for the files that actually print out the utilities.
@RobinMalfait RobinMalfait requested a review from a team as a code owner November 28, 2024 14:14
CHANGELOG.md Outdated
- Nothing yet!
### Fixed

- Improve performance of `@tailwindcss/postcss` and `@tailwindcss/vite` ([#15226](https://github.com/tailwindlabs/tailwindcss/pull/15226))
Copy link
Member

Choose a reason for hiding this comment

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

Let's provide some info here on how it was improved and explain that we only process files that use tailwind features or so

CHANGELOG.md Outdated Show resolved Hide resolved
@adamwathan adamwathan merged commit 99b73ee into next Nov 29, 2024
1 check passed
@adamwathan adamwathan deleted the fix/improve-postcss-and-vite-performance branch November 29, 2024 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants