@@ -153,7 +153,7 @@ function cli(process) {
153
153
154
154
// ... and do the magic!
155
155
if ( commands . args . length > 0 ) {
156
- minify ( commands . args ) ;
156
+ minify ( process , options , debugMode , commands . args ) ;
157
157
} else {
158
158
stdin = process . openStdin ( ) ;
159
159
stdin . setEncoding ( 'utf-8' ) ;
@@ -162,93 +162,93 @@ function cli(process) {
162
162
data += chunk ;
163
163
} ) ;
164
164
stdin . on ( 'end' , function ( ) {
165
- minify ( data ) ;
165
+ minify ( process , options , debugMode , data ) ;
166
166
} ) ;
167
167
}
168
+ }
168
169
169
- function findArgumentTo ( option , rawArgs , args ) {
170
- var value = true ;
171
- var optionAt = rawArgs . indexOf ( option ) ;
172
- var nextOption = rawArgs [ optionAt + 1 ] ;
173
- var looksLikePath ;
174
- var asArgumentAt ;
175
-
176
- if ( ! nextOption ) {
177
- return value ;
178
- }
179
-
180
- looksLikePath = nextOption . indexOf ( '.css' ) > - 1 ||
181
- / \/ / . test ( nextOption ) ||
182
- / \\ [ ^ \- ] / . test ( nextOption ) ||
183
- / ^ h t t p s ? : \/ \/ / . test ( nextOption ) ;
184
- asArgumentAt = args . indexOf ( nextOption ) ;
170
+ function findArgumentTo ( option , rawArgs , args ) {
171
+ var value = true ;
172
+ var optionAt = rawArgs . indexOf ( option ) ;
173
+ var nextOption = rawArgs [ optionAt + 1 ] ;
174
+ var looksLikePath ;
175
+ var asArgumentAt ;
185
176
186
- if ( ! looksLikePath ) {
187
- value = nextOption ;
188
- }
177
+ if ( ! nextOption ) {
178
+ return value ;
179
+ }
189
180
190
- if ( ! looksLikePath && asArgumentAt > - 1 ) {
191
- args . splice ( asArgumentAt , 1 ) ;
192
- }
181
+ looksLikePath = nextOption . indexOf ( '.css' ) > - 1 ||
182
+ / \/ / . test ( nextOption ) ||
183
+ / \\ [ ^ \- ] / . test ( nextOption ) ||
184
+ / ^ h t t p s ? : \/ \/ / . test ( nextOption ) ;
185
+ asArgumentAt = args . indexOf ( nextOption ) ;
193
186
194
- return value ;
187
+ if ( ! looksLikePath ) {
188
+ value = nextOption ;
195
189
}
196
190
197
- function minify ( data ) {
198
- new CleanCSS ( options ) . minify ( data , function ( errors , minified ) {
199
- var mapFilename ;
200
-
201
- if ( debugMode ) {
202
- console . error ( 'Original: %d bytes' , minified . stats . originalSize ) ;
203
- console . error ( 'Minified: %d bytes' , minified . stats . minifiedSize ) ;
204
- console . error ( 'Efficiency: %d%' , ~ ~ ( minified . stats . efficiency * 10000 ) / 100.0 ) ;
205
- console . error ( 'Time spent: %dms' , minified . stats . timeSpent ) ;
206
-
207
- if ( minified . inlinedStylesheets . length > 0 ) {
208
- console . error ( 'Inlined stylesheets:' ) ;
209
- minified . inlinedStylesheets . forEach ( function ( uri ) {
210
- console . error ( '- %s' , uri ) ;
211
- } ) ;
212
- }
213
- }
191
+ if ( ! looksLikePath && asArgumentAt > - 1 ) {
192
+ args . splice ( asArgumentAt , 1 ) ;
193
+ }
214
194
215
- outputFeedback ( minified . errors , true ) ;
216
- outputFeedback ( minified . warnings ) ;
195
+ return value ;
196
+ }
217
197
218
- if ( minified . errors . length > 0 ) {
219
- process . exit ( 1 ) ;
198
+ function minify ( process , options , debugMode , data ) {
199
+ new CleanCSS ( options ) . minify ( data , function ( errors , minified ) {
200
+ var mapFilename ;
201
+
202
+ if ( debugMode ) {
203
+ console . error ( 'Original: %d bytes' , minified . stats . originalSize ) ;
204
+ console . error ( 'Minified: %d bytes' , minified . stats . minifiedSize ) ;
205
+ console . error ( 'Efficiency: %d%' , ~ ~ ( minified . stats . efficiency * 10000 ) / 100.0 ) ;
206
+ console . error ( 'Time spent: %dms' , minified . stats . timeSpent ) ;
207
+
208
+ if ( minified . inlinedStylesheets . length > 0 ) {
209
+ console . error ( 'Inlined stylesheets:' ) ;
210
+ minified . inlinedStylesheets . forEach ( function ( uri ) {
211
+ console . error ( '- %s' , uri ) ;
212
+ } ) ;
220
213
}
214
+ }
221
215
222
- if ( minified . sourceMap ) {
223
- mapFilename = path . basename ( options . output ) + '.map' ;
224
- output ( minified . styles + '/*# sourceMappingURL=' + mapFilename + ' */' ) ;
225
- outputMap ( minified . sourceMap , mapFilename ) ;
226
- } else {
227
- output ( minified . styles ) ;
228
- }
229
- } ) ;
230
- }
216
+ outputFeedback ( minified . errors , true ) ;
217
+ outputFeedback ( minified . warnings ) ;
218
+
219
+ if ( minified . errors . length > 0 ) {
220
+ process . exit ( 1 ) ;
221
+ }
231
222
232
- function output ( minified ) {
233
- if ( options . output ) {
234
- fs . writeFileSync ( options . output , minified , 'utf8' ) ;
223
+ if ( minified . sourceMap ) {
224
+ mapFilename = path . basename ( options . output ) + '.map' ;
225
+ output ( process , options , minified . styles + '/*# sourceMappingURL=' + mapFilename + ' */' ) ;
226
+ outputMap ( options , minified . sourceMap , mapFilename ) ;
235
227
} else {
236
- process . stdout . write ( minified ) ;
228
+ output ( process , options , minified . styles ) ;
237
229
}
238
- }
230
+ } ) ;
231
+ }
239
232
240
- function outputMap ( sourceMap , mapFilename ) {
241
- var mapPath = path . join ( path . dirname ( options . output ) , mapFilename ) ;
242
- fs . writeFileSync ( mapPath , sourceMap . toString ( ) , 'utf-8' ) ;
243
- }
233
+ function outputFeedback ( messages , isError ) {
234
+ var prefix = isError ? '\x1B[31mERROR\x1B[39m:' : 'WARNING:' ;
244
235
245
- function outputFeedback ( messages , isError ) {
246
- var prefix = isError ? '\x1B[31mERROR\x1B[39m:' : 'WARNING:' ;
236
+ messages . forEach ( function ( message ) {
237
+ console . error ( '%s %s' , prefix , message ) ;
238
+ } ) ;
239
+ }
247
240
248
- messages . forEach ( function ( message ) {
249
- console . error ( '%s %s' , prefix , message ) ;
250
- } ) ;
241
+ function output ( process , options , minified ) {
242
+ if ( options . output ) {
243
+ fs . writeFileSync ( options . output , minified , 'utf8' ) ;
244
+ } else {
245
+ process . stdout . write ( minified ) ;
251
246
}
252
247
}
253
248
249
+ function outputMap ( options , sourceMap , mapFilename ) {
250
+ var mapPath = path . join ( path . dirname ( options . output ) , mapFilename ) ;
251
+ fs . writeFileSync ( mapPath , sourceMap . toString ( ) , 'utf-8' ) ;
252
+ }
253
+
254
254
module . exports = cli ;
0 commit comments