Fixes #145 - adds minifications stats when calling library with `debug: true` option.
authorGoalSmashers <jakub@goalsmashers.com>
Mon, 4 Nov 2013 07:24:42 +0000 (08:24 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Mon, 4 Nov 2013 07:30:28 +0000 (08:30 +0100)
* Refactors CLI `--debug` to use these stats too.
* Moves `originalSize` field into `stats` one.

History.md
README.md
bin/cleancss
lib/clean.js
test/custom-test.js

index 70920da..775cf68 100644 (file)
@@ -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.
index 98fdcbe..e8142e2 100644 (file)
--- 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?
 
index cd4fd70..1ba077b 100755 (executable)
@@ -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;
index 531e252..74981c9 100644 (file)
@@ -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
   };
 };
index ddb9ca2..eef8039 100644 (file)
@@ -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);