@@ -3,10 +3,8 @@ import selectorParser, { Node, Selector } from 'postcss-selector-parser'
33
44const animationNameRE = / ^ ( - \w + - ) ? a n i m a t i o n - n a m e $ /
55const animationRE = / ^ ( - \w + - ) ? a n i m a t i o n $ /
6- const cssVarRE = / \b v a r \( - - ( g l o b a l : ) ? ( [ ^ ) ] + ) \) / g
76
8- export default postcss . plugin ( 'vue-scoped' , ( options : any ) => ( root : Root ) => {
9- const { id, vars : hasInjectedVars } = options as { id : string ; vars : boolean }
7+ export default postcss . plugin ( 'vue-scoped' , ( id : any ) => ( root : Root ) => {
108 const keyframes = Object . create ( null )
119 const shortId = id . replace ( / ^ d a t a - v - / , '' )
1210
@@ -135,46 +133,37 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
135133 } ) . processSync ( node . selector )
136134 } )
137135
138- const hasKeyframes = Object . keys ( keyframes ) . length
139- if ( hasKeyframes || hasInjectedVars )
136+ if ( Object . keys ( keyframes ) . length ) {
137+ // If keyframes are found in this <style>, find and rewrite animation names
138+ // in declarations.
139+ // Caveat: this only works for keyframes and animation rules in the same
140+ // <style> element.
141+ // individual animation-name declaration
140142 root . walkDecls ( decl => {
141- // If keyframes are found in this <style>, find and rewrite animation names
142- // in declarations.
143- // Caveat: this only works for keyframes and animation rules in the same
144- // <style> element.
145- if ( hasKeyframes ) {
146- // individual animation-name declaration
147- if ( animationNameRE . test ( decl . prop ) ) {
148- decl . value = decl . value
149- . split ( ',' )
150- . map ( v => keyframes [ v . trim ( ) ] || v . trim ( ) )
151- . join ( ',' )
152- }
153- // shorthand
154- if ( animationRE . test ( decl . prop ) ) {
155- decl . value = decl . value
156- . split ( ',' )
157- . map ( v => {
158- const vals = v . trim ( ) . split ( / \s + / )
159- const i = vals . findIndex ( val => keyframes [ val ] )
160- if ( i !== - 1 ) {
161- vals . splice ( i , 1 , keyframes [ vals [ i ] ] )
162- return vals . join ( ' ' )
163- } else {
164- return v
165- }
166- } )
167- . join ( ',' )
168- }
143+ if ( animationNameRE . test ( decl . prop ) ) {
144+ decl . value = decl . value
145+ . split ( ',' )
146+ . map ( v => keyframes [ v . trim ( ) ] || v . trim ( ) )
147+ . join ( ',' )
169148 }
170-
171- // rewrite CSS variables
172- if ( hasInjectedVars && cssVarRE . test ( decl . value ) ) {
173- decl . value = decl . value . replace ( cssVarRE , ( _ , $1 , $2 ) => {
174- return $1 ? `var(--${ $2 } )` : `var(--${ shortId } -${ $2 } )`
175- } )
149+ // shorthand
150+ if ( animationRE . test ( decl . prop ) ) {
151+ decl . value = decl . value
152+ . split ( ',' )
153+ . map ( v => {
154+ const vals = v . trim ( ) . split ( / \s + / )
155+ const i = vals . findIndex ( val => keyframes [ val ] )
156+ if ( i !== - 1 ) {
157+ vals . splice ( i , 1 , keyframes [ vals [ i ] ] )
158+ return vals . join ( ' ' )
159+ } else {
160+ return v
161+ }
162+ } )
163+ . join ( ',' )
176164 }
177165 } )
166+ }
178167} )
179168
180169function isSpaceCombinator ( node : Node ) {
0 commit comments