Webpack - 错误:无法在加载器列表中定义“query”和多个加载器

43

我在按照这篇教程添加react-hot加载器后,出现了这个错误:https://thoughtbot.com/blog/setting-up-webpack-for-react-and-hot-module-replacement

我收到了Error: Cannot define 'query' and multiple loaders in loaders list

var WebpackDevServer = require("webpack-dev-server");
var webpack = require('webpack');
var path = require('path');
require("babel-polyfill");

var BUILD_DIR = path.resolve(__dirname, 'build');
var APP_DIR = path.resolve(__dirname, 'src');

module.exports = {
  entry: [
    'babel-polyfill',
    'bootstrap-loader',
    'webpack/hot/dev-server',
    APP_DIR + '/import.js',
  ],
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      test: /\.jsx?$/,
      loaders: ['react-hot', 'babel'],
      exclude: /node_modules/,
      query: {
        plugins: ['transform-runtime'],
        presets: ['es2015', 'stage-0', 'react']
      }
    }, {
      test: /\.css$/,
      loader: "style-loader!css-loader"
    }, {
      test: /\.scss$/,
      loaders: ["style", "css", "sass"]
    }, {
      test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
      loader: 'url-loader?limit=100000'
    }]
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ]
};

6个回答

76

看起来查询是一种定制单个加载器行为的替代方式,比直接指定这些参数更干净(请参见下文)。如果存在多个加载器,则Webpack不知道query配置应用于哪个加载器。

以下内容应该解决您的问题:

module: {
    loaders: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loaders: ['react-hot', 'babel?presets[]=es2015,presets[]=stage-0,presets[]=react,plugins[]=transform-runtime']
    }

编辑:尽管此解决方案适用于Webpack 1,但请查看其他答案以获取在更近期版本中运行的更简洁的解决方案。


感谢您发布这个解决方案。 - Maxwelll
对于复杂的查询,请使用 webpack-combine-loaders - Rockallite

18

我的解决方案:

loaders: [{
  test: /\.(js|jsx)$/,
  loaders: ['react-hot', 'babel?' + JSON.stringify({
    cacheDirectory: true,
    plugins: [
      'transform-runtime',
      'transform-decorators-legacy'
    ],
    presets: ['es2015', 'react', 'stage-0'],
    env: {
      production: {
        presets: ['react-optimize']
      }
    }
  }), 'eslint'],
  include: src,
  exclude: /node_modules/
}

1
我喜欢JSON.stringify的想法 :) - velop

10

webpack 2 和 3 中,这可以更干净地配置。

可以通过加载器对象的数组传递加载器。每个加载器对象都可以指定一个类似于 webpack 1 的 queryoptions 对象,用于特定加载器。

例如,在 webpack 2/3 中使用 react-hot-loaderbabel-loader,并为 babel-loader 配置一些选项:

module: {
  rules: [{
    test: /\.js$/,
    exclude: /node_modules/,
    use: [{
      loader: 'react-hot-loader'
    }, {
      loader: 'babel-loader',
      options: {
        babelrc: false,
        presets: [
          'es2015-native-modules'
          'stage-0',
          'react'
        ]
      }
    }]
  }] 
}

为了比较,这里是相同配置在webpack 1中使用查询字符串方法的示例。

module: {
  rules: [{
    test: /\.js$/,
    exclude: /node_modules/,
    loaders: [
      'react-hot',
      'babel-loader?' +
        'babelrc=false,' +
        'presets[]=es2015,' +
        'presets[]=stage-0,' +
        'presets[]=react'
      ]
  }] 
}

请注意整个过程中所有更改的属性名称。

另外,请注意我在`babel-loader`配置中将`es2015`预设更改为`es2015-native-modules`预设。这与`options`规范无关,只是因为包含ES6模块允许您使用v2引入的webpack树摇特性。它可以保持不变仍然有效,但如果不指出明显的升级,答案会感觉不完整 :-)。


免责声明:这与我在类似的问题的回答相同,但是由于此问题具有相似的投票/查看/谷歌排名,因此我也会在此处发布答案。


1
对于webpack 2,我成功配置如下:


    var webpack = require("webpack");
    var path = require("path");
module.exports = { entry: "./src/index.js", output: { path: path.resolve(__dirname, "dist/assets"), filename: "bundle.js", publicPath: "/assets/" }, devServer: { inline: true, contentBase: './dist', port: 3000 }, module: { loaders: [ { test: /\.js$/, exclude: /(node_modules)/, loader: "babel-loader", options: { presets: ['latest', 'react', 'stage-0'] } } ] } };

0

我曾经遇到过同样的问题,后来我找到了解决方案。你可以试试:

--- 这是解决方案 ---

如果你在 ".babelrc" 文件中定义了 "presets", 那么你就不需要在 "webpack.config.js" 文件中指定它,删除它就可以了。


如果你没有这样做,我建议你去你的“.babelrc”文件中指定你的预设。


0

这个解决方案对我有效:

module: {
        loaders:[
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                loader: 'babel-loader'
            }
        ]
    }

和在 .babelrc 中的预设

{
    'presets': ['latest', 'react', 'stage-0']
}

请参考https://webpack.github.io/docs/usage.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接