1- import { type CssNode , type Declaration , generate , walk } from 'css-tree' ;
1+ import {
2+ type CssNode ,
3+ type Declaration ,
4+ generate ,
5+ parse ,
6+ type Value ,
7+ walk ,
8+ } from 'css-tree' ;
29import { getReactProperty } from '../compatibility/get-react-property.js' ;
310import type { CustomProperties } from './get-custom-properties.js' ;
11+ import { resolveCalcExpressions } from './resolve-calc-expressions.js' ;
12+ import { sanitizeDeclarations } from './sanitize-declarations.js' ;
413import { stripEmptyTailwindVars } from './strip-empty-tailwind-vars.js' ;
514import { unwrapValue } from './unwrap-value.js' ;
615
16+ function hasVarFunction ( node : CssNode ) {
17+ let found = false ;
18+ walk ( node , {
19+ visit : 'Function' ,
20+ enter ( func ) {
21+ if ( func . name === 'var' ) {
22+ found = true ;
23+ return this . break ;
24+ }
25+ } ,
26+ } ) ;
27+ return found ;
28+ }
29+
730export function makeInlineStylesFor (
831 inlinableRules : CssNode [ ] ,
932 customProperties : CustomProperties ,
@@ -22,49 +45,70 @@ export function makeInlineStylesFor(
2245 } ) ;
2346 }
2447
25- for ( const rule of inlinableRules ) {
26- walk ( rule , {
48+ const resolveValue = (
49+ value : CssNode ,
50+ resolving = new Set < string > ( ) ,
51+ ) : CssNode => {
52+ const resolved = parse ( generate ( value ) , { context : 'value' } ) as Value ;
53+ walk ( resolved , {
2754 visit : 'Function' ,
28- enter ( func , funcParentListItem ) {
29- if ( func . name === 'var' ) {
30- let variableName : string | undefined ;
31- walk ( func , {
32- visit : 'Identifier' ,
33- enter ( identifier ) {
34- variableName = identifier . name ;
35- return this . break ;
36- } ,
37- } ) ;
38- if ( variableName ) {
39- const definition = localVariableDeclarations . get ( variableName ) ;
40- if ( definition ) {
41- funcParentListItem . data = unwrapValue ( definition . value ) ;
42- } else {
43- // For most variables tailwindcss defines, they also define a custom
44- // property for them with an initial value that we can inline here
45- const customProperty = customProperties . get ( variableName ) ;
46- if ( customProperty ?. initialValue ) {
47- funcParentListItem . data = unwrapValue (
48- customProperty . initialValue . value ,
49- ) ;
50- }
51- }
52- }
55+ leave ( func , item ) {
56+ if ( func . name !== 'var' ) return ;
57+ const children = func . children . toArray ( ) ;
58+ const name = children [ 0 ] ? generate ( children [ 0 ] ) . trim ( ) : '' ;
59+ if ( resolving . has ( name ) ) return ;
60+
61+ const definition = localVariableDeclarations . get ( name ) ;
62+ const initialValue = customProperties . get ( name ) ?. initialValue ;
63+ const comma = children . findIndex (
64+ ( child ) => child . type === 'Operator' && child . value === ',' ,
65+ ) ;
66+ const fallback =
67+ comma === - 1
68+ ? undefined
69+ : children
70+ . slice ( comma + 1 )
71+ . map ( ( child ) => generate ( child ) )
72+ . join ( '' ) ;
73+ const replacement = definition ?. value ?? initialValue ?. value ;
74+ if ( replacement ) {
75+ item . data = unwrapValue (
76+ resolveValue ( replacement , new Set ( [ ...resolving , name ] ) ) as Value ,
77+ ) ;
78+ } else if ( fallback ) {
79+ const declaration : Declaration = {
80+ type : 'Declaration' ,
81+ property : 'value' ,
82+ important : false ,
83+ value : resolveValue (
84+ parse ( fallback , { context : 'value' } ) ,
85+ resolving ,
86+ ) as Value ,
87+ } ;
88+ sanitizeDeclarations ( declaration ) ;
89+ item . data = unwrapValue ( declaration . value ) ;
5390 }
5491 } ,
5592 } ) ;
93+ return resolved ;
94+ } ;
5695
96+ for ( const rule of inlinableRules ) {
5797 walk ( rule , {
5898 visit : 'Declaration' ,
5999 enter ( declaration ) {
60100 if ( declaration . property . startsWith ( '--' ) ) {
61101 return ;
62102 }
63- stripEmptyTailwindVars ( declaration . value ) ;
103+ let value : CssNode = declaration . value ;
104+ if ( hasVarFunction ( value ) ) {
105+ value = resolveValue ( value ) ;
106+ resolveCalcExpressions ( value ) ;
107+ stripEmptyTailwindVars ( value ) ;
108+ }
64109
65110 styles [ getReactProperty ( declaration . property ) ] =
66- generate ( declaration . value ) . trim ( ) +
67- ( declaration . important ? '!important' : '' ) ;
111+ generate ( value ) . trim ( ) + ( declaration . important ? '!important' : '' ) ;
68112 } ,
69113 } ) ;
70114 }
0 commit comments