11
11
module . exports = function ( node , options ) {
12
12
options = options || { } ;
13
13
return options . compress
14
- ? node . stylesheet . rules . map ( rule ( options ) ) . join ( '' )
15
- : node . stylesheet . rules . map ( rule ( options ) ) . join ( '\n\n' ) ;
14
+ ? node . stylesheet . rules . map ( visit ( options ) ) . join ( '' )
15
+ : node . stylesheet . rules . map ( visit ( options ) ) . join ( '\n\n' ) ;
16
16
} ;
17
17
18
+ /**
19
+ * Visit rule nodes.
20
+ */
21
+
22
+ function visit ( options ) {
23
+ var _rule = rule ( options ) ;
24
+ var _keyframes = keyframes ( options ) ;
25
+ return function ( node ) {
26
+ if ( node . keyframes ) return _keyframes ( node ) ;
27
+ if ( node . import ) return atimport ( node ) ;
28
+ return _rule ( options ) ;
29
+ }
30
+ }
31
+
18
32
/**
19
33
* Compile import.
20
34
*/
@@ -23,25 +37,72 @@ function atimport(rule) {
23
37
return '@import ' + rule . import + ';' ;
24
38
}
25
39
40
+ /**
41
+ * Compile keyframes.
42
+ */
43
+
44
+ function keyframes ( options ) {
45
+ if ( options . compress ) {
46
+ return function ( keyframes ) {
47
+ return '@'
48
+ + ( keyframes . vendor || '' )
49
+ + 'keyframes '
50
+ + keyframes . name
51
+ + '{'
52
+ + keyframes . keyframes . map ( keyframe ( options ) ) . join ( '' )
53
+ + '}' ;
54
+ }
55
+ }
56
+
57
+ return function ( keyframes ) {
58
+ return '@'
59
+ + ( keyframes . vendor || '' )
60
+ + 'keyframes '
61
+ + keyframes . name
62
+ + ' {\n'
63
+ + keyframes . keyframes . map ( keyframe ( options ) ) . join ( '\n' )
64
+ + '}' ;
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Compile keyframe.
70
+ */
71
+
72
+ function keyframe ( options ) {
73
+ if ( options . compress ) {
74
+ return function ( keyframe ) {
75
+ return keyframe . values . join ( ',' )
76
+ + '{'
77
+ + keyframe . declarations . map ( declaration ( options ) ) . join ( ';' )
78
+ + '}'
79
+ }
80
+ }
81
+
82
+ return function ( keyframe ) {
83
+ return ' '
84
+ + keyframe . values . join ( ', ' )
85
+ + ' {\n'
86
+ + keyframe . declarations . map ( indent ( declaration ( options ) ) ) . join ( ';' )
87
+ + '\n }\n'
88
+ }
89
+ }
90
+
26
91
/**
27
92
* Compile rule.
28
93
*/
29
94
30
95
function rule ( options ) {
31
96
if ( options . compress ) {
32
- return function ( rule ) {
33
- if ( rule . import ) return atimport ( rule ) ;
34
-
97
+ return function ( rule ) {
35
98
return rule . selector
36
99
+ '{'
37
100
+ rule . declarations . map ( declaration ( options ) ) . join ( ';' )
38
101
+ '}' ;
39
102
}
40
103
}
41
104
42
- return function ( rule ) {
43
- if ( rule . import ) return atimport ( rule ) ;
44
-
105
+ return function ( rule ) {
45
106
return rule . selector
46
107
+ ' {\n'
47
108
+ rule . declarations . map ( declaration ( options ) ) . join ( '\n' )
@@ -63,4 +124,14 @@ function declaration(options) {
63
124
return function ( decl ) {
64
125
return ' ' + decl . property + ': ' + decl . value + ';' ;
65
126
}
127
+ }
128
+
129
+ /**
130
+ * Indent.
131
+ */
132
+
133
+ function indent ( fn ) {
134
+ return function ( val ) {
135
+ return ' ' + fn ( val ) ;
136
+ }
66
137
}
0 commit comments