Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not getting autoprefixer to work with extract-text-webpack-plugin #81

Closed
badtant opened this issue Jul 4, 2016 · 17 comments
Closed

Not getting autoprefixer to work with extract-text-webpack-plugin #81

badtant opened this issue Jul 4, 2016 · 17 comments

Comments

@badtant
Copy link

badtant commented Jul 4, 2016

Hi,

I have a working dev config for my scss like this:

var autoprefixer = require('autoprefixer');

  module: {
    loaders: [
      {
        test: /\.scss$/,
        loaders: ['style-loader', 'css-loader?sourceMap', 'postcss-loader', 'sass-loader?sourceMap']
      }
    ]
  },
  sassLoader: {
    includePaths: ['node_modules/normalize.css'],
    precision: '8',
    outputStyle: 'nested'
  },
  postcss: [ autoprefixer({ browsers: ['last 10 versions'] }) ]

Now I wanted to extract the css to a file when I build for production. I get a file with the config below. The problem is that the options for postcss (for instance autoprefixer) doesn't get applied.

var autoprefixer = require('autoprefixer');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var extractCSS = new ExtractTextPlugin('bundle.css');

  module: {
    loaders: [
      {
        test: /\.scss$/,
        loader: extractCSS.extract('style-loader', 'css-loader!postcss-loader!sass-loader')
      }
    ]
  },
  sassLoader: {
    includePaths: ['node_modules/normalize.css'],
    precision: '8',
    outputStyle: 'compressed'
  },
  postcss: [ autoprefixer({ browsers: ['last 10 versions'] }) ],
  plugins: [
    extractCSS
  ]
};

Can anyone see what I'm doing wrong?
Thanks

@ai
Copy link
Contributor

ai commented Jul 4, 2016

Seems like it is problem of css-loader CSS minifier. Please check Autoprefixer FAQ for details (sorry I am in the train in the middle of Kazakhstan) - you should move browser requirements to browserslist config.

Also last 10 versions is very lame config. I am sure, that you don't care about IE 3 or old Chrome 😉. Be responsible developer, talk with your client and set actual browser requirements (it should be equal to browsers where you test your website).

@ai ai closed this as completed Jul 4, 2016
@badtant
Copy link
Author

badtant commented Jul 4, 2016

I'm not sure what you mean :)
First you point me to css-loader and then to autoprefixer?

And yes, thats just a test config with the last 10 browsers so that I can see if it's working or not.

@ai
Copy link
Contributor

ai commented Jul 4, 2016

Please check Autoprefixer FAQ section in Autoprefixer README. It contains question "No prefixes in production", answer will contain explanation and solution.

Sorry, I can't give you a direct link.

@badtant
Copy link
Author

badtant commented Jul 4, 2016

Ahh, now I see. Thanks!

@JounQin
Copy link

JounQin commented Jul 26, 2016

@badtant You should disable the Minification option of css-loader with css?-minimize.

minification

@donpinkus
Copy link

@badtant - would love to see an example of your final working config

@ai
Copy link
Contributor

ai commented Oct 20, 2016

Webpack fixed a Autoprefixer issue, so we could just remove this section!

@badtant
Copy link
Author

badtant commented Oct 21, 2016

@donpinkus right now:
extractTextInstance.extract('style-loader', ['css-loader?-autoprefixer', 'postcss-loader')

@hugojing
Copy link

hugojing commented Nov 10, 2016

Thanks @JounQin and @badtant ! Both works!

    loader: ExtractTextPlugin.extract(
      'style-loader',
      'css-loader?-autoprefixer!postcss-loader'
    )

or

    loader: ExtractTextPlugin.extract(
      'style-loader',
      'css-loader?-minimize!postcss-loader'
    )

@ai
Copy link
Contributor

ai commented Nov 10, 2016

@hugojing please write +1 in this issue webpack-contrib/css-loader#281

This is very important

@szykov
Copy link

szykov commented Feb 9, 2017

Tried everything but still nothing. :

 module: {
        rules: [
            {
                test: /\.css$/,
                exclude: path.join(__dirname, '/angular2App/app/'),
                use: ExtractTextPlugin.extract({
                    fallback: ['style-loader'],
                    use: ['css-loader?-autoprefixer&sourceMap=true&importLoaders=1', 'postcss-loader']
                })
            },
       ...
      ]

postcss.config.js

module.exports = {
  plugins: [
    require('autoprefixer')
  ]
}

package.json

  "browserslist": [
    "> 1%",
    "last 2 versions"
  ],

No change, it doesn't add prefixes....

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Feb 9, 2017

@szykov

Just for getting started, but could you please use try using webpack 2 loader syntax inside ExtractTextPlugin.extract() ?

webpack.config.js

ExtractTextPlugin.extract({
   fallback: 'style-loader',
   use: [ 
     { 
       loader: 'css-loader', 
       options : { autoprefixer: false, sourceMap: true, importLoaders: 1 } 
     }, 
     'postcss-loader'
   ]
 })

postcss.config.js

plugins: [
- require('autoprefixer')
+ require('autoprefixer')()
]

@szykov
Copy link

szykov commented Feb 10, 2017

@michael-ciniawsky it worked, thanks. 👍

@cordoval
Copy link

cordoval commented Jul 4, 2017

@michael-ciniawsky is there a way to disappear that postcss.config.js and push it into the webpack.config.js file? sorry to abuse you on this one 👴

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Jul 4, 2017

webpack.config.js

ExtractTextPlugin.extract({
   fallback: 'style-loader',
   use: [ 
     { 
       loader: 'css-loader', 
       options: { importLoaders: 1 } 
     }, 
     { 
        loader: 'postcss-loader', 
        options: {
          ident: 'postcss',
          plugins: () => [ require('autoprefixer')({...options}) ]
        }
     }
   ]
 })

@cordoval
Copy link

cordoval commented Jul 4, 2017

👍

@rouberg
Copy link

rouberg commented Nov 16, 2018

I find the real reason of no prefix, it's not the problem of extract-text-webpack-plugin. when build we use optimize-css-assets-webpack-plugin normally, optimize-css-assets-webpack-plugin use cssnano as default processor, and cssnano removed all prefix, so autoprefixer seemed not work in production.
we can config like this:

new OptimizeCssAssetsPlugin({
    cssProcessorOptions: {
      safe: true,
      autoprefixer: { disable: true },
      discardComments: {
        removeAll: true
      }
    }
  });

the option of autoprefixer: { disable: true } will disable cssnano's autoprefixer and saved prefixer that already exist by ourselves.

also see the issue optimize-css-assets-webpack-plugin/issues/51

maybe some brother need this so I copy and attach this although this issue closed a long time ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants