Adds an optional callback to CleanCSS#minify method.
authorGoalSmashers <jakub@goalsmashers.com>
Tue, 10 Dec 2013 09:18:08 +0000 (10:18 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Mon, 6 Jan 2014 18:21:13 +0000 (19:21 +0100)
History.md
lib/clean.js
test/module-test.js

index 7894e7d..82d5d92 100644 (file)
@@ -1,6 +1,7 @@
 [2.1.0 / 2013-xx-xx (UNRELEASED)](https://github.com/GoalSmashers/clean-css/compare/v2.0.0...HEAD)
 ==================
 
+* Adds an optional callback to minify method.
 * Deprecates --selectors-merge-mode / selectorsMergeMode in favor to --compatibility / compatibility.
 * Skips empty removal if advanced processing is enabled.
 * Fixed issue [#160](https://github.com/GoalSmashers/clean-css/issues/160) - re-runs optimizer until a clean pass.
index 187aaa5..2c3b03f 100644 (file)
@@ -51,7 +51,7 @@ var CleanCSS = module.exports = function CleanCSS(options) {
   this.lineBreak = process.platform == 'win32' ? '\r\n' : '\n';
 };
 
-CleanCSS.prototype.minify = function(data) {
+CleanCSS.prototype.minify = function(data, callback) {
   var startedAt;
   var stats = this.stats;
   var options = this.options;
@@ -348,5 +348,7 @@ CleanCSS.prototype.minify = function(data) {
     stats.minifiedSize = data.length;
   }
 
-  return data;
+  return callback ?
+    callback.call(this, this.context.errors.length > 0 ? this.context.errors : null, data) :
+    data;
 };
index b485ae5..8cce9fa 100644 (file)
@@ -40,6 +40,30 @@ vows.describe('module tests').addBatch({
       delete CleanCSS.prototype.foo;
     }
   },
+  'with callback passed and no errors': {
+    topic: function() {
+      new CleanCSS().minify('a{color:#f00}', this.callback);
+    },
+    'should correctly set context': function() {
+      assert.equal(true, this instanceof CleanCSS);
+    },
+    'should yield no error': function(errors, minified) {
+      /* jshint unused: false */
+      assert.equal(errors, null);
+    },
+    'should yield minified data': function(errors, minified) {
+      assert.equal(minified, 'a{color:red}');
+    }
+  },
+  'with callback passed and one error': {
+    topic: function() {
+      new CleanCSS().minify('@import "missing.css";', this.callback);
+    },
+    'should yield no error and minify': function(errors, minified) {
+      /* jshint unused: false */
+      assert.equal(errors.length, 1);
+    }
+  },
   'no debug': {
     topic: function() {
       var minifier = new CleanCSS();