webpack with less and postcss autoprefixer

后端 未结 2 1560
鱼传尺愫
鱼传尺愫 2021-01-07 03:19

I\'m new to webpack and I\'m trying to set everything up to work on a react project. I have managed to get everything working in webpack as expected but have hit a road bloc

2条回答
  •  北海茫月
    2021-01-07 04:04

    I struggled on the same kind of problem and found a solution. I also went through the process of applying autoprefixer which didn't work and didn't display any error message either.

    I don't know if my solution will solve your problem, but let's give it a try. I tried to transpile scss code, which basically is the same process as transpiling less code.

    I defined neither a postcss.config.js nor a .browserlistrc file. Instead, in the webpack.config.js, I first

    const autoprefixer = require('autoprefixer');
    

    Then, I set the following configuration in the webpack.config.js (this here would be the equivalent for less):

    module: {
      rules: [
        // ... other rules ... 
        { 
          test: /\.less$/,
          loader: ExtractTextPlugin.extract(['css-loader',
          {
            loader: 'postcss-loader',
            options: {
              plugins: () => autoprefixer({
                browsers: ['last 3 versions', '> 1%']
              })
            }
          }, 'less-loader'])
        }
      // ... other rules ...
      ]
    }, 
    plugins: [
       // ... other plugins ...
       new ExtractTextPlugin({
        filename: '[name].css',
        allChunks: true,
      })
      // ... other plugins ...
    ]
    

    As far as I understand from the huge amount of forums I looked for that piece of information, it is crucial that the loader order be 'css-loader', 'postcss-loader', and then 'less-loader'. In my particular case, it didn't work at all without the option

    {
      browsers: ['last 3 versions', '> 1%']
    }    
    

    Hope this helps...

提交回复
热议问题