Skip to content

Commit

Permalink
Fixes #145 - adds minifications stats when calling library with `debu…
Browse files Browse the repository at this point in the history
…g: true` option.

* Refactors CLI `--debug` to use these stats too.
* Moves `originalSize` field into `stats` one.
  • Loading branch information
GoalSmashers committed Nov 4, 2013
1 parent 2b5ffd7 commit 1b84e61
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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?

Expand Down
16 changes: 5 additions & 11 deletions bin/cleancss
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
21 changes: 18 additions & 3 deletions lib/clean.js
Expand Up @@ -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;
Expand All @@ -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')
Expand Down Expand Up @@ -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
};
};
29 changes: 29 additions & 0 deletions test/custom-test.js
Expand Up @@ -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);

0 comments on commit 1b84e61

Please sign in to comment.