* use `new CleanCSS(options).minify(css)` to minify CSS.
* adds 1.x -> 2.x migration guide.
* 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.
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
### 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)
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;
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) {
});
}
- this.originalSize = data.length;
-
replace(function escapeComments() {
data = commentsProcessor.escape(data);
});
// trim spaces at beginning and end
return data.trim();
- }
-};
+ };
-module.exports = CleanCSS;
+ return {
+ lineBreak: lineBreak,
+ options: options,
+ minify: minify
+ };
+};
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;
}
};
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);
-var cleanCSS = require('../index');
+var CleanCSS = require('../index');
var path = require('path');
if (!process.hrtime) {
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);
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}');
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';
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);
};
};