Fixes #138 - makes CleanCSS interface fully object oriented.
authorGoalSmashers <jakub@goalsmashers.com>
Sun, 3 Nov 2013 14:27:44 +0000 (15:27 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Sun, 3 Nov 2013 17:25:31 +0000 (18:25 +0100)
* use `new CleanCSS(options).minify(css)` to minify CSS.
* adds 1.x -> 2.x migration guide.

History.md
README.md
bin/cleancss
lib/clean.js
test/batch-test.js
test/bench.js
test/custom-test.js
test/unit-test.js

index 988374b..2dda205 100644 (file)
@@ -4,6 +4,7 @@
 * Adds simplified and more advanced text escaping / restoring via `EscapeStore` class.
 * Adds simplified and much faster empty elements removal.
 * Adds missing `@import` processing to our benchmark (run via `npm run bench`).
+* Fixed issue [#138](https://github.com/GoalSmashers/clean-css/issues/138) - makes CleanCSS interface OO.
 * Fixed issue [#157](https://github.com/GoalSmashers/clean-css/issues/157) - gets rid of `removeEmpty` option.
 * Fixed issue [#159](https://github.com/GoalSmashers/clean-css/issues/159) - escaped quotes inside content.
 * Fixed issue [#162](https://github.com/GoalSmashers/clean-css/issues/162) - strip quotes from base64 encoded URLs.
index fc66c27..358a656 100644 (file)
--- a/README.md
+++ b/README.md
@@ -26,6 +26,33 @@ node.js 0.8.0+ (tested on CentOS, Ubuntu, OS X 10.6+, and Windows 7+)
 npm install clean-css
 ```
 
+### How to upgrade clean-css from 1.x to 2.x?
+
+#### Command-line interface (CLI)
+
+```
+npm update clean-css
+```
+
+or point `package.json` to version 2.x. That's it!
+
+#### Node.js module
+
+Update `clean-css` as for CLI above.
+Then change your JavaScript code from:
+
+```js
+var minimized = CleanCSS.process(source, options);
+```
+
+into
+
+```js
+var minimized = new CleanCSS(options).minify(source);
+```
+
+And you are done.
+
 ### How to use clean-css CLI?
 
 Clean-css accepts the following command line arguments (please make sure
@@ -84,15 +111,15 @@ cat one.css two.css three.css | cleancss | gzip -9 -c > merged-minified-and-gzip
 ### How to use clean-css programmatically?
 
 ```js
-var cleanCSS = require('clean-css');
+var CleanCSS = require('clean-css');
 var source = 'a{font-weight:bold;}';
-var minimized = cleanCSS.process(source);
+var minimized = new CleanCSS().minify(source);
 ```
 
-Process method accepts a hash as a second parameter, i.e.,
-`cleanCSS.process(source, options)` with the following options available:
+CleanCSS constructor accepts a hash as a parameter, i.e.,
+`new CleanCSS(options).minify(source)` with the following options available:
 
-* `keepSpecialComments` - `*` for keeping all (default), `1` for keeping first one, `0` for removing all
+* `keepSpecialComments` - `*` for keeping all (default), `1` for keeping first one only, `0` for removing all
 * `keepBreaks` - whether to keep line breaks (default is false)
 * `benchmark` - turns on benchmarking mode measuring time spent on cleaning up
   (run `npm run bench` to see example)
index 068082c..cd4fd70 100755 (executable)
@@ -104,16 +104,17 @@ if (options.source) {
 
 function minify(data) {
   var minified;
+  var minifier = new CleanCSS(cleanOptions);
 
   if (options.debug) {
     var start = process.hrtime();
-    minified = CleanCSS.process(data, cleanOptions);
+    minified = minifier.minify(data);
     var taken = process.hrtime(start);
 
     console.error('Minification time: %dms', ~~(taken[0] * 1e3 + taken[1] / 1e6));
     console.error('Compression efficiency: %d%', ~~((1 - minified.length / CleanCSS.originalSize) * 100));
   } else {
-    minified = CleanCSS.process(data, cleanOptions);
+    minified = minifier.minify(data);
   }
 
   return minified;
index cb17048..531e252 100644 (file)
@@ -22,23 +22,25 @@ var UrlsProcessor = require('./text/urls');
 
 var SelectorsOptimizer = require('./selectors/optimizer');
 
-var CleanCSS = {
-  process: function(data, options) {
+module.exports = function(options) {
+  var lineBreak = process.platform == 'win32' ? '\r\n' : '\n';
+
+  options = options || {};
+  options.keepBreaks = options.keepBreaks || false;
+
+  //active by default
+  if (options.processImport === undefined)
+    options.processImport = true;
+
+  var minify = function(data) {
+    this.originalSize = data.length;
+
     var replace = function() {
       if (typeof arguments[0] == 'function')
         arguments[0]();
       else
         data = data.replace.apply(data, arguments);
     };
-    var lineBreak = process.platform == 'win32' ? '\r\n' : '\n';
-    this.lineBreak = lineBreak;
-
-    options = options || {};
-    options.keepBreaks = options.keepBreaks || false;
-
-    //active by default
-    if (options.processImport === undefined)
-      options.processImport = true;
 
     // replace function
     if (options.benchmark) {
@@ -76,8 +78,6 @@ var CleanCSS = {
       });
     }
 
-    this.originalSize = data.length;
-
     replace(function escapeComments() {
       data = commentsProcessor.escape(data);
     });
@@ -292,7 +292,11 @@ var CleanCSS = {
 
     // trim spaces at beginning and end
     return data.trim();
-  }
-};
+  };
 
-module.exports = CleanCSS;
+  return {
+    lineBreak: lineBreak,
+    options: options,
+    minify: minify
+  };
+};
index dea564c..b5194e8 100644 (file)
@@ -2,7 +2,7 @@ var vows = require('vows');
 var path = require('path');
 var fs = require('fs');
 var assert = require('assert');
-var cleanCSS = require('../index');
+var CleanCSS = require('../index');
 
 var lineBreak = process.platform == 'win32' ? /\r\n/g : /\n/g;
 
@@ -27,10 +27,10 @@ var batchContexts = function() {
       }
     };
     context[testName]['minimizing ' + testName + '.css'] = function(data) {
-      var processed = cleanCSS.process(data.plain, {
+      var processed = new CleanCSS({
         keepBreaks: true,
         root: data.root
-      });
+      }).minify(data.plain);
 
       var processedTokens = processed.split(lineBreak);
       var minimizedTokens = data.minimized.split(lineBreak);
index aa6afcb..93e24f5 100644 (file)
@@ -1,4 +1,4 @@
-var cleanCSS = require('../index');
+var CleanCSS = require('../index');
 var path = require('path');
 
 if (!process.hrtime) {
@@ -10,7 +10,7 @@ var benchDir = path.join(__dirname, 'data-bench');
 var cssData = require('fs').readFileSync(path.join(benchDir, 'complex.css'), 'utf8');
 
 var start = process.hrtime();
-cleanCSS.process(cssData, { benchmark: true, root: benchDir });
+new CleanCSS({ benchmark: true, root: benchDir }).minify(cssData);
 
 var itTook = process.hrtime(start);
 console.log('complete minification: %d ms', 1000 * itTook[0] + itTook[1] / 1000000.0);
index bb13d84..ddb9ca2 100644 (file)
@@ -3,9 +3,9 @@ var assert = require('assert');
 var CleanCSS = require('../index');
 
 vows.describe('clean-custom').addBatch({
-  'imported as function': {
+  'imported as function': {
     topic: function() {
-      return CleanCSS.process;
+      return new CleanCSS().minify;
     },
     'should process CSS correctly': function(process) {
       assert.equal(process('a{  color: #f00;  }'), 'a{color:red}');
index 5054386..783cb8b 100644 (file)
@@ -1,7 +1,7 @@
 var vows = require('vows');
 var assert = require('assert');
 var path = require('path');
-var cleanCSS = require('../index');
+var CleanCSS = require('../index');
 var ColorShortener = require('../lib/colors/shortener');
 
 var lineBreak = process.platform == 'win32' ? '\r\n' : '\n';
@@ -9,14 +9,14 @@ var cssContext = function(groups, options) {
   var context = {};
   var clean = function(expectedCSS) {
     return function(css) {
-      var cleanedCSS = null;
+      var minifiedCSS = null;
       try {
-        cleanedCSS = cleanCSS.process(css, options);
+        minifiedCSS = new CleanCSS(options).minify(css);
       } catch (e) {
-        // swallow - cleanedCSS is set to null and that's the new expected value
+        // swallow - minifiedCSS is set to null and that's the new expected value
       }
 
-      assert.equal(cleanedCSS, expectedCSS);
+      assert.equal(minifiedCSS, expectedCSS);
     };
   };