Using autoprefixer with postcss in webpack 2.x

前端 未结 4 1432
误落风尘
误落风尘 2021-02-07 01:15

How to use autoprefixer with webpack 2.x.

Previously, it used to be something like this...

...

module: {
  loaders: [
     {
       test:          


        
4条回答
  •  难免孤独
    2021-02-07 01:51

    Webpack 2.x.x is a killer and a build breaker

    webpack 2.x.x introduced webpack.LoaderOptionsPlugin() plugin where you need to define all the loader option plugins. Like, autoprefixer is a plugin for postcss-loader. So, it has to go here.

    And

    • module.rules replaces module.loaders
    • All the loaders should have explicitly say that they are a loader. Ex. loader: 'style!css' should be loader: 'style-loader!css-loader'

    The new config will look something like this...

    ...
    
    module: {
      rules: [
         {
           test: /\.scss$/,
           loaders: ['style-loader', 'css-loader', 'sass-loader', 'postcss-loader']
         }
       ]
    },
    
    plugins: [
      new webpack.LoaderOptionsPlugin({
        options: {
          postcss: [
            autoprefixer(),
          ]
         }
      })
    ],
    
    ...
    

    Hope this helps everyone.

提交回复
热议问题