Skip to content

Commit

Permalink
Add safe option
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Sep 20, 2015
1 parent 41a01ca commit 3c758db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
52 changes: 36 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var decamelize = require('decamelize');
var defined = require('defined');
var assign = require('object-assign');
var postcss = require('postcss');
var warnOnce = require('./lib/warnOnce');

Expand Down Expand Up @@ -39,13 +40,30 @@ var processors = {
styleCache: require('./lib/styleCache')
};

var defaultOptions = {
autoprefixer: {
add: false
}
};

var safeOptions = {
postcssConvertValues: {
length: false
},
postcssReduceIdents: {
counterStyle: false,
keyframes: false
},
postcssZindex: {
disable: true
}
};

var cssnano = postcss.plugin('cssnano', function (options) {
options = options || {};

var safe = options.safe === true;
var proc = postcss();
var plugins = Object.keys(processors);
var len = plugins.length;
var i = 0;

if (typeof options.fontFamily !== 'undefined' || typeof options.minifyFontWeight !== 'undefined') {
warnOnce('The fontFamily & minifyFontWeight options have been ' +
Expand All @@ -55,31 +73,33 @@ var cssnano = postcss.plugin('cssnano', function (options) {
}
}

while (i < len) {
var plugin = plugins[i++];
var processor = processors[plugin];
var method = processor;

Object.keys(processors).forEach(function (plugin) {
var shortName = plugin.replace('postcss', '');
shortName = shortName.slice(0, 1).toLowerCase() + shortName.slice(1);

var opts = defined(
options[shortName],
options[plugin],
options[decamelize(plugin, '-')],
{}
options[decamelize(plugin, '-')]
);

if (opts === false || opts.disable) {
continue;
if (opts === false) {
opts = {
disable: true
};
}

if (plugin === 'autoprefixer') {
opts.add = false;
opts = assign({},
defaultOptions[plugin],
safe ? safeOptions[plugin] : null,
opts
);

if (!opts.disable) {
proc.use(processors[plugin](opts));
}

proc.use(method(opts));
}
});

return proc;
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"decamelize": "^1.0.0",
"defined": "^1.0.0",
"indexes-of": "^1.0.1",
"object-assign": "^4.0.1",
"postcss": "^5.0.5",
"postcss-calc": "^5.0.0",
"postcss-colormin": "^2.1.1",
Expand Down

0 comments on commit 3c758db

Please sign in to comment.