此错误是在本教程之后的数组中添加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()
]
};
似乎查询是自定义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时,请参阅其他答案以获取在较新版本中可用的更干净的解决方案。
我的解决方案:
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]中,可以更简洁地配置它。
加载程序可以在一组加载程序对象中传递。每个加载程序对象都可以指定一个options
对象,该对象的作用类似于该特定加载程序的webpack 1 query
。
例如,在[[webpack 2/3
中,同时使用react-hot-loader
和babel-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的回答相同,但是此问题的投票/观看次数/谷歌排名相似,因此我也在此处发布答案。
webpack 2