From 9e1fdd2846a8fb71c7e0504d743f569cb8e3b6cf Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Wed, 7 Dec 2016 11:54:15 +0100 Subject: [PATCH] Removes `debug` API switch. Why: * Stats are always calculated and available under `stats` property of the output hash. --- History.md | 1 + README.md | 3 +-- bin/cleancss | 4 ++-- lib/clean.js | 45 +++++++++++++++------------------------ lib/utils/read-sources.js | 4 ++++ test/module-test.js | 10 +-------- 6 files changed, 26 insertions(+), 41 deletions(-) diff --git a/History.md b/History.md index abb706dc..bd1c42e2 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ * Adds more detailed error & warning messages on top of the new tokenizer. * Requires Node.js 4.0+ to run. +* Removes `debug` API switch as stats are always gathered and available under `stats` property. * Replaces the old tokenizer with a new one which doesn't use any escaping. * Replaces the old `@import` inlining with one on top of the new tokenizer. diff --git a/README.md b/README.md index c63502dc..2f26cda0 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,6 @@ CleanCSS constructor accepts a hash as a parameter, i.e., * `aggressiveMerging` - set to false to disable aggressive merging of properties. * `benchmark` - turns on benchmarking mode measuring time spent on cleaning up (run `npm run bench` to see example) * `compatibility` - enables compatibility mode, see [below for more examples](#how-to-set-a-compatibility-mode) -* `debug` - set to true to get minification statistics under `stats` property (see `test/custom-test.js` for examples) * `inliner` - a hash of options for `@import` inliner, see [test/protocol-imports-test.js](https://github.com/jakubpawlowicz/clean-css/blob/master/test/protocol-imports-test.js#L372) for examples, or [this comment](https://github.com/jakubpawlowicz/clean-css/issues/612#issuecomment-119594185) for a proxy use case. * `keepBreaks` - whether to keep line breaks (default is false) * `keepSpecialComments` - `*` for keeping all (default), `1` for keeping first one only, `0` for removing all @@ -141,7 +140,7 @@ The output of `minify` method (or the 2nd argument to passed callback) is a hash * `sourceMap` - output source map (if requested with `sourceMap` option) * `errors` - a list of errors raised * `warnings` - a list of warnings raised -* `stats` - a hash of statistic information (if requested with `debug` option): +* `stats` - a hash of statistic information: * `originalSize` - original content size (after import inlining) * `minifiedSize` - optimized content size * `timeSpent` - time spent on optimizations diff --git a/bin/cleancss b/bin/cleancss index 24284660..d51a5455 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -60,11 +60,11 @@ if (!fromStdin && commands.args.length === 0) { } // Now coerce commands into CleanCSS configuration... +var debugMode = commands.debug; var options = { advanced: commands.skipAdvanced ? false : true, aggressiveMerging: commands.skipAggressiveMerging ? false : true, compatibility: commands.compatibility, - debug: commands.debug, inliner: commands.timeout ? { timeout: parseFloat(commands.timeout) * 1000 } : undefined, keepBreaks: !!commands.keepLineBreaks, keepSpecialComments: commands.s0 ? 0 : (commands.s1 ? 1 : '*'), @@ -139,7 +139,7 @@ function processImportFrom(rules) { function minify(data) { new CleanCSS(options).minify(data, function (errors, minified) { - if (options.debug) { + if (debugMode) { console.error('Original: %d bytes', minified.stats.originalSize); console.error('Minified: %d bytes', minified.stats.minifiedSize); console.error('Efficiency: %d%', ~~(minified.stats.efficiency * 10000) / 100.0); diff --git a/lib/clean.js b/lib/clean.js index 31ba217c..7fdbe384 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -28,7 +28,6 @@ var CleanCSS = module.exports = function CleanCSS(options) { aggressiveMerging: undefined === options.aggressiveMerging ? true : !!options.aggressiveMerging, benchmark: options.benchmark, compatibility: compatibility(options.compatibility), - debug: options.debug, explicitRoot: !!options.root, explicitTarget: !!options.target, inliner: options.inliner || {}, @@ -80,7 +79,13 @@ function proxyOptionsFrom(httpProxy) { CleanCSS.prototype.minify = function (input, callback) { var context = { - stats: {}, + stats: { + efficiency: 0, + minifiedSize: 0, + originalSize: 0, + startedAt: process.hrtime(), + timeSpent: 0 + }, errors: [], warnings: [], options: this.options, @@ -125,36 +130,20 @@ function optimize(tokens, context) { } function withMetadata(output, context) { - output.stats = context.stats; + output.stats = calculateStatsFrom(output.styles, context); output.errors = context.errors; output.warnings = context.warnings; + return output; } -// function minifyWithDebug(context, data) { -// var startedAt = process.hrtime(); -// context.stats.originalSize = context.sourceTracker.removeAll(data).length; - -// data = minify(context, data); - -// var elapsed = process.hrtime(startedAt); -// context.stats.timeSpent = ~~(elapsed[0] * 1e3 + elapsed[1] / 1e6); -// context.stats.efficiency = 1 - data.styles.length / context.stats.originalSize; -// context.stats.minifiedSize = data.styles.length; +function calculateStatsFrom(styles, context) { + var elapsed = process.hrtime(context.stats.startedAt); -// return data; -// } + delete context.stats.startedAt; + context.stats.timeSpent = ~~(elapsed[0] * 1e3 + elapsed[1] / 1e6); + context.stats.efficiency = 1 - styles.length / context.stats.originalSize; + context.stats.minifiedSize = styles.length; -// function minify(context, data) { - // var options = context.options; - // var stringify = options.sourceMap ? - // sourceMapStringify : - // simpleStringify; - - // var tokens = tokenize(data, context); - // var allTokens = inline() - // var optimizedTokens = optimize(tokens, context); - // var output = stringify(optimizedTokens, context); - - // return output; -// } + return context.stats; +} diff --git a/lib/utils/read-sources.js b/lib/utils/read-sources.js index 3de64f98..6177059c 100644 --- a/lib/utils/read-sources.js +++ b/lib/utils/read-sources.js @@ -37,6 +37,8 @@ function readSources(input, context, callback) { function fromString(input, context, parentInlinerContext, callback) { var tokens = tokenize(input, context); + context.stats.originalSize += input.length; + return context.options.processImport ? inlineImports(tokens, context, parentInlinerContext, callback) : callback(tokens); @@ -87,6 +89,8 @@ function fromHash(input, context, parentInlinerContext, callback) { config ) ); + + context.stats.originalSize += source.styles.length; } return context.options.processImport ? diff --git a/test/module-test.js b/test/module-test.js index b73e54dd..0ac7f2e0 100644 --- a/test/module-test.js +++ b/test/module-test.js @@ -74,18 +74,10 @@ vows.describe('module tests').addBatch({ assert.equal(minified.styles, '@import url(https://fonts.googleapis.com/css?family=Open+Sans);'); } }, - 'no debug': { + 'debug info': { 'topic': function () { return new CleanCSS().minify('a{ color: #f00 }'); }, - 'should not populate stats hash': function (error, minified) { - assert.isEmpty(minified.stats); - } - }, - 'debug': { - 'topic': function () { - return new CleanCSS({ debug: true }).minify('a{ color: #f00 }'); - }, 'should give time taken': function (error, minified) { assert.isNumber(minified.stats.timeSpent); }, -- 2.34.1