Skip to content

Conversation

@thecrypticace
Copy link
Contributor

@thecrypticace thecrypticace commented Dec 4, 2025

Companion PR to tailwindlabs/tailwindcss#19405

The aim of this PR is to improve the performance around looking up / compiling candidates into utilities when using v4.

It does this in three stages:

  1. For earlier versions (v4.0–v4.1.17) we swap out PostCSS (mostly) with our own CSS parser. The implementation is tailored to improve performance and memory usage for how we use it. This also means I've been able to optimize how we walk the AST and remove mutations in many places.

There are still some internal APIs that use PostCSS because they interface with multiple versions of Tailwind CSS. These still use PostCSS and we translate our AST into a PostCSS AST for these cases. Eventually these APIs will also be converted to use our internal CSS AST directly — we'll support old versions by converting from PostCSS to our internal AST — that is for a future PR.

  1. For new enough versions, since our AST here mirrors the one in Tailwind CSS, we can use the nodes directly from Tailwind CSS itself instead of generating the code inside Tailwind CSS, serializing to a string, and then finally re-parsing. This is exactly what the candidatesToAst enables (will ship in Tailwind CSS v4.1.18).

  2. A large portion of the candidate compiling perf is spent on color lookups. The above will help with that significantly but there's still a large amount of time analyzing declaration values for colors. This PR replaces our unwieldy color regex to pick out potential colors with a fine tuned parser.

This speedup gives us more headroom to possibly re-enable mask utility color swatches. The overhead from parsing an extra 5k utilities is still there and still fairly significant but it is now much lower than before.

Fixes #1295

It’s not used
This is intended to mirror the AST implementation of Tailwind CSS v4.1.18+
A few APIs are shared across versions and rely on PostCSS nodes. We’ll pass in PostCSS ASTs to those APIs by translating our AST to PostCSS’s AST.

This new setup is also intended to remove AST manipulation wherever possible. We build data structures and can skip over ignored nodes when walking. The eventual goal is to reduce memory usage by using nodes returned by Tailwind CSS’s internal cache.
@thecrypticace thecrypticace force-pushed the feat/candidates-to-ast branch from bf0ad36 to d48915b Compare December 5, 2025 17:37
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.

Color swatches missing from mask-* utilities

2 participants