0

I'm trying to add autoprefixer to webpack 2.2.1 and having issues seeing the prefixes. I installed postcss-loader https://github.com/postcss/postcss-loader as this seems to be the listed way to handle postcss in webpack.

I'm currently using scss files which I'm importing into my react files. example: import styles from '../../styles/header.scss'; This is handled in webpack using sass-loader.

I'm not getting any error with my setup but I'm also not seeing any autopre-fixing going on to my files ? I presume this only needs to be added in development not production ?

Here is my dev setup webpack config.

const path = require('path')
const webpack = require('webpack')

const ROOT_DIR = path.resolve(__dirname, '../app')

    module.exports = {
      devtool: 'eval',

      entry: [
        `${ROOT_DIR}/js/index`,
        'webpack-hot-middleware/client'
      ],

      output: {
        path: path.resolve(__dirname, '../public'),
        filename: 'bundle.js',
        publicPath: '/public/'
      },

      plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.DefinePlugin({
          "config.ASSET_URL": JSON.stringify(process.env.ASSETS_URL),
          "config.GA_TRACKING_ID": JSON.stringify(process.env.GA_TRACKING_ID)
        })
      ],
      module: {
        loaders: [
          { test: /\.js?$/,
            loader: 'babel-loader',
            include: path.join(__dirname, '../app'),
            exclude: /node_modules/
          },
          { test: /\.scss?$/,
            include: path.join(__dirname, '../app', 'styles'),
            use: [
               'style-loader',
               'css-loader',
               {
                 loader: 'postcss-loader',
                 options: { plugins: [
                     require('autoprefixer')
                 ] }
               },
               {
                 loader: 'sass-loader',
                 options: {
                   data: "$assetPath: '" + process.env.ASSETS_URL + "';"
                 }
               }
             ]
          },
          {
            test: /\.(jpe?g|png|gif|svg)$/i,
            include : path.join(__dirname, '../app', 'images'),
            loader  : 'file-loader?limit=30000&name=[name].[ext]'
          },
          {
            test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
            include : path.join(__dirname, '../app', 'fonts'),
            loader: 'file-loader?name=fonts/[name].[ext]'
          }
        ]
      }
    }
4

1 回答 1

0

我最近有一个类似的事情要处理。如果您在 scss 文件的目录中创建一个单独的 postcss.config.js 文件并在其中包含它,请检查它是否有效

//postcss.config.js
module.exports = {
    plugins: [
        require('autoprefixer')
    ]
}

在你的 webpack 配置中

  { test: /\.scss?$/,
    include: path.join(__dirname, '../app', 'styles'),
    use: [
       {loader: "style-loader"},
       {loader: "css-loader"},
       {loader: "postcss-loader"},
       {loader: "sass-loader",
         options: {
           data: "$assetPath: '" + process.env.ASSETS_URL + "';"
         }
       }
     ]
  },
于 2017-12-12T07:40:34.747 回答