Fixes debug mode stats for stylesheets using `@import` statements.
authorGoalSmashers <jakub@goalsmashers.com>
Thu, 12 Dec 2013 16:30:54 +0000 (17:30 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Mon, 6 Jan 2014 18:37:35 +0000 (19:37 +0100)
* Size of inlined files were not taken into account resulting in wrong "Original size" and "Efficiency".

History.md
lib/clean.js
test/binary-test.js

index 6b80334..396c05a 100644 (file)
@@ -3,6 +3,7 @@
 
 * Adds an optional callback to minify method.
 * Deprecates --selectors-merge-mode / selectorsMergeMode in favor to --compatibility / compatibility.
+* Fixes debug mode stats for stylesheets using `@import` statements.
 * Skips empty removal if advanced processing is enabled.
 * Fixed issue [#85](https://github.com/GoalSmashers/clean-css/issues/85) - resolving protocol `@import`s.
 * Fixed issue [#160](https://github.com/GoalSmashers/clean-css/issues/160) - re-runs optimizer until a clean pass.
index 51a563e..a9e616d 100644 (file)
@@ -60,11 +60,6 @@ CleanCSS.prototype.minify = function(data, callback) {
   if (Buffer.isBuffer(data))
     data = data.toString();
 
-  if (options.debug) {
-    this.startedAt = process.hrtime();
-    this.stats.originalSize = data.length;
-  }
-
   if (options.processImport) {
     // inline all imports
     var self = this;
@@ -88,6 +83,7 @@ CleanCSS.prototype.minify = function(data, callback) {
 };
 
 var minify = function(data, callback) {
+  var startedAt;
   var stats = this.stats;
   var options = this.options;
   var context = this.context;
@@ -102,6 +98,11 @@ var minify = function(data, callback) {
   var freeTextProcessor = new FreeTextProcessor();
   var urlsProcessor = new UrlsProcessor();
 
+  if (options.debug) {
+    this.startedAt = process.hrtime();
+    this.stats.originalSize = data.length;
+  }
+
   var replace = function() {
     if (typeof arguments[0] == 'function')
       arguments[0]();
@@ -125,6 +126,11 @@ var minify = function(data, callback) {
     };
   }
 
+  if (options.debug) {
+    startedAt = process.hrtime();
+    stats.originalSize = data.length;
+  }
+
   replace(function escapeComments() {
     data = commentsProcessor.escape(data);
   });
@@ -358,7 +364,7 @@ var minify = function(data, callback) {
   data = data.trim();
 
   if (options.debug) {
-    var elapsed = process.hrtime(this.startedAt);
+    var elapsed = process.hrtime(startedAt);
     stats.timeSpent = ~~(elapsed[0] * 1e3 + elapsed[1] / 1e6);
     stats.efficiency = 1 - data.length / stats.originalSize;
     stats.minifiedSize = data.length;
index 21688e4..4663521 100644 (file)
@@ -100,6 +100,13 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({
       assert.include(stderr, path.join(process.cwd(), 'test/data/imports-min.css'));
     },
   }),
+  'piped with correct debug info on inlining': pipedContext('@import url(test/data/imports.css);', '-d', {
+    'should output correct info': function(error, stdout, stderr) {
+      assert.include(stderr, 'Original: 120 bytes');
+      assert.include(stderr, 'Minified: 86 bytes');
+      assert.include(stderr, 'Efficiency: 28.33%');
+    },
+  }),
   'to output file with debug info': pipedContext('a{color: #f00;}', '-d -o debug.css', {
     'should output nothing to stdout and debug info to stderr': function(error, stdout, stderr) {
       assert.equal(stdout, '');