Css-loader: How can i set the discardComments.removeAll config option for cssnano?

Created on 21 Sep 2016  ·  6Comments  ·  Source: webpack-contrib/css-loader

cssnano supports the following config option:

{
    discardComments: {
        removeAll: true
    }
}

How can i pass this config option through css-loader to cssnano?

http://cssnano.co/optimisations/

Most helpful comment

I figured this out through trial and error because it doesn't seem to be documented at all. Basically, the options given to cssnano start off as simply the query that you set for the loader, so this worked for me:

{
  test: /\.css$/,
  loader: 'css?{discardComments:{removeAll:true}}'
}

All 6 comments

I figured this out through trial and error because it doesn't seem to be documented at all. Basically, the options given to cssnano start off as simply the query that you set for the loader, so this worked for me:

{
  test: /\.css$/,
  loader: 'css?{discardComments:{removeAll:true}}'
}

@CookPete

It worked in Webpack@1, but not work in [email protected]

My code:

{
  test: /\.styl$/,
  loader: 'style!css?{discardComments:{removeAll:true}}!stylus'
}

@viko16 I don't know how webpack 2 deals with loader queries now, but it may be worth trying the actual query config option rather than inlining the JSON structure in the loader string? More info in https://github.com/webpack/webpack/issues/482

For your example, something like this (complete guess):

module: {
  loaders: [{
    test: /\.styl$/,
    loaders: [
      {
        loader: 'style'
      },
      {
        loader: 'css',
        query: {
          discardComments: {
            removeAll: true
          }
        }
      },
      {
        loader: 'stylus'
      }
    ]
  }]
}

OMG, I found that I just forgot to add parameters '-p': 😂

# before
cross-env NODE_ENV=production webpack
# after √
cross-env NODE_ENV=production webpack -p

Then everything is all right!!!!!!

*_On the other hand, both "inline query string" and "JSON structure" are effective. Thank you for your help. :+1: @CookPete *_

Awesome, works like a charm 👍

What if I'm using webpack's js API what means I can't pass the param -p?

Was this page helpful?
0 / 5 - 0 ratings