Postcss-loader Autoprefixer 不适用于 Webpack 3

Postcss-loader Autoprefixer not working with Webpack 3

我正在尝试使用一系列加载器将所有 SCSS 文件提取到一个 ExtractTextPlugin 文件中。在我拥有的加载程序系列中,我使用的是 postcss-loader 中使用的 autoprefixer 插件。但是,似乎没有任何前缀!

我的webpack配置如下:

webpack.config.js

const path = require('path');
const autoprefixer = require('autoprefixer');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const config = {
  context: __dirname,
  entry: {
    app: [
      path.resolve(entrypath),
    ],
  },
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            'css-loader',
            'sass-loader',
            {
              loader: 'postcss-loader',
              options: {
                plugins: () => [autoprefixer()]
              }
            }
          ]
        }),
      },
    ],
  },
  output: {
    path: path.join(__dirname, '../app/assets/javascripts/generated/'),
  },
  plugins: [
    new ExtractTextPlugin({
      filename: 'compiled-draft-document-scss.css',
      allChunks: true
    }),
  ],
};

module.exports = config;

除了添加了 none 个前缀外,我的输出似乎一切正常。以下是我使用的包的相关版本:

"extract-text-webpack-plugin": "3.0.1",
"webpack": "^3.1.0",
"style-loader": "^0.19.0",
"css-loader": "^0.23.1",
"moment": "^2.11.1",
"postcss-loader": "^0.10.1",
"sass-loader": "^6.0.6",

来自 postcss-loader README:

Use it after css-loader and style-loader, but before other preprocessor loaders like e.g. sass|less|stylus-loader.

所以你的加载程序链应该是:

[
  'css-loader',
  {
    loader: 'postcss-loader',
    options: {
      plugins: () => [autoprefixer()]
    }
  },
  'sass-loader'    
]

在您的 package.json 文件中,添加

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