Ensure @variant can be used in JS based APIs#20252
Conversation
…an be used via `@variant` inside JS based APIs (addComponents, etc) This is a regression test to make sure that the functionality introduced by #19480 works. Turns out that this PR handled `@variant` in `addBase`, but we already handle that later. The typography use case is also invalid, because in the typography plugin we don't use `addBase` at all. Handling `@variant` when you call `addBase(…)` can result in bugs if you rely on variants that are defined later. Since we already handle the `@variant` substitution when we compile AST Nodes, this all should work already.
Noticed that we loop over the `ast` to get the `node` out, and we do that twice, for every class we compile. We can reuse the nodes instead.
Confidence Score: 5/5Safe to merge — the only production code change is a single-variable extraction that eliminates a redundant Both changes are low-risk. The No files require special attention. Reviews (1): Last reviewed commit: "reuse nodes" | Re-trigger Greptile |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
Walkthrough
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
This PR doesn't fix any issues, but it does add an integration test (as a regression test) to make sure that
@variantwith default variants and custom variants can be used inside of JS based plugin APIs.In Tailwind CSS v4.3.1 we introduced a PR that handles
@variantin theaddBasePlugin API (#19480). This was a bit of an older PR, but the tests made sense, so it was merged.However, by introducing that PR, we introduced a bug that the
@variantwas handled too early. If you added custom variants later and used it in theaddBase, then you would get an error since the variant isn't available (yet).That issue was fixed by #20247
Now the question remains, why did we even have the original PR when it already worked?
The use case we had was using
@variantas part of the@tailwindcss/typographyplugin configuration for one of our templates. I was indeed able to reproduce the issue where@variant lgwas seen in the output CSS file.Turns out that this template was using
@tailwindcss/typography+@variantin the configuration, but it was also using Tailwind CSS v4.1.15.Upgrading to the latest version automagically fixed the issue we had. This is also the behavior you can see in the integration test.
The correct behavior was introduced in an even older PR #19263
All that said, everything should work in the next release related to
@variantusages inside JS based APIs.Tiny improvement
While debugging what's going on, I noticed that we looped over the AST to get some nodes out and we did that twice. This PR also improves that by re-using the same list of nodes instead of computing it twice. This won't have a huge impact, but it happened while compiling every single utility which is not ideal.
Test plan
@variantin the output CSS fileInput:

Before:

After:
