Further simplifies CleanCSS#minify by extracting debug options.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 26 Sep 2014 15:47:37 +0000 (16:47 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 10 Oct 2014 20:22:44 +0000 (21:22 +0100)
lib/clean.js

index a35399e..951f4aa 100644 (file)
@@ -37,32 +37,56 @@ var CleanCSS = module.exports = function CleanCSS(options) {
 
 CleanCSS.prototype.minify = function(data, callback) {
   var options = this.options;
+  var self = this;
 
   if (Buffer.isBuffer(data))
     data = data.toString();
 
   if (options.processImport || data.indexOf('@shallow') > 0) {
     // inline all imports
-    var self = this;
     var runner = callback ?
       process.nextTick :
-      function(callback) { return callback(); };
+      function (callback) { return callback(); };
 
-    return runner(function() {
+    return runner(function () {
       return new ImportInliner(self.context, options.inliner).process(data, {
         localOnly: !callback,
         root: options.root || process.cwd(),
         relativeTo: options.relativeTo,
-        whenDone: function(data) {
-          return minify.call(self, data, callback);
-        }
+        whenDone: runMinifier(callback, self)
       });
     });
   } else {
-    return minify.call(this, data, callback);
+    return runMinifier(callback, self)(data);
   }
 };
 
+function runMinifier(callback, self) {
+  return function (data) {
+    data = self.options.debug ?
+      minifyWithDebug(self, data) :
+      minify.call(self, data);
+
+    return callback ?
+      callback.call(self, self.context.errors.length > 0 ? self.context.errors : null, data) :
+      data;
+  };
+}
+
+function minifyWithDebug(self, data) {
+  var startedAt = process.hrtime();
+  self.stats.originalSize = data.length;
+
+  data = minify.call(self, data);
+
+  var elapsed = process.hrtime(startedAt);
+  self.stats.timeSpent = ~~(elapsed[0] * 1e3 + elapsed[1] / 1e6);
+  self.stats.efficiency = 1 - data.length / self.stats.originalSize;
+  self.stats.minifiedSize = data.length;
+
+  return data;
+}
+
 function benchmark(runner) {
   return function (processor, action) {
     var name =  processor.constructor.name + '#' + action;
@@ -73,9 +97,7 @@ function benchmark(runner) {
   };
 }
 
-function minify(data, callback) {
-  var startedAt;
-  var stats = this.stats;
+function minify(data) {
   var options = this.options;
   var context = this.context;
 
@@ -96,11 +118,6 @@ function minify(data, callback) {
   if (options.benchmark)
     run = benchmark(run);
 
-  if (options.debug) {
-    startedAt = process.hrtime();
-    stats.originalSize = data.length;
-  }
-
   run(commentsProcessor, 'escape');
   run(expressionsProcessor, 'escape');
   run(urlsProcessor, 'escape');
@@ -112,14 +129,5 @@ function minify(data, callback) {
   run(expressionsProcessor, 'restore');
   run(commentsProcessor, 'restore');
 
-  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 callback ?
-    callback.call(this, this.context.errors.length > 0 ? this.context.errors : null, data) :
-    data;
-};
+  return data;
+}