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>
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