Webpack - Error: Cannot define 'query' and multiple loaders in loaders list
Webpack - Error: Cannot define 'query' and multiple loaders in loaders list
我按照本教程在数组中添加 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()
]
};
似乎查询是自定义 单个 加载器行为的另一种方法,它比内联指定这些参数更清晰(见下文)。如果存在多个加载器,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
。
例如,同时使用react-hot-loader
和babel-loader
,babel-loader
配置了一些选项,在webpack 2/3
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中相同的配置,使用query string方式
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 tree-shaking 功能。它可以单独留下,它仍然可以工作,但如果没有指出明显的升级,答案会感觉不完整:-)
免责声明:这与我对 similar question 的回答相同,但此问题的 votes/views/google 排名相似,因此我将 post 回答这里也是。
这个解决方案对我有用:
module: {
loaders:[
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: 'babel-loader'
}
]
}
和 .babelrc 中的预设
{
'presets': ['latest', 'react', 'stage-0']
}
对于 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']
}
}
]
}
};
自从我为自己找到解决方案后,我就遇到了同样的问题。你可以试试:
--- 这是解决方案 ---
如果您在“.babelrc”文件中定义了"presets"
那就不用在"webpack.config.js"文件里指定了,删掉就好了
如果你不这样做,
我建议你去你的“.babelrc”文件并在那里指定你的预设
我按照本教程在数组中添加 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()
]
};
似乎查询是自定义 单个 加载器行为的另一种方法,它比内联指定这些参数更清晰(见下文)。如果存在多个加载器,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
。
例如,同时使用react-hot-loader
和babel-loader
,babel-loader
配置了一些选项,在webpack 2/3
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中相同的配置,使用query string方式
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 tree-shaking 功能。它可以单独留下,它仍然可以工作,但如果没有指出明显的升级,答案会感觉不完整:-)
免责声明:这与我对 similar question 的回答相同,但此问题的 votes/views/google 排名相似,因此我将 post 回答这里也是。
这个解决方案对我有用:
module: {
loaders:[
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: 'babel-loader'
}
]
}
和 .babelrc 中的预设
{
'presets': ['latest', 'react', 'stage-0']
}
对于 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']
}
}
]
}
};
自从我为自己找到解决方案后,我就遇到了同样的问题。你可以试试:
--- 这是解决方案 ---
如果您在“.babelrc”文件中定义了"presets" 那就不用在"webpack.config.js"文件里指定了,删掉就好了
如果你不这样做, 我建议你去你的“.babelrc”文件并在那里指定你的预设