From 4950d8a09b41dbbb04dc4cc21c7521b37483d4bd Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Thu, 12 Dec 2013 17:30:54 +0100 Subject: [PATCH] Fixes debug mode stats for stylesheets using `@import` statements. * Size of inlined files were not taken into account resulting in wrong "Original size" and "Efficiency". --- History.md | 1 + lib/clean.js | 18 ++++++++++++------ test/binary-test.js | 7 +++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/History.md b/History.md index 6b803342..396c05ae 100644 --- a/History.md +++ b/History.md @@ -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. diff --git a/lib/clean.js b/lib/clean.js index 51a563e5..a9e616d3 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -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; diff --git a/test/binary-test.js b/test/binary-test.js index 21688e45..46635210 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -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, ''); -- 2.34.1