From: Jakub Pawlowicz Date: Fri, 26 Sep 2014 15:15:48 +0000 (+0100) Subject: Simplifies CleanCSS#minify method. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=b120c5867cb79a2cc907fae3dd7a0e39a839ed22;p=clean-css.git Simplifies CleanCSS#minify method. * All processing is now done through content processors with predicatable API. * Benchmarking also got easier because of that. --- diff --git a/lib/clean.js b/lib/clean.js index ae15391d..a35399e0 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -63,6 +63,16 @@ CleanCSS.prototype.minify = function(data, callback) { } }; +function benchmark(runner) { + return function (processor, action) { + var name = processor.constructor.name + '#' + action; + var start = process.hrtime(); + runner(processor, action); + var itTook = process.hrtime(start); + console.log('%d ms: ' + name, 1000 * itTook[0] + itTook[1] / 1000000); + }; +} + function minify(data, callback) { var startedAt; var stats = this.stats; @@ -74,69 +84,33 @@ function minify(data, callback) { var freeTextProcessor = new FreeTextProcessor(); var urlsProcessor = new UrlsProcessor(); - var replace = function() { - if (typeof arguments[0] == 'function') - arguments[0](); - else - data = data.replace.apply(data, arguments); - }; + var urlRebase = new UrlRebase(options, context); - // replace function - if (options.benchmark) { - var originalReplace = replace; - replace = function(pattern, replacement) { - var name = typeof pattern == 'function' ? - /function (\w+)\(/.exec(pattern.toString())[1] : - pattern; + var run = function (processor, action, enabled) { + if (enabled === false) + return; - var start = process.hrtime(); - originalReplace(pattern, replacement); + data = processor[action](data); + }; - var itTook = process.hrtime(start); - console.log('%d ms: ' + name, 1000 * itTook[0] + itTook[1] / 1000000); - }; - } + if (options.benchmark) + run = benchmark(run); if (options.debug) { startedAt = process.hrtime(); stats.originalSize = data.length; } - replace(function escapeComments() { - data = commentsProcessor.escape(data); - }); - - replace(function escapeExpressions() { - data = expressionsProcessor.escape(data); - }); - - replace(function escapeFreeText() { - data = freeTextProcessor.escape(data); - }); - - replace(function escapeUrls() { - data = urlsProcessor.escape(data); - }); - - replace(function restoreUrls() { - data = urlsProcessor.restore(data); - }); - - replace(function rebaseUrls() { - data = options.noRebase ? data : new UrlRebase(options, context).process(data); - }); - - replace(function restoreFreeText() { - data = freeTextProcessor.restore(data); - }); - - replace(function restoreComments() { - data = commentsProcessor.restore(data); - }); - - replace(function restoreExpressions() { - data = expressionsProcessor.restore(data); - }); + run(commentsProcessor, 'escape'); + run(expressionsProcessor, 'escape'); + run(urlsProcessor, 'escape'); + run(freeTextProcessor, 'escape'); + run(selectorsOptimizer, 'process'); + run(freeTextProcessor, 'restore'); + run(urlsProcessor, 'restore'); + run(urlRebase, 'process', !options.noRebase); + run(expressionsProcessor, 'restore'); + run(commentsProcessor, 'restore'); if (options.debug) { var elapsed = process.hrtime(startedAt);