From 1b84e61c30909147a2a6f391851793fa7ee6f6dd Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Mon, 4 Nov 2013 08:24:42 +0100 Subject: [PATCH] Fixes #145 - adds minifications stats when calling library with `debug: true` option. * Refactors CLI `--debug` to use these stats too. * Moves `originalSize` field into `stats` one. --- History.md | 1 + README.md | 1 + bin/cleancss | 16 +++++----------- lib/clean.js | 21 ++++++++++++++++++--- test/custom-test.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/History.md b/History.md index 70920dae..775cf681 100644 --- a/History.md +++ b/History.md @@ -5,6 +5,7 @@ * Adds simplified and much faster empty elements removal. * Adds missing `@import` processing to our benchmark (run via `npm run bench`). * Fixed issue [#138](https://github.com/GoalSmashers/clean-css/issues/138) - makes CleanCSS interface OO. +* Fixed issue [#145](https://github.com/GoalSmashers/clean-css/issues/145) - debug mode in library too. * Fixed issue [#157](https://github.com/GoalSmashers/clean-css/issues/157) - gets rid of `removeEmpty` option. * Fixed issue [#159](https://github.com/GoalSmashers/clean-css/issues/159) - escaped quotes inside content. * Fixed issue [#162](https://github.com/GoalSmashers/clean-css/issues/162) - strip quotes from Base64 encoded URLs. diff --git a/README.md b/README.md index 98fdcbe3..e8142e2e 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ CleanCSS constructor accepts a hash as a parameter, i.e., * `noRebase` - whether to skip URLs rebasing * `noAdvanced` - set to true to disable advanced optimizations - selector & property merging, reduction, etc. * `selectorsMergeMode` - `ie8` for IE8 compatibility mode, `*` for merging all (default) +* `debug` - set to true to get minification statistics under `stats` property (see `test/custom-test.js` for examples) ### What are the clean-css' dev commands? diff --git a/bin/cleancss b/bin/cleancss index cd4fd702..1ba077b9 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -76,7 +76,7 @@ if (commands.skipAdvanced) if (commands.selectorsMergeMode) cleanOptions.selectorsMergeMode = commands.selectorsMergeMode; if (commands.debug) - options.debug = true; + cleanOptions.debug = true; if (commands.args.length > 0) { var source = commands.args[0]; options.source = source; @@ -103,18 +103,12 @@ if (options.source) { } function minify(data) { - var minified; var minifier = new CleanCSS(cleanOptions); + var minified = minifier.minify(data); - if (options.debug) { - var start = process.hrtime(); - minified = minifier.minify(data); - var taken = process.hrtime(start); - - console.error('Minification time: %dms', ~~(taken[0] * 1e3 + taken[1] / 1e6)); - console.error('Compression efficiency: %d%', ~~((1 - minified.length / CleanCSS.originalSize) * 100)); - } else { - minified = minifier.minify(data); + if (cleanOptions.debug) { + console.error('Minification time: %dms', minifier.stats.timeSpent); + console.error('Compression efficiency: %d%', ~~(minifier.stats.efficiency * 100)); } return minified; diff --git a/lib/clean.js b/lib/clean.js index 531e2524..74981c90 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -24,6 +24,7 @@ var SelectorsOptimizer = require('./selectors/optimizer'); module.exports = function(options) { var lineBreak = process.platform == 'win32' ? '\r\n' : '\n'; + var stats = {}; options = options || {}; options.keepBreaks = options.keepBreaks || false; @@ -33,7 +34,11 @@ module.exports = function(options) { options.processImport = true; var minify = function(data) { - this.originalSize = data.length; + var startedAt; + if (options.debug) { + startedAt = process.hrtime(); + stats.originalSize = data.length; + } var replace = function() { if (typeof arguments[0] == 'function') @@ -291,12 +296,22 @@ module.exports = function(options) { }); // trim spaces at beginning and end - return data.trim(); + data = data.trim(); + + if (options.debug) { + var elapsed = process.hrtime(startedAt); + stats.timeSpent = ~~(elapsed[0] * 1e3 + elapsed[1] / 1e6); + stats.efficiency = 1 - data.length / stats.originalSize; + stats.minifiedSize = data.length; + } + + return data; }; return { lineBreak: lineBreak, options: options, - minify: minify + minify: minify, + stats: stats }; }; diff --git a/test/custom-test.js b/test/custom-test.js index ddb9ca20..eef8039c 100644 --- a/test/custom-test.js +++ b/test/custom-test.js @@ -10,5 +10,34 @@ vows.describe('clean-custom').addBatch({ 'should process CSS correctly': function(process) { assert.equal(process('a{ color: #f00; }'), 'a{color:red}'); } + }, + 'no debug': { + topic: function() { + var minifier = new CleanCSS(); + minifier.minify('a{ color: #f00 }'); + return minifier; + }, + 'should not populate stats hash': function(minifier) { + assert.deepEqual({}, minifier.stats); + } + }, + 'debug': { + topic: function() { + var minifier = new CleanCSS({ debug: true }); + minifier.minify('a{ color: #f00 }'); + return minifier; + }, + 'should give time taken': function(minifier) { + assert.isNumber(minifier.stats.timeSpent); + }, + 'should give original size': function(minifier) { + assert.equal(minifier.stats.originalSize, 16); + }, + 'should give minified size': function(minifier) { + assert.equal(minifier.stats.minifiedSize, 12); + }, + 'should give efficiency': function(minifier) { + assert.equal(minifier.stats.efficiency, 0.25); + } } }).export(module); -- 2.34.1