如何为CSS缩小配置cssnano?

问题描述 投票:0回答:1

我正在尝试为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列出了cssn​​ano文档中的所有优化内容

here是如何在discardComments预设之上覆盖单个优化default的示例。

是否可以像discardComments那样分别覆盖每个优化配置?在为开发人员和生产人员创建单独的配置时,这可能非常方便。

[另外,在this repo中使用最小的示例和样板也失败了。

css webpack minify postcss-loader cssnano
1个回答
0
投票
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.
© www.soinside.com 2019 - 2024. All rights reserved.