Skip to content

Commit

Permalink
Adds --skip-restructuring / restructuring switches.
Browse files Browse the repository at this point in the history
Adds a CLI & API switches to turn off restructuring, which is on
by default.
  • Loading branch information
jakubpawlowicz committed Feb 25, 2015
1 parent 6e047d4 commit 570ba63
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -60,6 +60,7 @@ cleancss [options] source-file, [source-file, ...]
reduction, etc.
--skip-aggressive-merging Disable properties merging based on their order
--skip-media-merging Disable `@media` merging
--skip-restructuring Disable restructuring optimizations
--skip-shorthand-compacting Disable shorthand compacting
--rounding-precision [N] Rounds to `N` decimal places. Defaults to 2. -1 disables rounding.
-c, --compatibility [ie7|ie8] Force compatibility mode (see Readme for advanced examples)
Expand Down Expand Up @@ -123,6 +124,7 @@ CleanCSS constructor accepts a hash as a parameter, i.e.,
* `processImport` - whether to process `@import` rules
* `rebase` - set to false to skip URL rebasing
* `relativeTo` - path to __resolve__ relative `@import` rules and URLs
* `restructuring` - set to false to disable restructuring in advanced optimizations
* `root` - path to __resolve__ absolute `@import` rules and __rebase__ relative URLs
* `roundingPrecision` - rounding precision; defaults to `2`; `-1` disables rounding
* `shorthandCompacting` - set to false to skip shorthand compacting (default is true unless sourceMap is set when it's false)
Expand Down
2 changes: 2 additions & 0 deletions bin/cleancss
Expand Up @@ -26,6 +26,7 @@ commands
.option('--skip-advanced', 'Disable advanced optimizations - selector & property merging, reduction, etc.')
.option('--skip-aggressive-merging', 'Disable properties merging based on their order')
.option('--skip-media-merging', 'Disable @media merging')
.option('--skip-restructuring', 'Disable restructuring optimizations')
.option('--skip-shorthand-compacting', 'Disable shorthand compacting')
.option('--rounding-precision [n]', 'Rounds to `N` decimal places. Defaults to 2. -1 disables rounding.', parseInt)
.option('-c, --compatibility [ie7|ie8]', 'Force compatibility mode (see Readme for advanced examples)')
Expand Down Expand Up @@ -68,6 +69,7 @@ var options = {
mediaMerging: commands.skipMediaMerging ? false : true,
processImport: commands.skipImport ? false : true,
rebase: commands.skipRebase ? false : true,
restructuring: commands.skipRestructuring ? false : true,
root: commands.root,
roundingPrecision: commands.roundingPrecision,
shorthandCompacting: commands.skipShorthandCompacting ? false : true,
Expand Down
1 change: 1 addition & 0 deletions lib/clean.js
Expand Up @@ -39,6 +39,7 @@ var CleanCSS = module.exports = function CleanCSS(options) {
processImport: undefined === options.processImport ? true : !!options.processImport,
rebase: undefined === options.rebase ? true : !!options.rebase,
relativeTo: options.relativeTo,
restructuring: undefined === options.restructuring ? true : !!options.restructuring,
root: options.root,
roundingPrecision: options.roundingPrecision,
shorthandCompacting: !!options.sourceMap ? false : (undefined === options.shorthandCompacting ? true : !!options.shorthandCompacting),
Expand Down
2 changes: 1 addition & 1 deletion lib/selectors/optimizers/advanced.js
Expand Up @@ -718,7 +718,7 @@ AdvancedOptimizer.prototype.optimize = function (tokens) {
self.mergeNonAdjacentBySelector(tokens);
self.mergeNonAdjacentByBody(tokens);

if (withRestructuring) {
if (self.options.restructuring && withRestructuring) {
self.restructure(tokens);
self.mergeAdjacent(tokens);
}
Expand Down
5 changes: 5 additions & 0 deletions test/binary-test.js
Expand Up @@ -130,6 +130,11 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({
assert.equal(stdout, 'a{color:red}p{color:red}');
}
}),
'skip restructuring optimizations': pipedContext('div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}', '--skip-restructuring', {
'should do basic optimizations only': function(error, stdout) {
assert.equal(stdout, 'div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}');
}
}),
'no relative to path': binaryContext('./test/fixtures/partials-absolute/base.css', {
'should not be able to resolve it fully': function(error, stdout, stderr) {
assert.isEmpty(stdout);
Expand Down
14 changes: 14 additions & 0 deletions test/module-test.js
Expand Up @@ -312,6 +312,20 @@ vows.describe('module tests').addBatch({
'gets right output': function (minified) {
assert.include(minified.styles, 'url(/test/fixtures/dummy.png)');
}
},
'restructuring': {
'on': {
'topic': new CleanCSS({ restructuring: true }).minify('div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}'),
'gets right output': function (minified) {
assert.equal(minified.styles, '.two,div{margin-top:0}.one{margin:0}.two{display:block}');
}
},
'off': {
'topic': new CleanCSS({ restructuring: false }).minify('div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}'),
'gets right output': function (minified) {
assert.equal(minified.styles, 'div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}');
}
}
}
},
'source map': {
Expand Down
1 change: 1 addition & 0 deletions test/selectors/optimizer-test.js
Expand Up @@ -10,6 +10,7 @@ function optimizerContext(group, specs, options) {
var context = {};
options = options || {};
options.shorthandCompacting = true;
options.restructuring = true;
options.compatibility = new Compatibility(options.compatibility).toOptions();

function optimized(target) {
Expand Down

0 comments on commit 570ba63

Please sign in to comment.