@@ -3,11 +3,18 @@ var selectorParser = require('postcss-selector-parser')
33
44module . exports = postcss . plugin ( 'add-id' , function ( opts ) {
55 return function ( root ) {
6+ var keyframes = Object . create ( null )
7+
68 root . each ( function rewriteSelector ( node ) {
79 if ( ! node . selector ) {
810 // handle media queries
9- if ( node . type === 'atrule' && node . name === 'media' ) {
10- node . each ( rewriteSelector )
11+ if ( node . type === 'atrule' ) {
12+ if ( node . name === 'media' ) {
13+ node . each ( rewriteSelector )
14+ } else if ( node . name === 'keyframes' ) {
15+ // register keyframes
16+ keyframes [ node . params ] = node . params = node . params + '-' + opts . id
17+ }
1118 }
1219 return
1320 }
@@ -23,5 +30,36 @@ module.exports = postcss.plugin('add-id', function (opts) {
2330 } )
2431 } ) . process ( node . selector ) . result
2532 } )
33+
34+ // If keyframes are found in this <style>, find and rewrite animation names
35+ // in declarations
36+ //
37+ // TODO: the keyframes tracking needs to be across multiple <style> tags
38+ // that belongs to the same component...
39+ if ( Object . keys ( keyframes ) . length ) {
40+ root . walkDecls ( decl => {
41+ // individual animation-name declaration
42+ if ( / - ? a n i m a t i o n - n a m e $ / . test ( decl . prop ) ) {
43+ decl . value = decl . value . split ( ',' )
44+ . map ( v => keyframes [ v . trim ( ) ] )
45+ . filter ( v => v )
46+ . join ( ',' )
47+ }
48+ // shorthand
49+ if ( / - ? a n i m a t i o n $ / . test ( decl . prop ) ) {
50+ decl . value = decl . value . split ( ',' )
51+ . map ( v => {
52+ var vals = v . split ( / \s + / )
53+ var name = vals [ 0 ]
54+ if ( keyframes [ name ] ) {
55+ return [ keyframes [ name ] ] . concat ( vals . slice ( 1 ) ) . join ( ' ' )
56+ } else {
57+ return v
58+ }
59+ } )
60+ . join ( ',' )
61+ }
62+ } )
63+ }
2664 }
2765} )
0 commit comments