Skip to content

Commit 5b046b4

Browse files
Fixes #5 - handles non-boolean compatibility options.
Why: * There was no way in CLI to set them as API allows doing it via a hash; * ideally this should be moved into API as a way of setting compatibility options in a similar manner as optimization levels.
1 parent 2a2eb29 commit 5b046b4

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Bumps clean-css dependency to 4.1.x.
55
* Fixed issue [#1](https://github.com/jakubpawlowicz/clean-css-cli/issues/1) - option to remove inlined files.
66
* Fixed issue [#2](https://github.com/jakubpawlowicz/clean-css-cli/issues/2) - glob matching source paths.
7+
* Fixed issue [#5](https://github.com/jakubpawlowicz/clean-css-cli/issues/5) - non-boolean compatibility options.
78
* Fixed issue [#7](https://github.com/jakubpawlowicz/clean-css-cli/issues/7) - using CLI as a module.
89

910
[4.0.12 / 2017-04-12](https://github.com/jakubpawlowicz/clean-css-cli/compare/v4.0.11...v4.0.12)

index.js

+30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var CleanCSS = require('clean-css');
55
var commands = require('commander');
66
var glob = require('glob');
77

8+
var COMPATIBILITY_PATTERN = /([\w\.]+)=(\w+)/g;
9+
810
function cli(process, beforeMinifyCallback) {
911
var packageConfig = fs.readFileSync(path.join(__dirname, 'package.json'));
1012
var buildVersion = JSON.parse(packageConfig).version;
@@ -211,6 +213,7 @@ function expandGlobs(paths) {
211213
function minify(process, beforeMinifyCallback, options, debugMode, removeInlinedFiles, data) {
212214
var cleanCss = new CleanCSS(options);
213215

216+
applyNonBooleanCompatibilityFlags(cleanCss, options.compatibility);
214217
beforeMinifyCallback(cleanCss);
215218
cleanCss.minify(data, function (errors, minified) {
216219
var mapFilename;
@@ -250,6 +253,33 @@ function minify(process, beforeMinifyCallback, options, debugMode, removeInlined
250253
});
251254
}
252255

256+
function applyNonBooleanCompatibilityFlags(cleanCss, compatibility) {
257+
var match;
258+
var scope;
259+
var parts;
260+
var i, l;
261+
262+
if (!compatibility) {
263+
return;
264+
}
265+
266+
patternLoop:
267+
while ((match = COMPATIBILITY_PATTERN.exec(compatibility)) !== null) {
268+
scope = cleanCss.options.compatibility;
269+
parts = match[1].split('.');
270+
271+
for (i = 0, l = parts.length - 1; i < l; i++) {
272+
scope = scope[parts[i]];
273+
274+
if (!scope) {
275+
continue patternLoop;
276+
}
277+
}
278+
279+
scope[parts.pop()] = match[2];
280+
}
281+
}
282+
253283
function outputFeedback(messages, isError) {
254284
var prefix = isError ? '\x1B[31mERROR\x1B[39m:' : 'WARNING:';
255285

test/binary-test.js

+7
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ vows.describe('cleancss')
394394
}
395395
})
396396
})
397+
.addBatch({
398+
'custom compatibility non-boolean options': pipedContext('.block-1{color:red}.block-2{color:red}', '--compatibility "selectors.mergeLimit=1,unknown.option=all" -O2', {
399+
'keeps source intact': function (error, stdout) {
400+
assert.equal(stdout, '.block-1{color:red}.block-2{color:red}');
401+
}
402+
})
403+
})
397404
.addBatch({
398405
'rounding precision': {
399406
'default': pipedContext('div{width:0.10051px}', '', {

0 commit comments

Comments
 (0)