Skip to content

fix(compiler): support lightningcss 1.30.2+ visitor deserialization - #605

Merged
danstepanov merged 2 commits into
nativewind:mainfrom
sammoore:fix/lightningcss-1.30.2-visitors
Dec 4, 2026
Merged

danstepanov merged 2 commits into
nativewind:mainfrom
sammoore:fix/lightningcss-1.30.2-visitors

Conversation

@sammoore

@sammoore sammoore commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #470.

The bug

Starting in lightningcss@1.30.2 (upstream commit 9879b91, switching visitor buffering to serde-content), returning a node from a JS visitor triggers two deserialization regressions that remain unfixed in published releases through 1.33.0:

  1. Explicit null in Option<T> fields (parcel-bundler/lightningcss#1065, #1081): any unresolved var() arrives in JS with from: null and fallback: null, so handing it back to lightningcss throws failed to deserialize; expected an object-like struct named Specifier, found (). Anonymous @layer and unnamed @container rules fail the same way.
  2. Non-finite floats and out-of-range integers in f32 fields (parcel-bundler/lightningcss#1188): returning a parsed LengthValue / Angle / Time / Resolution whose value is Infinity (Tailwind v4's .rounded-full { border-radius: calc(infinity * 1px); }) or an integer outside [-32768, 65535] (e.g. 99999px) throws expected a 32-bit floating point, found inff64 / 99999i64.

In src/compiler/compiler.ts, the first pass's StyleSheetExit returned the entire StyleSheet AST (even when inlineVariables: false), so every stylesheet with an unresolved var() (such as Tailwind 4's .border { border-style: var(--tw-border-style); }) or .rounded-full crashed on lightningcss >= 1.30.2. The loader in lightningcss-loader.ts only blocked 1.30.2 specifically, leaving 1.31.x–1.33.0 to crash at compile time and blocking Tailwind bumps past 4.1.12.

The fix

  • Stop returning StyleSheet from the first pass (src/compiler/compiler.ts, src/compiler/inline-variables.ts):
    • Replace StyleSheetExit returning sheet with a read-only StyleSheet enter visitor that registers @property rules and builds a VariableInliningPlan, returning undefined.
    • Apply inlining per-declaration in Declaration: return undefined for untouched declarations (so rules like .border and .rounded-full never cross back into Rust's deserializer), return [] to remove unread single-definition custom properties, and return only declarations where a var() actually folded.
    • Track same-block local variable scope in Rule (style, nested-declarations, keyframes) to preserve exact block-scoped inlining semantics.
  • Sanitize tokens on returned declarations (src/compiler/inline-variables.ts, src/compiler/individual-transforms.ts):
    • Strip null Option fields (from / fallback on var() and env()) for #1065/#1081.
    • Rewrite out-of-range integer or non-finite length / angle / time / resolution tokens as raw token.dimension tokens for #1188 (both in folded declarations and in unparseIndividualTransform).

Verification

  • Unit & integration tests:
    • yarn test src/ on lightningcss@1.30.1: 114/114 suites, 2,362/2,362 tests passed.
    • yarn test src/ on lightningcss@1.33.0: 114/114 suites, 2,362/2,362 tests passed (including calc(infinity * 1px) in calc.test.tsx and absolute-units.test.tsx).
    • Compiler suite verified on lightningcss@1.30.2: 14/14 suites, 228/228 tests passed.
    • node --test node/__tests__/*.mjs: 42/42 passed.
    • Added regression tests in src/__tests__/compiler/visitor-returns.test.ts asserting StyleSheet/StyleSheetExit never return a sheet, untouched declarations return nothing, returned declarations contain no null fields or unsafe f32 integer nodes, and same-block @keyframes variable scoping holds.
  • Output parity: 100% byte-identical compiled stylesheet JSON vs main across medium (17.4 KB) and large (895.8 KB) Tailwind v4 sheets (default, inlineVariables: false, inlineRem: false).
  • Performance:
    • Runtime (bench): src/native/ is untouched and bench/hermes/build.mjs emits bit-for-bit identical JS bundles for both plain and tailwind stylesheets (0% runtime change on Hermes and V8).
    • Compile time (Node 24, lightningcss@1.30.1): 1.09×–1.16× faster with default inlining (895.8 KB Tailwind: 1617 ms → 1484 ms) and 1.24×–1.33× faster with inlineVariables: false (1597 ms → 1290 ms), as a byproduct of skipping full-AST deserialization.

Follow-ups (not in this PR)

  • Separate lightningcss AST changes in 1.31+ (nullable ContainerRule.condition for name-only @container) and 1.32+ (rotate: none serialized as "none", affecting Tailwind's rotate-none) are tracked on a separate branch (fix/lightningcss-1.31-1.32-ast).

@sammoore
sammoore force-pushed the fix/lightningcss-1.30.2-visitors branch from 63d856a to 5cfc519 Compare December 3, 2026 23:44
@sammoore
sammoore marked this pull request as ready for review December 3, 2026 23:44
Avoids returning StyleSheet from the first pass (returning only modified/removed declarations instead), strips null Option fields (parcel-bundler/lightningcss#1065, #1081), and rewrites out-of-range integer dimensions as raw dimension tokens (#1188). The loader's 1.30.2 check stays.

Fixes nativewind#470.
…on tokens

The second pass already converts a rem dimension token with inlineRem, so
converting it to px before handing it back to lightningcss changes nothing.
Removes the remMultiplier parameter threaded through foldDeclaration,
sanitizeTokenList and unparseIndividualTransform.

Output is identical on lightningcss 1.30.1 and 1.33.0 for 70000rem through
var() and translate, with and without inlineRem.
@danstepanov
danstepanov force-pushed the fix/lightningcss-1.30.2-visitors branch from 5cfc519 to ecac3a4 Compare December 4, 2026 03:32
@danstepanov
danstepanov merged commit 42a374b into nativewind:main Dec 4, 2026
6 checks passed
sammoore added a commit to sammoore/react-native-css that referenced this pull request Dec 4, 2026
Since nativewind#605 returns nothing from the visitor for an untouched rule,
lightningcss no longer round-trips it through its serializer, which
wrote -0 as 0. The operand now keeps its sign. It compares as 0, and
JSON writes it as 0 for a device, so nothing renders differently.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sammoore added a commit to sammoore/react-native-css that referenced this pull request Dec 4, 2026
Since nativewind#605 returns nothing from the visitor for an untouched rule,
lightningcss no longer round-trips it through its serializer, which
wrote -0 as 0. The operand now keeps its sign. It compares as 0, and
JSON writes it as 0 for a device, so nothing renders differently.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sammoore added a commit to sammoore/react-native-css that referenced this pull request Dec 4, 2026
Since nativewind#605 returns nothing from the visitor for an untouched rule,
lightningcss no longer round-trips it through its serializer, which
wrote -0 as 0. The operand now keeps its sign. It compares as 0, and
JSON writes it as 0 for a device, so nothing renders differently.
danstepanov pushed a commit that referenced this pull request Dec 4, 2026
…decide with unknown (#608)

* fix: evaluate range pairs, and answer a condition the runtime cannot decide with unknown

Media and container queries now evaluate through one walk and one comparison in
three-valued logic (MQ5 §3.1): a term the runtime cannot decide is unknown, its negation
too, and a rule applies only when its condition is true, as in Chromium, Firefox and WebKit.

- A range pair is its two comparisons, where both runtimes answered it false.
- A term is unknown when its feature is unmeasured, its value is outside the feature's
  syntax (a keyword or a type Media Queries 5 does not give it, env(), a negative
  resolution or ratio component) or its operand is unresolved, which compiles to null so
  JSON carries it. A condition the compiler's unknown terms leave no way to be true
  compiles to no rule, as does a container condition CSS Conditional 5 §5.4 discards.
- A number past the largest float compiles to the largest safe integer and a length to the
  same bound rounded, since JSON writes Infinity as null.
- A feature in a boolean context is false only for zero, none and no-preference.
- A viewport length in a condition resolves against the window.
- hover, any-hover, pointer and any-pointer answer for the primary input, as Safari and
  Chrome do on a phone; color, color-index, monochrome, grid, scripting, update,
  overflow-block, overflow-inline, scan, color-gamut, dynamic-range and their video- forms
  answer as every React Native screen does, forced-colors as none off Windows, and
  -webkit-device-pixel-ratio as resolution.
- A keyword and a media type match in any case.
- A compiled condition is readonly at every depth, and the condition compiler reads
  lightningcss's AST through DeepReadonly.

* fix(native): keep main's answer to every hover comparison

The PR answers hover for the primary input, none on a touchscreen, which
drops Tailwind's hover:, group-hover: and peer-hover: wherever a hover
does arrive on iOS or Android (an iPad trackpad, a component that calls
onHoverIn), unlike Nativewind v4 and every v5 release. Leaving hover
unanswered would be worse: under three-valued logic (hover: hover) would
be unknown, and hover: would apply nowhere.

What hover should answer is left for later, so this keeps main's
placeholder unchanged: every (hover: ...) comparison holds. any-hover and
hover's boolean context go unanswered, as no Tailwind variant writes them.
pointer and any-pointer keep answering for the primary input.

* refactor(native): drop media features no Tailwind variant reaches

color, color-index, monochrome, grid, update, overflow-block,
overflow-inline, scan, color-gamut, dynamic-range, their video- forms and
-webkit-device-pixel-ratio go back to being unmeasured, and an
<mq-boolean> compiles as before. No Tailwind variant queries any of them, so
they are a feature of their own rather than part of evaluating a condition
in three-valued logic. scripting and forced-colors stay: Tailwind's
noscript: and forced-colors: reach them, and without an answer their not-
forms would become unknown.

* refactor(compiler): keep main's handling of non-finite numbers

Drops safeNumber: a media number, resolution or calc() operand compiles
as written again, and a length in a declaration keeps main's clamping.
safeNumber changed stock Tailwind output (rounded-full's
calc(infinity * 1px) went from 9007199254740991 to 9007199254740990) and
covers only operands past the largest float, which no Tailwind variant
writes. A viewport length lightningcss prints as an overflowing number,
such as calc(NaN * 1vw), reaches a device as null and stays unknown, so
its over-the-wire test row goes too.

* refactor: drop the readonly condition types

Removes DeepReadonly, ReadonlyStyleDescriptor and the readonly tuples in
MediaCondition, RenderGuard and the condition walkers, with
condition-types.test.ts, which only checked them. They change no emitted
code; MediaFeatureOperand keeps the shape a condition operand has. The
container guard keeps the PR's honest type for a missing container, which
the lint the PR enables on this file requires.

* refactor(native): represent an unknown condition as undefined

A condition still takes three values, since not unknown must stay
unknown rather than become true, but undefined carries the third as
well as a string did, so the Truth type and the UNKNOWN constant go.

* test(compiler): expect -0 from a -0dppx resolution

Since #605 returns nothing from the visitor for an untouched rule,
lightningcss no longer round-trips it through its serializer, which
wrote -0 as 0. The operand now keeps its sign. It compares as 0, and
JSON writes it as 0 for a device, so nothing renders differently.

* test(tailwind): drop the hover platform setup main's answer makes moot

With main's answer to every hover comparison kept, hover: and
group-hover: apply whatever the platform, so the Mac Catalyst mock
around those tests changed nothing, and the touchscreen tests repeated
them on the iOS preset. The tests go back to main's layout, keeping the
author's assertions that the colour is absent. The setup moves to the
hover patch, where the platform decides the answer.

---------

Co-authored-by: Yevhenii <evgeny.kotyrlo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants