我正在尝试为cssnano
配置postcss-loader
插件,这非常类似于CSS,如here所述。
Webpack配置:
...
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: () => [
cssnano({
preset: ['default', {
discardComments: {
removeAll: true,
},
// how to find index of all available options?
}]
})
]
}
},
'sass-loader'
]
}
]
},
...
Here列出了cssnano文档中的所有优化内容
here是如何在discardComments
预设之上覆盖单个优化default
的示例。
是否可以像discardComments
那样分别覆盖每个优化配置?在为开发人员和生产人员创建单独的配置时,这可能非常方便。
[另外,在this repo中使用最小的示例和样板也失败了。
please find following steps to creat that
Minimize CSS files with cssnano
wpack.io uses postcss-loader and thereby PostCSS. So we can take advantage of it to minify our CSS/SASS files during production builds.
1
Install and use cssnano
npm i -D cssnano
or with
yarn add --dev cssnano
2
Edit postcss.config.js file
/* eslint-disable global-require, import/no-extraneous-dependencies */
const postcssConfig = {
plugins: [require('autoprefixer')],
};
// If we are in production mode, then add cssnano
if (process.env.NODE_ENV === 'production') {
postcssConfig.plugins.push(
require('cssnano')({
// use the safe preset so that it doesn't
// mutate or remove code from our css
preset: 'default',
})
);
}
module.exports = postcssConfig;
Save it and you are good to go.