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

问题描述 投票:44回答:6

此错误是在本教程之后的数组中添加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()
  ]
};
javascript reactjs webpack react-hot-loader
6个回答
76
投票

似乎查询是自定义single加载程序行为的另一种方式,比内联指定那些参数更干净(请参阅下文)。如果存在多个加载程序,则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时,请参阅其他答案以获取在较新版本中可用的更干净的解决方案。


16
投票

我的解决方案:

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/
}

10
投票

webpack 2和3]中,可以更简洁地配置它。

加载程序可以在一组加载程序对象中传递。每个加载程序对象都可以指定一个options对象,该对象的作用类似于该特定加载程序的webpack 1 query

例如,在[[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' ] }] }

请注意已更改的属性名称贯穿整个链。

另外,请注意,我在es2015配置中将es2015-native-modules预设更改为babel-loader预设。这与options的规范无关,只是包含es6模块使您可以使用v2中引入的webpack摇树功能。可以不理会它,并且仍然可以使用,但是如果没有指出明显的升级,答案将是不完整的:-)


免责声明:这与我对similar question的回答相同,但是此问题的投票/观看次数/谷歌排名相似,因此我也在此处发布答案。


1
投票
对于

webpack 2


0
投票
此解决方案对我有用:

0
投票
自从我找到自己的解决方案以来,我就面临着同样的问题。您可以尝试:
© www.soinside.com 2019 - 2024. All rights reserved.