@@ -34,7 +34,8 @@ function cli(process, beforeMinifyCallback) {
34
34
. option ( '--remove-inlined-files' , 'Remove files inlined in <source-file ...> or via `@import` statements' )
35
35
. option ( '--skip-rebase' , 'Disable URLs rebasing' )
36
36
. option ( '--source-map' , 'Enables building input\'s source map' )
37
- . option ( '--source-map-inline-sources' , 'Enables inlining sources inside source maps' ) ;
37
+ . option ( '--source-map-inline-sources' , 'Enables inlining sources inside source maps' )
38
+ . option ( '--input-source-map [file]' , 'Specifies the path of the input source map file' ) ;
38
39
39
40
commands . on ( '--help' , function ( ) {
40
41
console . log ( ' Examples:\n' ) ;
@@ -156,14 +157,25 @@ function cli(process, beforeMinifyCallback) {
156
157
sourceMapInlineSources : commands . sourceMapInlineSources
157
158
} ;
158
159
160
+ if ( commands . inputSourceMap && ! options . sourceMap ) {
161
+ options . sourceMap = true ;
162
+ }
163
+
159
164
if ( options . sourceMap && ! options . output ) {
160
165
outputFeedback ( [ 'Source maps will not be built because you have not specified an output file.' ] , true ) ;
161
166
options . sourceMap = false ;
162
167
}
163
168
169
+ var configurations = {
170
+ beforeMinifyCallback : beforeMinifyCallback ,
171
+ debugMode : debugMode ,
172
+ removeInlinedFiles : removeInlinedFiles ,
173
+ inputSourceMap : commands . inputSourceMap
174
+ } ;
175
+
164
176
// ... and do the magic!
165
177
if ( commands . args . length > 0 ) {
166
- minify ( process , beforeMinifyCallback , options , debugMode , removeInlinedFiles , expandGlobs ( commands . args ) ) ;
178
+ minify ( process , options , configurations , expandGlobs ( commands . args ) ) ;
167
179
} else {
168
180
stdin = process . openStdin ( ) ;
169
181
stdin . setEncoding ( 'utf-8' ) ;
@@ -172,7 +184,7 @@ function cli(process, beforeMinifyCallback) {
172
184
data += chunk ;
173
185
} ) ;
174
186
stdin . on ( 'end' , function ( ) {
175
- minify ( process , beforeMinifyCallback , options , debugMode , removeInlinedFiles , data ) ;
187
+ minify ( process , options , configurations , data ) ;
176
188
} ) ;
177
189
}
178
190
}
@@ -211,15 +223,15 @@ function expandGlobs(paths) {
211
223
} , [ ] ) ;
212
224
}
213
225
214
- function minify ( process , beforeMinifyCallback , options , debugMode , removeInlinedFiles , data ) {
226
+ function minify ( process , options , configurations , data ) {
215
227
var cleanCss = new CleanCSS ( options ) ;
216
228
217
229
applyNonBooleanCompatibilityFlags ( cleanCss , options . compatibility ) ;
218
- beforeMinifyCallback ( cleanCss ) ;
219
- cleanCss . minify ( data , function ( errors , minified ) {
230
+ configurations . beforeMinifyCallback ( cleanCss ) ;
231
+ cleanCss . minify ( data , getSourceMapContent ( configurations . inputSourceMap ) , function ( errors , minified ) {
220
232
var mapFilename ;
221
233
222
- if ( debugMode ) {
234
+ if ( configurations . debugMode ) {
223
235
console . error ( 'Original: %d bytes' , minified . stats . originalSize ) ;
224
236
console . error ( 'Minified: %d bytes' , minified . stats . minifiedSize ) ;
225
237
console . error ( 'Efficiency: %d%' , ~ ~ ( minified . stats . efficiency * 10000 ) / 100.0 ) ;
@@ -240,7 +252,7 @@ function minify(process, beforeMinifyCallback, options, debugMode, removeInlined
240
252
process . exit ( 1 ) ;
241
253
}
242
254
243
- if ( removeInlinedFiles ) {
255
+ if ( configurations . removeInlinedFiles ) {
244
256
minified . inlinedStylesheets . forEach ( fs . unlinkSync ) ;
245
257
}
246
258
@@ -289,6 +301,21 @@ function outputFeedback(messages, isError) {
289
301
} ) ;
290
302
}
291
303
304
+ function getSourceMapContent ( sourceMapPath ) {
305
+ if ( ! sourceMapPath || ! fs . existsSync ( sourceMapPath ) ) {
306
+ return null ;
307
+ }
308
+ var content = null ;
309
+
310
+ try {
311
+ content = fs . readFileSync ( sourceMapPath ) . toString ( ) ;
312
+ } catch ( e ) {
313
+ console . error ( 'Failed to read the input source map file.' ) ;
314
+ }
315
+
316
+ return content ;
317
+ }
318
+
292
319
function output ( process , options , minified ) {
293
320
if ( options . output ) {
294
321
fs . writeFileSync ( options . output , minified , 'utf8' ) ;
0 commit comments