From 677d4d0ee82f1cc9da0d8571e2614d673b7d79e5 Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Sun, 3 Nov 2013 15:27:44 +0100 Subject: [PATCH] Fixes #138 - makes CleanCSS interface fully object oriented. * use `new CleanCSS(options).minify(css)` to minify CSS. * adds 1.x -> 2.x migration guide. --- History.md | 1 + README.md | 37 ++++++++++++++++++++++++++++++++----- bin/cleancss | 5 +++-- lib/clean.js | 36 ++++++++++++++++++++---------------- test/batch-test.js | 6 +++--- test/bench.js | 4 ++-- test/custom-test.js | 4 ++-- test/unit-test.js | 10 +++++----- 8 files changed, 68 insertions(+), 35 deletions(-) diff --git a/History.md b/History.md index 988374be..2dda2054 100644 --- a/History.md +++ b/History.md @@ -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. diff --git a/README.md b/README.md index fc66c276..358a656e 100644 --- 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) diff --git a/bin/cleancss b/bin/cleancss index 068082cb..cd4fd702 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -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; diff --git a/lib/clean.js b/lib/clean.js index cb170481..531e2524 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -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 + }; +}; diff --git a/test/batch-test.js b/test/batch-test.js index dea564c7..b5194e88 100644 --- a/test/batch-test.js +++ b/test/batch-test.js @@ -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); diff --git a/test/bench.js b/test/bench.js index aa6afcbd..93e24f5b 100644 --- a/test/bench.js +++ b/test/bench.js @@ -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); diff --git a/test/custom-test.js b/test/custom-test.js index bb13d846..ddb9ca20 100644 --- a/test/custom-test.js +++ b/test/custom-test.js @@ -3,9 +3,9 @@ var assert = require('assert'); var CleanCSS = require('../index'); vows.describe('clean-custom').addBatch({ - 'imported as function': { + 'imported as a 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}'); diff --git a/test/unit-test.js b/test/unit-test.js index 50543861..783cb8bb 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -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); }; }; -- 2.34.1