|
| 1 | +var decamelize = require('decamelize'); |
| 2 | +var defined = require('defined'); |
| 3 | +var assign = require('object-assign'); |
| 4 | +var postcss = require('postcss'); |
| 5 | + |
| 6 | +// Processors |
| 7 | +var postcssFilterPlugins = require('postcss-filter-plugins'); |
| 8 | +var postcssDiscardComments = require('postcss-discard-comments'); |
| 9 | +var postcssReduceInitial = require('postcss-reduce-initial'); |
| 10 | +var postcssMinifyGradients = require('postcss-minify-gradients'); |
| 11 | +var postcssSvgo = require('postcss-svgo'); |
| 12 | +var postcssReduceTransforms = require('postcss-reduce-transforms'); |
| 13 | +var autoprefixer = require('autoprefixer'); |
| 14 | +var postcssZindex = require('postcss-zindex'); |
| 15 | +var postcssConvertValues = require('postcss-convert-values'); |
| 16 | +var postcssCalc = require('postcss-calc'); |
| 17 | +var postcssColormin = require('postcss-colormin'); |
| 18 | +var postcssOrderedValues = require('postcss-ordered-values'); |
| 19 | +var postcssMinifySelectors = require('postcss-minify-selectors'); |
| 20 | +var postcssMinifyParams = require('postcss-minify-params'); |
| 21 | +var postcssNormalizeCharset = require('postcss-normalize-charset'); |
| 22 | +var postcssMinifyFontValues = require('postcss-minify-font-values'); |
| 23 | +var postcssDiscardUnused = require('postcss-discard-unused'); |
| 24 | +var postcssNormalizeUrl = require('postcss-normalize-url'); |
| 25 | +var postcssMergeIdents = require('postcss-merge-idents'); |
| 26 | +var postcssReduceIdents = require('postcss-reduce-idents'); |
| 27 | +var postcssMergeLonghand = require('postcss-merge-longhand'); |
| 28 | +var postcssDiscardDuplicates = require('postcss-discard-duplicates'); |
| 29 | +var postcssDiscardOverridden = require('postcss-discard-overridden'); |
| 30 | +var postcssMergeRules = require('postcss-merge-rules'); |
| 31 | +var postcssDiscardEmpty = require('postcss-discard-empty'); |
| 32 | +var postcssUniqueSelectors = require('postcss-unique-selectors'); |
| 33 | + |
| 34 | + |
| 35 | +var functionOptimiser = require('cssnano/dist/lib/functionOptimiser'); |
| 36 | +var filterOptimiser = require('cssnano/dist/lib/filterOptimiser'); |
| 37 | +var reduceBackgroundRepeat = require('cssnano/dist/lib/reduceBackgroundRepeat'); |
| 38 | +var reducePositions = require('cssnano/dist/lib/reducePositions'); |
| 39 | +var core = require('cssnano/dist/lib/core'); |
| 40 | +var reduceTimingFunctions = require('cssnano/dist/lib/reduceTimingFunctions'); |
| 41 | +var styleCache = require('cssnano/dist/lib/styleCache'); |
| 42 | + |
| 43 | +/** |
| 44 | + * Deprecation warnings |
| 45 | + */ |
| 46 | + |
| 47 | +var warnOnce = require('cssnano/dist/lib/warnOnce'); |
| 48 | + |
| 49 | +var processors = { |
| 50 | + postcssFilterPlugins: function() { return postcssFilterPlugins({silent: true}) }, |
| 51 | + postcssDiscardComments: postcssDiscardComments, |
| 52 | + postcssMinifyGradients: postcssMinifyGradients, |
| 53 | + postcssReduceInitial: postcssReduceInitial, |
| 54 | + postcssSvgo: postcssSvgo, |
| 55 | + postcssReduceTransforms: postcssReduceTransforms, |
| 56 | + autoprefixer: autoprefixer, |
| 57 | + postcssZindex: postcssZindex, |
| 58 | + postcssConvertValues: postcssConvertValues, |
| 59 | + reduceTimingFunctions: reduceTimingFunctions, |
| 60 | + postcssCalc: postcssCalc, |
| 61 | + postcssColormin: postcssColormin, |
| 62 | + postcssOrderedValues: postcssOrderedValues, |
| 63 | + postcssMinifySelectors: postcssMinifySelectors, |
| 64 | + postcssMinifyParams: postcssMinifyParams, |
| 65 | + postcssNormalizeCharset: postcssNormalizeCharset, |
| 66 | + postcssDiscardOverridden: postcssDiscardOverridden, |
| 67 | + // minify-font-values should be run before discard-unused |
| 68 | + postcssMinifyFontValues: postcssMinifyFontValues, |
| 69 | + postcssDiscardUnused: postcssDiscardUnused, |
| 70 | + postcssNormalizeUrl: postcssNormalizeUrl, |
| 71 | + functionOptimiser: functionOptimiser, |
| 72 | + filterOptimiser: filterOptimiser, |
| 73 | + reduceBackgroundRepeat: reduceBackgroundRepeat, |
| 74 | + reducePositions: reducePositions, |
| 75 | + core: core, |
| 76 | + // Optimisations after this are sensitive to previous optimisations in |
| 77 | + // the pipe, such as whitespace normalising/selector re-ordering |
| 78 | + postcssMergeIdents: postcssMergeIdents, |
| 79 | + postcssReduceIdents: postcssReduceIdents, |
| 80 | + postcssMergeLonghand: postcssMergeLonghand, |
| 81 | + postcssDiscardDuplicates: postcssDiscardDuplicates, |
| 82 | + postcssMergeRules: postcssMergeRules, |
| 83 | + postcssDiscardEmpty: postcssDiscardEmpty, |
| 84 | + postcssUniqueSelectors: postcssUniqueSelectors, |
| 85 | + styleCache: styleCache, |
| 86 | +}; |
| 87 | + |
| 88 | +var defaultOptions = { |
| 89 | + autoprefixer: { |
| 90 | + add: false, |
| 91 | + }, |
| 92 | + postcssConvertValues: { |
| 93 | + length: false, |
| 94 | + }, |
| 95 | + postcssNormalizeCharset: { |
| 96 | + add: false, |
| 97 | + }, |
| 98 | +}; |
| 99 | + |
| 100 | +var safeOptions = { |
| 101 | + postcssConvertValues: { |
| 102 | + length: false, |
| 103 | + }, |
| 104 | + postcssDiscardUnused: { |
| 105 | + disable: true, |
| 106 | + }, |
| 107 | + postcssMergeIdents: { |
| 108 | + disable: true, |
| 109 | + }, |
| 110 | + postcssReduceIdents: { |
| 111 | + counterStyle: false, |
| 112 | + keyframes: false, |
| 113 | + }, |
| 114 | + postcssNormalizeUrl: { |
| 115 | + stripWWW: false, |
| 116 | + }, |
| 117 | + postcssZindex: { |
| 118 | + disable: true, |
| 119 | + }, |
| 120 | +}; |
| 121 | + |
| 122 | +var nanoPluginHandler = function nanoPluginHandler (options) { |
| 123 | + if (typeof options === 'undefined') options = {} |
| 124 | + |
| 125 | + // Prevent PostCSS from throwing when safe is defined |
| 126 | + if (options.safe === true) { |
| 127 | + options.isSafe = true; |
| 128 | + options.safe = null; |
| 129 | + } |
| 130 | + |
| 131 | + var safe = options.isSafe; |
| 132 | + var proc = postcss(); |
| 133 | + |
| 134 | + if (typeof options.fontFamily !== 'undefined' || typeof options.minifyFontWeight !== 'undefined') { |
| 135 | + warnOnce('The fontFamily & minifyFontWeight options have been ' + |
| 136 | + 'consolidated into minifyFontValues, and are now deprecated.'); |
| 137 | + if (!options.minifyFontValues) { |
| 138 | + options.minifyFontValues = options.fontFamily; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (typeof options.singleCharset !== 'undefined') { |
| 143 | + warnOnce('The singleCharset option has been renamed to ' + |
| 144 | + 'normalizeCharset, and is now deprecated.'); |
| 145 | + options.normalizeCharset = options.singleCharset; |
| 146 | + } |
| 147 | + |
| 148 | + Object.keys(processors).forEach(function(plugin) { |
| 149 | + var shortName = plugin.replace('postcss', ''); |
| 150 | + shortName = shortName.slice(0, 1).toLowerCase() + shortName.slice(1); |
| 151 | + |
| 152 | + var opts = defined( |
| 153 | + options[shortName], |
| 154 | + options[plugin], |
| 155 | + options[decamelize(plugin, '-')] |
| 156 | + ); |
| 157 | + |
| 158 | + if (opts === false) { |
| 159 | + opts = {disable: true}; |
| 160 | + } |
| 161 | + |
| 162 | + opts = assign({}, |
| 163 | + defaultOptions[plugin], |
| 164 | + safe ? safeOptions[plugin] : null, |
| 165 | + opts |
| 166 | + ); |
| 167 | + |
| 168 | + if (!opts.disable) { |
| 169 | + proc.use(processors[plugin](opts)); |
| 170 | + } |
| 171 | + |
| 172 | + }); |
| 173 | + |
| 174 | + return proc; |
| 175 | +} |
| 176 | + |
| 177 | +var cssnano = postcss.plugin('cssnano', nanoPluginHandler) |
| 178 | + |
| 179 | + |
| 180 | +cssnano.process = function (css, options) { |
| 181 | + if (typeof options === 'undefined') options = {} |
| 182 | + options.map = options.map || (options.sourcemap ? true : null); |
| 183 | + return postcss([cssnano(options)]).process(css, options); |
| 184 | +}; |
| 185 | + |
| 186 | +module.exports = cssnano; |
0 commit comments