Skip to content

Commit 3ccbb24

Browse files
committed
Add minify option
Uses cssnano to minify the output
1 parent bbb0078 commit 3ccbb24

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

bin/suitcss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var writeFileSync = fs.outputFileSync;
1818
program
1919
.version(require('../package.json').version)
2020
.usage('[<input>] [<output>]')
21+
.option('-m, --minify', 'minify output with cssnano')
2122
.option('-i, --import-root [path]', 'the root directory for imported css files', '')
2223
.option('-c, --config [path]', 'a custom PostCSS config file', '')
2324
.option('-v, --verbose', 'log verbose output for debugging')
@@ -107,7 +108,7 @@ function run () {
107108
var css = buffer.toString();
108109

109110
suitcss(css, {
110-
compress: program.compress,
111+
minify: program.minify,
111112
root: program.importRoot,
112113
config: config
113114
}).then(function(result) {

lib/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var last = require('lodash/array/last');
77
var autoprefixer = require('autoprefixer');
88
var bemLinter = require('postcss-bem-linter');
99
var postcss = require('postcss');
10+
var cssnano = require('cssnano');
1011
var reporter = require('postcss-reporter');
1112

1213
/**
@@ -16,6 +17,7 @@ var reporter = require('postcss-reporter');
1617
module.exports = preprocessor;
1718

1819
var defaults = {
20+
minify: undefined,
1921
use: [
2022
'postcss-import',
2123
'postcss-custom-properties',
@@ -38,6 +40,17 @@ var defaults = {
3840
},
3941
'postcss-reporter': {
4042
clearMessages: true
43+
},
44+
// http://cssnano.co/optimisations/
45+
cssnano: {
46+
calc: false,
47+
autoprefixer: false,
48+
mergeRules: false,
49+
// Disable unsafe optimisations
50+
zindex: false,
51+
discardUnused: false,
52+
reduceIdents: false,
53+
mergeIdents: false
4154
}
4255
};
4356

@@ -63,9 +76,13 @@ function preprocessor(css, options) {
6376
return settings ? plugin(settings) : plugin;
6477
});
6578

66-
var processor = postcss(plugins).process(css);
79+
var processor = postcss(plugins);
80+
81+
if (options.minify) {
82+
processor.use(cssnano(options.cssnano));
83+
}
6784

68-
return processor;
85+
return processor.process(css);
6986
}
7087

7188

@@ -80,6 +97,8 @@ function mergeOptions(options) {
8097
options.config = options.config || {};
8198

8299
var merged = assign({}, defaults, options.config);
100+
101+
merged.minify = options.minify;
83102
merged['postcss-import'].root = options.root;
84103

85104
// Ensure postcss-reporter is always the final plugin

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"colors": "~1.1.2",
1717
"commander": "~2.9.0",
1818
"fs-extra": "^0.26.2",
19+
"cssnano": "^3.3.2",
1920
"lodash": "^3.10.1",
2021
"object-assign-deep": "0.0.4",
2122
"pad-component": "0.0.1",

test/fixtures/minify.out.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.u-img{border-radius:50%}.Component{font-size:16px;width:16.66667%}.Component-item{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex}@media (min-width:200px){.Component-item{color:red}}

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ describe('suitcss', function () {
4242
expect(opts['postcss-import'].root).to.equal('test/root');
4343
});
4444

45+
it('should allow an minify option to be set', function() {
46+
var opts = mergeOptions({minify: true});
47+
expect(opts.minify).to.be.true;
48+
});
49+
4550
it('should merge config options with existing defaults', function() {
4651
var autoprefixer = {browsers: ['> 1%', 'IE 7'], cascade: false};
4752
var opts = mergeOptions({
@@ -142,6 +147,16 @@ describe('cli', function () {
142147
});
143148
});
144149

150+
it('should minify the output', function (done) {
151+
exec('bin/suitcss -i test/fixtures test/fixtures/import.css test/fixtures/cli/output.css -m', function (err, stdout) {
152+
if (err) return done(err);
153+
var res = read('fixtures/cli/output');
154+
var expected = read('fixtures/minify.out');
155+
expect(res).to.equal(expected);
156+
done();
157+
});
158+
});
159+
145160
it('should allow a config file to be passed', function (done) {
146161
exec('bin/suitcss -i test/fixtures -c test/test.config.js test/fixtures/config.css test/fixtures/cli/output.css', function (err, stdout) {
147162
if (err) return done(err);

0 commit comments

Comments
 (0)