简体   繁体   English

Cssnano不会删除重复项

[英]Cssnano doesn't remove duplicates

I have a React web application where i use CSS modules. 我有一个React Web应用程序,我使用CSS模块。 I am using Webpack to build the application. 我正在使用Webpack来构建应用程序。

Related part from the webpack.config.js : webpack.config.js中的相关部分:

  {
    test: /\.css$/,
    use: extractCSS.extract(
    {
      fallback: 'style-loader',
      use: [{
          loader: 'css-loader',
          options: {
            modules: true,
            importLoaders: 1,
            localIdentName: '[name]__[local]___[hash:base64:5]',
          },
        },
        {
          loader: 'postcss-loader',
          options: {
            ident: 'postcss',
            plugins: () => [
              require('autoprefixer')({}),
              require('postcss-discard-empty')({}),
              require('postcss-discard-comments')({}),
              require('postcss-color-function')({}),
              require('postcss-flexbugs-fixes')({}),
              require('cssnano')({
                preset: ['default', {
                  discardComments: {
                    removeAll: true,
                  },
                  colormin: false,
                  cssDeclarationSorter: false,
                }],
              }),
            ],
          },
        },
      ],
    }),
  },

The minification seems to work. 缩小似乎有效。 I could find some duplicate CSS rules present in the CSS like below 我可以在CSS中找到一些重复的CSS规则,如下所示

CSS

I am wondering whether this is a problem with my configuration or the tools I am using (PostCss and CssNano) 我想知道这是我的配置或我正在使用的工具的问题(PostCss和CssNano)

Thanks you. 谢谢。

It happens because you have CSS minifier (cssnano) is postcss-loader . 这是因为你有CSS minifier(cssnano)是postcss-loader Loaders are applied to every file separated, as result cssnano doesn't see other files to remove duplicates across files. 加载器应用于每个分隔的文件,因此cssnano没有看到其他文件来删除文件中的重复项。

Use mini-css-extract-plugin . 使用mini-css-extract-plugin This plugin use cssnano as well. 这个插件也使用cssnano。 But it calls cssnano after concatenating every file to one big bundle. 但它在将每个文件连接到一个大包之后调用cssnano。 As result, cssnano will see all CSS together and will be able to remove duplicates across the whole bundle. 因此,cssnano将一起看到所有CSS,并且能够删除整个包中的重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM