@@ -72,17 +72,19 @@ export function walk(
72
72
}
73
73
74
74
export function toCss ( ast : AstNode [ ] ) {
75
- let atRoots : string [ ] = [ ]
75
+ let atRoots : string = ''
76
+ let seenAtProperties = new Set < string > ( )
76
77
77
78
function stringify ( node : AstNode , depth = 0 ) : string {
78
79
let css = ''
80
+ let indent = ' ' . repeat ( depth )
79
81
80
82
// Rule
81
83
if ( node . kind === 'rule' ) {
82
84
// Pull out `@at-root` rules to append later
83
85
if ( node . selector === '@at-root' ) {
84
86
for ( let child of node . nodes ) {
85
- atRoots . push ( stringify ( child , 0 ) )
87
+ atRoots += stringify ( child , 0 )
86
88
}
87
89
return css
88
90
}
@@ -95,31 +97,45 @@ export function toCss(ast: AstNode[]) {
95
97
// @layer base, components, utilities;
96
98
// ```
97
99
if ( node . selector [ 0 ] === '@' && node . nodes . length === 0 ) {
98
- return `${ ' ' . repeat ( depth ) } ${ node . selector } ;\n`
100
+ return `${ indent } ${ node . selector } ;\n`
99
101
}
100
102
101
- css += `${ ' ' . repeat ( depth ) } ${ node . selector } {\n`
103
+ if ( node . selector [ 0 ] === '@' && node . selector . startsWith ( '@property ' ) && depth === 0 ) {
104
+ // Don't output duplicate `@property` rules
105
+ if ( seenAtProperties . has ( node . selector ) ) {
106
+ return ''
107
+ }
108
+
109
+ seenAtProperties . add ( node . selector )
110
+ }
111
+
112
+ css += `${ indent } ${ node . selector } {\n`
102
113
for ( let child of node . nodes ) {
103
114
css += stringify ( child , depth + 1 )
104
115
}
105
- css += `${ ' ' . repeat ( depth ) } }\n`
116
+ css += `${ indent } }\n`
106
117
}
107
118
108
119
// Comment
109
120
else if ( node . kind === 'comment' ) {
110
- css += `${ ' ' . repeat ( depth ) } /*${ node . value } */\n`
121
+ css += `${ indent } /*${ node . value } */\n`
111
122
}
112
123
113
124
// Declaration
114
125
else if ( node . property !== '--tw-sort' && node . value !== undefined && node . value !== null ) {
115
- css += `${ ' ' . repeat ( depth ) } ${ node . property } : ${ node . value } ${ node . important ? '!important' : '' } ;\n`
126
+ css += `${ indent } ${ node . property } : ${ node . value } ${ node . important ? '!important' : '' } ;\n`
116
127
}
117
128
118
129
return css
119
130
}
120
131
121
- return ast
122
- . map ( ( node ) => stringify ( node ) )
123
- . concat ( atRoots )
124
- . join ( '\n' )
132
+ let css = ''
133
+ for ( let node of ast ) {
134
+ let result = stringify ( node )
135
+ if ( result !== '' ) {
136
+ css += result
137
+ }
138
+ }
139
+
140
+ return `${ css } ${ atRoots } `
125
141
}
0 commit comments