From d7d464c5018c9adfbf47739aa2b2227f4bb7b4bc Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 8 Jan 2017 10:52:37 +0100 Subject: [PATCH] See #850 - restores `-On` CLI option. Thanks @alexlamsl for figuring it out! --- README.md | 16 +++++++--------- bin/cleancss | 36 ++++++++++++++++++++++-------------- test/batch-test.js | 2 +- test/binary-test.js | 32 ++++++++++++++++---------------- 4 files changed, 46 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 586f974a..97d3aff4 100644 --- a/README.md +++ b/README.md @@ -71,9 +71,7 @@ Options: -c, --compatibility [ie7|ie8] Force compatibility mode (see Readme for advanced examples) -d, --debug Shows debug information (minification time & compression efficiency) -o, --output [output-file] Use [output-file] as output instead of STDOUT - --O0 Turn on level 0 optimizations - --O1 [optimizations] Turn on level 1 optimizations (default), see examples below - --O2 [optimizations] Turn on level 2 optimizations, see examples below + -O [optimizations] Turn on level optimizations; optionally accepts a list of fine-grained options, defaults to `1`, see examples below --beautify Formats output CSS by using indentation and one rule or property per line --inline [rules] Enables inlining for listed sources (defaults to `local`) --inline-timeout [seconds] Per connection timeout when fetching remote stylesheets (defaults to 5 seconds) @@ -114,14 +112,14 @@ Please note there is a difference between passing in a concatenated string and l Level 0 optimizations: ```bash -cleancss --O0 one.css +cleancss -O0 one.css ``` Level 1 optimizations: ```bash -cleancss --O1 one.css -cleancss --O1 roundingPrecision:4;specialComments:1 one.css +cleancss -O1 one.css +cleancss -O1 roundingPrecision:4;specialComments:1 one.css # `roundingPrecision` rounds pixel values to `N` decimal places; `off` disables rounding; defaults to `off` # `specialComments` denotes a number of /*! ... */ comments preserved; defaults to `all` ``` @@ -129,8 +127,8 @@ cleancss --O1 roundingPrecision:4;specialComments:1 one.css Level 2 optimizations: ```bash -cleancss --O2 one.css -cleancss --O2 mediaMerging:off;restructuring:off;semanticMerging:on;shorthandCompacting:off one.css +cleancss -O2 one.css +cleancss -O2 mediaMerging:off;restructuring:off;semanticMerging:on;shorthandCompacting:off one.css # `mediaMerging` controls `@media` merging behavior; defaults to `on` (alias to `true`) # `restructuring` controls content restructuring behavior; defaults `on` (alias to `true`) # `semanticMerging` controls semantic merging behavior; defaults to `off` (alias to `false`) @@ -283,7 +281,7 @@ Source maps are generated using [source-map](https://github.com/mozilla/source-m The level 1 `roundingPrecision` optimization option accept a string with per-unit rounding precision settings, e.g. ``` -clean-css --O1 roundingPrecision:all=3,px=5 +clean-css -O1 roundingPrecision:all=3,px=5 ``` or diff --git a/bin/cleancss b/bin/cleancss index e6c35f99..c3070ee1 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -17,9 +17,7 @@ commands .option('-c, --compatibility [ie7|ie8]', 'Force compatibility mode (see Readme for advanced examples)') .option('-d, --debug', 'Shows debug information (minification time & compression efficiency)') .option('-o, --output [output-file]', 'Use [output-file] as output instead of STDOUT') - .option('--O0', 'Turn on level 0 optimizations') - .option('--O1 [optimizations]', 'Turn on level 1 optimizations (default), see examples below') - .option('--O2 [optimizations]', 'Turn on level 2 optimizations, see examples below') + .option('-O [optimizations]', 'Turn on level optimizations; optionally accepts a list of fine-grained options, defaults to `1`, see examples below', function (val) { return Math.abs(parseInt(val)); }) .option('--beautify', 'Formats output CSS by using indentation and one rule or property per line') .option('--inline [rules]', 'Enables inlining for listed sources (defaults to `local`)') .option('--inline-timeout [seconds]', 'Per connection timeout when fetching remote stylesheets (defaults to 5 seconds)', parseFloat) @@ -36,17 +34,17 @@ commands.on('--help', function () { console.log(' %> cleancss one.css two.css three.css | gzip -9 -c > merged-minified-and-gzipped.css.gz'); console.log(''); console.log(' Level 0 optimizations:'); - console.log(' %> cleancss --O0 one.css'); + console.log(' %> cleancss -O0 one.css'); console.log(''); console.log(' Level 1 optimizations:'); - console.log(' %> cleancss --O1 one.css'); - console.log(' %> cleancss --O1 roundingPrecision:4;specialComments:1 one.css'); + console.log(' %> cleancss -O1 one.css'); + console.log(' %> cleancss -O1 roundingPrecision:4;specialComments:1 one.css'); console.log(' %> # `roundingPrecision` rounds pixel values to `N` decimal places; `off` disables rounding; defaults to `off`'); console.log(' %> # `specialComments` denotes a number of /*! ... */ comments preserved; defaults to `all`'); console.log(''); console.log(' Level 2 optimizations:'); - console.log(' %> cleancss --O2 one.css'); - console.log(' %> cleancss --O2 mediaMerging:off;restructuring:off;semanticMerging:on;shorthandCompacting:off one.css'); + console.log(' %> cleancss -O2 one.css'); + console.log(' %> cleancss -O2 mediaMerging:off;restructuring:off;semanticMerging:on;shorthandCompacting:off one.css'); console.log(' %> # `mediaMerging` controls `@media` merging behavior; defaults to `on` (alias to `true`)'); console.log(' %> # `restructuring` controls content restructuring behavior; defaults to `on` (alias to `true`)'); console.log(' %> # `semanticMerging` controls semantic merging behavior; defaults to `off` (alias to `false`)'); @@ -57,18 +55,28 @@ commands.on('--help', function () { commands.parse(process.argv); -// Fixes commander issue in the following case `cleancss --O1 source.css` where `source.css` is taken as `O1` value -if (commands.O1 && (fs.existsSync(commands.O1) || isRemote(commands.O1))) { - commands.args.unshift(commands.O1); +if (commands.O === 0) { + commands.O0 = true; +} + +if (commands.O === 1) { commands.O1 = true; } -// Fixes commander issue in the following case `cleancss --O2 source.css` where `source.css` is taken as `O2` value -if (commands.O2 && (fs.existsSync(commands.O2) || isRemote(commands.O2))) { - commands.args.unshift(commands.O2); +if (commands.O === 2) { commands.O2 = true; } +// Fixes commander issue in the following case `cleancss -O1 [configuration]` where `[configuration]` is taken as input stylesheet +if (commands.O1 && commands.args.length > 0 && !fs.existsSync(commands.args[0]) && !isRemote(commands.args[0])) { + commands.O1 = commands.args.shift(); +} + +// Fixes commander issue in the following case `cleancss -O2 [configuration]` where `[configuration]` is taken as input stylesheet +if (commands.O2 && commands.args.length > 0 && !fs.existsSync(commands.args[0]) && !isRemote(commands.args[0])) { + commands.O2 = commands.args.shift(); +} + // If no sensible data passed in just print help and exit var fromStdin = !process.env.__DIRECT__ && !process.stdin.isTTY; if (!fromStdin && commands.args.length === 0) { diff --git a/test/batch-test.js b/test/batch-test.js index 501bb3e8..c3928c84 100644 --- a/test/batch-test.js +++ b/test/batch-test.js @@ -71,7 +71,7 @@ function batchContexts() { 'minifying via CLI': { 'topic': function (data) { exec( - '__DIRECT__=1 ./bin/cleancss -b --O2 ' + (isIE7Mode ? '-c ie7 ' : '') + path.join(dir, filename), + '__DIRECT__=1 ./bin/cleancss -b -O2 ' + (isIE7Mode ? '-c ie7 ' : '') + path.join(dir, filename), { maxBuffer: 500 * 1024 }, this.callback.bind(null, data) ); diff --git a/test/binary-test.js b/test/binary-test.js index 72e58d13..df62a4c2 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -94,14 +94,14 @@ vows.describe('./bin/cleancss') }) }) .addBatch({ - 'strip all but first comment': pipedContext('/*!1st*//*! 2nd */a{display:block}', '--O1 specialComments:1', { + 'strip all but first comment': pipedContext('/*!1st*//*! 2nd */a{display:block}', '-O1 specialComments:1', { 'should keep the 2nd comment': function (error, stdout) { assert.equal(stdout, '/*!1st*/a{display:block}'); } }) }) .addBatch({ - 'strip all comments': pipedContext('/*!1st*//*! 2nd */a{display:block}', '--O1 specialComments:0', { + 'strip all comments': pipedContext('/*!1st*//*! 2nd */a{display:block}', '-O1 specialComments:0', { 'should keep the 2nd comment': function (error, stdout) { assert.equal(stdout, 'a{display:block}'); } @@ -155,14 +155,14 @@ vows.describe('./bin/cleancss') }) }) .addBatch({ - 'skip level 2 optimizations': pipedContext('a{color:red}p{color:red}', '--O1', { + 'skip level 2 optimizations': pipedContext('a{color:red}p{color:red}', '-O1', { 'should do basic optimizations only': function (error, stdout) { assert.equal(stdout, 'a{color:red}p{color:red}'); } }) }) .addBatch({ - 'skip restructuring optimizations': pipedContext('div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}', '--O2 restructuring:off', { + 'skip restructuring optimizations': pipedContext('div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}', '-O2 restructuring:off', { 'should do basic optimizations only': function (error, stdout) { assert.equal(stdout, 'div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}'); } @@ -178,7 +178,7 @@ vows.describe('./bin/cleancss') }) }) .addBatch({ - 'from source': binaryContext('--O2 ./test/fixtures/reset.css', { + 'from source': binaryContext('-O2 ./test/fixtures/reset.css', { 'should minimize': function (error, stdout) { var minimized = fs.readFileSync('./test/fixtures/reset-min.css', 'utf-8').replace(lineBreakRegExp, ''); assert.equal(stdout, minimized); @@ -193,7 +193,7 @@ vows.describe('./bin/cleancss') }) }) .addBatch({ - 'to file': binaryContext('--O2 -o ./reset1-min.css ./test/fixtures/reset.css', { + 'to file': binaryContext('-O2 -o ./reset1-min.css ./test/fixtures/reset.css', { 'should give no output': function (error, stdout) { assert.isEmpty(stdout); }, @@ -411,27 +411,27 @@ vows.describe('./bin/cleancss') assert.equal(stdout, 'div{width:.10051px}'); } }), - 'custom': pipedContext('div{width:0.00051px}', '--O1 roundingPrecision:4', { + 'custom': pipedContext('div{width:0.00051px}', '-O1 roundingPrecision:4', { 'should keep 4 decimal places': function (error, stdout) { assert.equal(stdout, 'div{width:.0005px}'); } }), - 'zero': pipedContext('div{width:1.5051px}', '--O1 roundingPrecision:0', { + 'zero': pipedContext('div{width:1.5051px}', '-O1 roundingPrecision:0', { 'should keep 0 decimal places': function (error, stdout) { assert.equal(stdout, 'div{width:2px}'); } }), - 'disabled': pipedContext('div{width:0.12345px}', '--O1 roundingPrecision:off', { + 'disabled': pipedContext('div{width:0.12345px}', '-O1 roundingPrecision:off', { 'should keep all decimal places': function (error, stdout) { assert.equal(stdout, 'div{width:.12345px}'); } }), - 'disabled via -1': pipedContext('div{width:0.12345px}', '--O1 roundingPrecision:\\\\-1', { + 'disabled via -1': pipedContext('div{width:0.12345px}', '-O1 roundingPrecision:\\\\-1', { 'should keep all decimal places': function (error, stdout) { assert.equal(stdout, 'div{width:.12345px}'); } }), - 'fine-grained': pipedContext('div{height:10.515rem;width:12.12345px}', '--O1 roundingPrecision:rem=2,px=1', { + 'fine-grained': pipedContext('div{height:10.515rem;width:12.12345px}', '-O1 roundingPrecision:rem=2,px=1', { 'should keep all decimal places': function (error, stdout) { assert.equal(stdout, 'div{height:10.52rem;width:12.1px}'); } @@ -440,12 +440,12 @@ vows.describe('./bin/cleancss') }) .addBatch({ 'neighbour merging': { - 'of (yet) unmergeable properties': pipedContext('a{display:inline-block;color:red;display:-moz-block}', '--O2 --skip-aggressive-merging', { + 'of (yet) unmergeable properties': pipedContext('a{display:inline-block;color:red;display:-moz-block}', '-O2 --skip-aggressive-merging', { 'gets right result': function (error, stdout) { assert.equal(stdout, 'a{display:inline-block;color:red;display:-moz-block}'); } }), - 'of mergeable properties': pipedContext('a{background:red;display:block;background:white}', '--O2 --skip-aggressive-merging', { + 'of mergeable properties': pipedContext('a{background:red;display:block;background:white}', '-O2 --skip-aggressive-merging', { 'gets right result': function (error, stdout) { assert.equal(stdout, 'a{background:#fff;display:block}'); } @@ -453,7 +453,7 @@ vows.describe('./bin/cleancss') } }) .addBatch({ - '@media merging': pipedContext('@media screen{a{color:red}}@media screen{a{display:block}}', '--O2 mediaMerging:off', { + '@media merging': pipedContext('@media screen{a{color:red}}@media screen{a{display:block}}', '-O2 mediaMerging:off', { 'gets right result': function (error, stdout) { assert.equal(stdout, '@media screen{a{color:red}}@media screen{a{display:block}}'); } @@ -461,7 +461,7 @@ vows.describe('./bin/cleancss') }) .addBatch({ 'shorthand compacting': { - 'of (yet) unmergeable properties': pipedContext('a{background:url(image.png);background-color:red}', '--O2 shorthandCompacting:off', { + 'of (yet) unmergeable properties': pipedContext('a{background:url(image.png);background-color:red}', '-O2 shorthandCompacting:off', { 'gets right result': function (error, stdout) { assert.equal(stdout, 'a{background:url(image.png);background-color:red}'); } @@ -615,7 +615,7 @@ vows.describe('./bin/cleancss') assert.equal(stdout, '.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}'); } }), - 'enabled': pipedContext('.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}', '--O2 semanticMerging:on', { + 'enabled': pipedContext('.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}', '-O2 semanticMerging:on', { 'should output right data': function (error, stdout) { assert.equal(stdout, '.a,.c{margin:0}.b{margin:10px;padding:0}'); } -- 2.34.1