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

IT技术 javascript reactjs webpack react-hot-loader
2021-04-26 08:05:21

在我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()
  ]
};
4个回答

查询似乎是自定义单个加载器行为的另一种方式,这比内联指定这些参数更清晰(见下文)。如果存在多个加载器,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,但请参阅其他答案以了解适用于更新版本的更清洁的解决方案。

我的解决方案:

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

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

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

例如,同时使用react-hot-loaderbabel-loader,与babel-loader配置有一些选项,在的WebPack三分之二

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预设更改为预设这与 的规范无关,只是包含 es6 module允许您使用 v2 中引入的 webpack tree-shaking 功能。它可以单独存在,它仍然可以工作,但是如果没有指出明显的升级,答案会感觉不完整:-)es2015-native-modulesbabel-loaderoptions


免责声明:这与我对类似问题的回答相同,但这个问题的投票/观点/谷歌排名相似,所以我也会在这里发布答案。


对于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']
                    }
                }
            ]
        }
    };