Repository navigation
fix(compiler): scope single-definition variable inlining to the declaring block - #592
Merged
Merged
Conversation
…ring block The inliner folded a custom property declared once into every var() in the stylesheet, whether or not the reference's element can hold the declaration. It now folds a value only into the block that declares it, or from a declaration outside @media and @container that reaches every element: `*`, or `:root`, `:host` and `html` for an inheriting property. An @supports block counts as extractRule decides it, through one rulesTheCompilerKeeps, so a dropped block is emptied before the census; a registered `*` property that nothing declares folds to its initial value; and a declaration is removed once no rule reads it. Values the fold no longer normalises reach the token path, which now reads `0` and `false` as values and gives a <ratio> the descriptor parseAspectRatio gives it. A compiled box-shadow drops a transparent shadow, as the runtime does.
Removes the inliner's emptying of @supports blocks the compiler drops, and rulesTheCompilerKeeps with it, so extractRule decides them through supportsConditionValid as on main. It is a second bug (a dropped block's declaration still counts as one) and not the scope bug this PR fixes. Tailwind is the only producer of such a block, its @layer properties fallback, and it repeats each @Property initial value there, so folding the fallback gives the same values as the registration. With the block emptied, --tw-text-shadow-color lost its only declaration, `initial`, its var() reached the runtime, and text-shadow-md rendered no shadow there (a text-shadow list read through a var() renders nothing on main too). Without it, the Tailwind scan matches main in every utility but the two arbitrary properties this PR scopes, which render the same.
…ntime Removes the inliner's fold of a `syntax: "*"` property's initial value where nothing declares it. Since nativewind#567 the stylesheet carries every registration and the runtime resolves an undeclared property to its initial value, so the fold only moved the same value to compile time; the runtime test for it, `a registered initial value applies where nothing declares the property`, passes without it. The Tailwind scan is unchanged.
…rites it Removes the token path's ratioDescriptor, so a kept `var()` over `16 / 9` reaches the runtime as the text "16 / 9" again, not "16/9". React Native's processAspectRatio splits on "/" and trims each side, so both render the same ratio; the change only made the parity test's kept and written values equal. The parity rows now name the difference as a known one. Tailwind's aspect-* utilities read a :root theme variable, which folds, so the scan is unchanged.
Removes parseBoxShadow's filter and the shared isTransparentColor, so a box-shadow the compiler parses keeps its transparent shadows again, and restores box-shadow.test.tsx to main. A transparent shadow paints nothing, so this changes no rendering; main's own tests already pin the compile path keeping it and the runtime dropping it. The parity rows for the two transparent lists now name that difference. Tailwind's shadow utilities read their shadows through variables and take the runtime path either way, so the scan is unchanged.
danstepanov
approved these changes
Nov 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #413: same commit, rebased onto
main, and four follow-up commits leave out pieces that aren't needed for the fix:@supportsblocks the compiler drops from the inlining census. On its own this breakstext-shadow-*, because Tailwind declares--tw-text-shadow-coloronly in that block. It needs a companion fix that folds a guaranteed-invalid property to its fallback.@supportspatch / previous bullet implemented again?<ratio>read through a variable as16/9instead of16 / 9. React Native reads both.box-shadowon the compile path. It paints nothing either way, and saves little by way of performance. Potentially a follow-up commit if folks don't agree.Summary
A custom property declared once in a stylesheet was folded into every
var()that reads it, whichever rule thevar()was in. So a variable set by one class reached elements that don't have that class:<View className="child" />on its own renderedwidth: 10. It now renders nothing, and inside.parentit still renderswidth: 10.Fix
var()only where every element the rule matches is guaranteed that value: in the rule that declares it, from a*declaration, or from:root/htmlfor an inheriting property. None of these fold from inside@mediaor@container. Everything else resolves at runtime, asinlineVariables: falsealready did.rgb(0 0 255 / var(--a))now gets its alpha.parseUnparsedtreats0andfalseas values instead of absences. Without this, the scoping change breaks kept variables set to0(width: var(--w)over--w: 0). It also fixesring-offset-0andoutline-offset-0, which compiled to nothing, somd:ring-offset-0andmd:outline-offset-0now override.inherits: falsehandling from feat(native): honour inherits: false on registered custom properties #567: a non-inheriting property, and any value that reads one, stays at runtime unless it's only declared on*.Regression status
[--brand:red]on a parent andbg-[--brand]on a child gives the child red, and a barebg-[--brand]gets nothing. This fixes a v4 regression.3.0.0-preview.3), including3.1.0-rc.1.Impact on Tailwind users
md:,dark:,hover:,*:,@md:,ios:and others). The only differences come from the0change above, and nothing throws. Every--tw-*variable that one utility sets and another reads is registeredinherits: false, which already stayed at runtime. The one exception is the mask stops, and mask properties aren't supported.[--brand:red]+bg-(--brand)),@utilityblocks and class themes that set a variable,md:[--x]/dark:[--x], and a:rootvariable declared only for dark mode. Before, they applied everywhere, including at the wrong breakpoint or colour scheme.*:[--x:…](or.list > * { --x: … }) no longer reaches the children. Child combinators aren't matched at runtime (*:bg-red-500doesn't apply either), so this used to work only because the value leaked onto every element. v4 behaves the same.Not changed
A
:rootvariable is still folded, so an ancestor'svars()orVariableContextProvidercan't override it (v4 allowed this). This is unchanged frommainand needs its own issue.Verification
yarn test(2045 tests),yarn typecheck,yarn lintvariable-inlining.test.tsx,inline-variables.test.tsandvariable-fold-parity.test.tsxmain, and nested-tree renders on v4 and on this branchmain: the full suite's 14 scenarios are within −1.3%…+1.2% (the media query scenario +0.1%), which is noise. In a per-element micro-benchmark, everything is within noise except a variable read from another class costs about 2 µs more per element, because it now resolves at runtime where it used to fold a value the element doesn't have.translate-x-4 rotate-45 scale-110is 8% faster, because a kept--tw-translate-y: 0no longer throws away the wholetranslate.