From: Jakub Pawlowicz Date: Sat, 13 Dec 2014 20:59:44 +0000 (+0000) Subject: Simplifies arguments handling in binary. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=95b1f34744f92c651bdffe8ff547275a95d97578;p=clean-css.git Simplifies arguments handling in binary. --- diff --git a/README.md b/README.md index 04bbf07f..6fc3b4dc 100644 --- a/README.md +++ b/README.md @@ -242,7 +242,7 @@ To generate a source map, use `sourceMap: true` option, e.g.: new CleanCSS({ sourceMap: true, target: pathToOutputDirectory }).minify(source, function (minified) { // access minified.sourceMap for SourceMapGenerator object // see https://github.com/mozilla/source-map/#sourcemapgenerator for more details - // see https://github.com/jakubpawlowicz/clean-css/blob/master/bin/cleancss#L151 on how it's used in clean-css' CLI + // see https://github.com/jakubpawlowicz/clean-css/blob/master/bin/cleancss#L132 on how it's used in clean-css' CLI }); ``` @@ -252,7 +252,7 @@ Using API you can also pass an input source map directly: new CleanCSS({ sourceMap: inputSourceMapAsString, target: pathToOutputDirectory }).minify(source, function (minified) { // access minified.sourceMap to access SourceMapGenerator object // see https://github.com/mozilla/source-map/#sourcemapgenerator for more details - // see https://github.com/jakubpawlowicz/clean-css/blob/master/bin/cleancss#L151 on how it's used in clean-css' CLI + // see https://github.com/jakubpawlowicz/clean-css/blob/master/bin/cleancss#L132 on how it's used in clean-css' CLI }); ``` diff --git a/bin/cleancss b/bin/cleancss index 40ef2d84..d726c539 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -48,69 +48,50 @@ commands.on('--help', function() { commands.parse(process.argv); -var options = { - sources: null, - target: null -}; -var cleanOptions = {}; -var fromStdin = !process.env.__DIRECT__ && !process.stdin.isTTY; - // If no sensible data passed in just print help and exit +var fromStdin = !process.env.__DIRECT__ && !process.stdin.isTTY; if (!fromStdin && commands.args.length === 0) { commands.outputHelp(); return 0; } // Now coerce commands into CleanCSS configuration... -if (commands.output) - cleanOptions.target = options.target = commands.output; -if (commands.keepLineBreaks) - cleanOptions.keepBreaks = true; -if (commands.s1) - cleanOptions.keepSpecialComments = 1; -if (commands.s0) - cleanOptions.keepSpecialComments = 0; -if (commands.root) - cleanOptions.root = commands.root; -if (commands.skipImport) - cleanOptions.processImport = false; -if (commands.skipRebase) - cleanOptions.rebase = false; -if (commands.skipAdvanced) - cleanOptions.advanced = false; -if (commands.skipAggressiveMerging) - cleanOptions.aggressiveMerging = false; -if (commands.skipShorthandCompacting) - cleanOptions.shorthandCompacting = false; -if (commands.compatibility) - cleanOptions.compatibility = commands.compatibility; -if (commands.sourceMap) - cleanOptions.sourceMap = true; -if (commands.roundingPrecision !== undefined) - cleanOptions.roundingPrecision = commands.roundingPrecision; -if (commands.debug) - cleanOptions.debug = true; -if (commands.timeout) - cleanOptions.inliner = { timeout: parseFloat(commands.timeout) * 1000 }; +var options = { + advanced: commands.skipAdvanced ? false : true, + aggressiveMerging: commands.skipAggressiveMerging ? false : true, + compatibility: commands.compatibility, + debug: commands.debug, + inliner: commands.timeout ? { timeout: parseFloat(commands.timeout) * 1000 } : undefined, + keepBreaks: !!commands.keepLineBreaks, + keepSpecialComments: commands.s0 ? 0 : (commands.s1 ? 1 : '*'), + processImport: commands.skipImport ? false : true, + rebase: commands.skipRebase ? false : true, + root: commands.root, + roundingPrecision: commands.roundingPrecision, + shorthandCompacting: commands.skipShorthandCompacting ? false : true, + sourceMap: commands.sourceMap, + target: commands.output +}; + if (commands.args.length > 0) { - var relativeTo = (cleanOptions.rebase ? cleanOptions.root : false) || commands.args[0]; - cleanOptions.relativeTo = path.dirname(path.resolve(relativeTo)); + var relativeTo = (options.rebase ? options.root : false) || commands.args[0]; + options.relativeTo = path.dirname(path.resolve(relativeTo)); options.sources = commands.args.map(function(source) { var isRemote = /^https?:\/\//.test(source); - if (cleanOptions.processImport === false) + if (options.processImport === false) source += '@shallow'; return isRemote ? source : - path.relative(cleanOptions.relativeTo, path.resolve(source)); + path.relative(options.relativeTo, path.resolve(source)); }); } -if (cleanOptions.sourceMap && !cleanOptions.target) { +if (options.sourceMap && !options.target) { outputFeedback(['Source maps will not be built because you have not specified an output file.'], true); - cleanOptions.sourceMap = false; + options.sourceMap = false; } // ... and do the magic! @@ -134,8 +115,8 @@ if (options.sources) { } function minify(data) { - new CleanCSS(cleanOptions).minify(data, function(errors, minified) { - if (cleanOptions.debug) { + new CleanCSS(options).minify(data, function (errors, minified) { + if (options.debug) { console.error('Original: %d bytes', minified.stats.originalSize); console.error('Minified: %d bytes', minified.stats.minifiedSize); console.error('Efficiency: %d%', ~~(minified.stats.efficiency * 10000) / 100.0);