@@ -6,7 +6,7 @@ const animationRE = /^(-\w+-)?animation$/
6
6
const cssVarRE = / \b v a r \( - - ( g l o b a l : ) ? ( [ ^ ) ] + ) \) / g
7
7
8
8
export default postcss . plugin ( 'vue-scoped' , ( options : any ) => ( root : Root ) => {
9
- const id : string = options
9
+ const { id , vars : hasInjectedVars } = options as { id : string ; vars : boolean }
10
10
const keyframes = Object . create ( null )
11
11
12
12
root . each ( function rewriteSelectors ( node ) {
@@ -135,44 +135,45 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
135
135
} )
136
136
137
137
const hasKeyframes = Object . keys ( keyframes ) . length
138
- root . walkDecls ( decl => {
139
- // If keyframes are found in this <style>, find and rewrite animation names
140
- // in declarations.
141
- // Caveat: this only works for keyframes and animation rules in the same
142
- // <style> element.
143
- if ( hasKeyframes ) {
144
- // individual animation-name declaration
145
- if ( animationNameRE . test ( decl . prop ) ) {
146
- decl . value = decl . value
147
- . split ( ',' )
148
- . map ( v => keyframes [ v . trim ( ) ] || v . trim ( ) )
149
- . join ( ',' )
150
- }
151
- // shorthand
152
- if ( animationRE . test ( decl . prop ) ) {
153
- decl . value = decl . value
154
- . split ( ',' )
155
- . map ( v => {
156
- const vals = v . trim ( ) . split ( / \s + / )
157
- const i = vals . findIndex ( val => keyframes [ val ] )
158
- if ( i !== - 1 ) {
159
- vals . splice ( i , 1 , keyframes [ vals [ i ] ] )
160
- return vals . join ( ' ' )
161
- } else {
162
- return v
163
- }
164
- } )
165
- . join ( ',' )
138
+ if ( hasKeyframes || hasInjectedVars )
139
+ root . walkDecls ( decl => {
140
+ // If keyframes are found in this <style>, find and rewrite animation names
141
+ // in declarations.
142
+ // Caveat: this only works for keyframes and animation rules in the same
143
+ // <style> element.
144
+ if ( hasKeyframes ) {
145
+ // individual animation-name declaration
146
+ if ( animationNameRE . test ( decl . prop ) ) {
147
+ decl . value = decl . value
148
+ . split ( ',' )
149
+ . map ( v => keyframes [ v . trim ( ) ] || v . trim ( ) )
150
+ . join ( ',' )
151
+ }
152
+ // shorthand
153
+ if ( animationRE . test ( decl . prop ) ) {
154
+ decl . value = decl . value
155
+ . split ( ',' )
156
+ . map ( v => {
157
+ const vals = v . trim ( ) . split ( / \s + / )
158
+ const i = vals . findIndex ( val => keyframes [ val ] )
159
+ if ( i !== - 1 ) {
160
+ vals . splice ( i , 1 , keyframes [ vals [ i ] ] )
161
+ return vals . join ( ' ' )
162
+ } else {
163
+ return v
164
+ }
165
+ } )
166
+ . join ( ',' )
167
+ }
166
168
}
167
- }
168
169
169
- // rewrite CSS variables
170
- if ( cssVarRE . test ( decl . value ) ) {
171
- decl . value = decl . value . replace ( cssVarRE , ( _ , $1 , $2 ) => {
172
- return $1 ? `var(--${ $2 } )` : `var(--${ id } -${ $2 } )`
173
- } )
174
- }
175
- } )
170
+ // rewrite CSS variables
171
+ if ( hasInjectedVars && cssVarRE . test ( decl . value ) ) {
172
+ decl . value = decl . value . replace ( cssVarRE , ( _ , $1 , $2 ) => {
173
+ return $1 ? `var(--${ $2 } )` : `var(--${ id } -${ $2 } )`
174
+ } )
175
+ }
176
+ } )
176
177
} )
177
178
178
179
function isSpaceCombinator ( node : Node ) {
0 commit comments