提取文本插件 'loader is used without the corresponding plugin' - React,Sass,Webpack

Extract Text Plugin 'loader is used without the corresponding plugin' - React, Sass, Webpack

我正在尝试对我的 React + Webpack 项目进行 SASS 编译 运行 并保持 运行 进入此错误:

Module build failed: Error: "extract-text-webpack-plugin" loader is used without the corresponding plugin

遵循本教程中的指南:Link

这是我的webpack.config.js

有什么建议吗?

const debug = process.env.NODE_ENV !== "production";
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  context: path.join(__dirname, "src"),
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./js/client.js",
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0'],
          plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
        }
      },
      {
          test: /\.scss$/,
          loader: ExtractTextPlugin.extract('css!sass')
      }
    ]
  },
  output: {
    path: __dirname + "/src/",
    filename: "client.min.js"
  },
  plugins: debug ? [] : [
    new ExtractTextPlugin('dist/styles/main.css', {
        allChunks: true
    }),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
  ],
};

所以我通过安装 style-loader、css-loader 和 sass-loader 解决了这个问题。

npm install style-loader css-loader sass-loader --save-dev

然后我就这样用了

{
  test: /\.scss$/,
  loader: ExtractTextPlugin.extract("style-loader","css-loader!sass-loader"),
 },

当您的 NODE_ENV 不是 production 时,您不包含任何插件。

plugins: debug ? [] : [
  new ExtractTextPlugin('dist/styles/main.css', {
    allChunks: true
  }),
  new webpack.optimize.DedupePlugin(),
  new webpack.optimize.OccurenceOrderPlugin(),
  new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],

所以当 debug 为真时,您将有

plugins: []

但您仍在 .scss 加载程序中使用它。

为了解决这个问题,你也可以将它添加到这些插件中(所以你没有像UglifyJsPlugin这样的生产插件):

plugins: debug
  ? [
      new ExtractTextPlugin("dist/styles/main.css", {
        allChunks: true
      })
    ]
  : [
      new ExtractTextPlugin("dist/styles/main.css", {
        allChunks: true
      }),
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.OccurenceOrderPlugin(),
      new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false })
    ]

或者您不在 .scss 加载程序中使用 ExtractTextPlugin(这不会将它们提取到开发中的 css 文件):

{
  test: /\.scss$/,
  loader: debug ? 'css!sass' : ExtractTextPlugin.extract('css!sass')
}

由于参数格式不正确,我发生了错误 - 参考这个-> https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/207

我的代码是这样的 module: { rules: [ { test: /\.scss$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use: ['css-loader', 'sass-loader'] }) }] }, plugins: [ new ExtractTextPlugin({ filename: 'css/[name].css', disable: false, allChunks: true }) ]