Simplifies CleanCSS#minify method.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 26 Sep 2014 15:15:48 +0000 (16:15 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 10 Oct 2014 20:22:44 +0000 (21:22 +0100)
* All processing is now done through content processors with predicatable API.
* Benchmarking also got easier because of that.

lib/clean.js

index ae15391..a35399e 100644 (file)
@@ -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);