feat: Add compile-time support for Tailwind Variants and Class Variance Authority #4
+1,520
−1
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.
Note this is mostly ai-generated PR and i will be testing over next few days with a couple of real-world react native apps to see how it handles and fix any issues.
feat: Add compile-time support for Tailwind Variants, CVA, and tailwind-merge
Summary
This PR adds compile-time Babel transformation support for the three major Tailwind CSS class utility libraries with all their key exports:
tailwind-variantstvclass-variance-authoritycva,cxtailwind-mergetwMerge,twJoinComponents using these libraries work unchanged on web and are automatically transformed to optimized
StyleSheet.createreferences for React Native.Implementation Highlights
Config-Driven Import Tracking
Adding new libraries or exports is just one line in the config:
Unified Processing
All class joiner utilities (
twMerge,twJoin,cx) share processing logic:How Each Utility is Processed
1. Variant Functions (
tv,cva)These create variant functions called later. We pre-compute all combinations:
2. Class Joiners (
twMerge,twJoin,cx)These are called directly. We resolve at compile time using the actual library:
Features Supported
Tailwind Variants (
tv)true/falsekeys)Class Variance Authority (
cva,cx)cva: Full variant support (base, variants, compounds, defaults)cx: Class concatenation (alias for clsx)tailwind-merge (
twMerge,twJoin)twMerge: Merge with conflict resolution (later classes win)twJoin: Join without conflict resolution (faster)Edge Cases
import { cva, cx })Files Changed
New Files
src/babel/utils/variantProcessing.ts- Core processing utilitiessrc/babel/plugin/visitors/variants.ts- Babel visitorssrc/babel/plugin/visitors/variants.test.ts- 28 comprehensive testsModified Files
src/babel/plugin.ts- Integrated class utility visitorssrc/babel/plugin/state.ts- Added unified tracking types and configsrc/babel/plugin/visitors/program.ts- Added cleanup on program exitDependencies Added
{ "dependencies": { "class-variance-authority": "0.7.1", "tailwind-variants": "3.2.2", "tailwind-merge": "3.4.0" } }Test Results
Example: Using All Libraries
Breaking Changes
None. Purely additive feature.
Future Improvements
tv()cn()utility patternsChecklist
npm test)npm run build)npm run lint)npm run check)