Javascript 带有 babel-loader 的 Webpack 无法识别导入关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31799558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webpack with babel-loader not recognizing import keyword
提问by Jacob
I have this webpack.config.js
:
我有这个webpack.config.js
:
module.exports = {
entry: './src/admin/client/index.jsx',
output: {
filename: './src/admin/client/static/js/app.js'
},
loaders: [
{
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/,
query: {
optional: ['runtime']
}
}
],
resolve: {
extensions: ['', '.js', '.jsx']
}
};
...yet I still get this error:
...但我仍然收到此错误:
$ webpack -v Hash: 2a9a40224beb025cb433 Version: webpack 1.10.5 Time: 44ms [0] ./src/admin/client/index.jsx 0 bytes [built] [failed] ERROR in ./src/admin/client/index.jsx Module parse failed: /project/src/admin/client/index.jsx Line 1: Unexpected reserved word You may need an appropriate loader to handle this file type. | import React from 'react'; | import AdminInterface from './components/AdminInterface';
I have:
我有:
- Installed
webpack
globally and locally - Installed
babel-loader
,babel-core
, andbabel-runtime
- Installed
babel-loader
globally, just in case
webpack
全局和本地安装- 安装
babel-loader
,babel-core
和babel-runtime
babel-loader
全局安装,以防万一
Why the hell is webpack seemingly ignoring babel-loader
? Or does babel-loader
not work with modules?
为什么 webpack 似乎无视babel-loader
?或者不适babel-loader
用于模块?
Update:
更新:
It looks like babel
handles the input file just fine. When I run:
看起来babel
处理输入文件就好了。当我运行时:
./node_modules/babel/bin/babel.js ./src/admin/client/index.jsx
...it outputs ES5 as expected. Therefore, it seems to me like somehow webpack
isn't properly loading babel-loader
.
...它按预期输出 ES5。因此,在我看来好像webpack
没有正确加载babel-loader
.
回答by Jacob
This looks like a case of operator error. My webpack.config.js
structure was not correct. Specifically, I needed to put the loader details inside of a module
section:
这看起来像是操作员错误的情况。我的webpack.config.js
结构不正确。具体来说,我需要将加载器详细信息放在一个module
部分中:
module.exports = {
entry: './src/admin/client/index.jsx',
output: {
filename: './src/admin/client/static/js/app.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/,
query: {
optional: ['runtime']
}
}
],
resolve: {
extensions: ['', '.js', '.jsx']
}
}
};
回答by Adam Bertrand
I fixed the same problem by including the es2015and reactpresetsand then adding them to the webpack.config.js file.
我通过包含es2015和react预设,然后将它们添加到 webpack.config.js 文件来解决同样的问题。
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-react
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-react
as explained in this post: https://www.twilio.com/blog/2015/08/setting-up-react-for-es6-with-webpack-and-babel-2.html
如本文所述:https: //www.twilio.com/blog/2015/08/setting-up-react-for-es6-with-webpack-and-babel-2.html
my full webpack.config.js file:
我的完整 webpack.config.js 文件:
module.exports = {
context: __dirname + "/src",
entry: './main',
output: {
path: __dirname + "/build",
filename: "bundle.js"
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: ['es2015', 'react']
}
}
],
resolve: {
extensions: ['.js', '.jsx']
}
}
};
回答by modao
What is the version of your babel?If babel version is up to 6+.You need to identify the preset 'es2015' and 'react' like this
你的babel是什么版本的?如果babel版本高达6+。你需要像这样识别预设的'es2015'和'react'
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel', // 'babel-loader' is also a legal name to reference
query: {
presets: ['react', 'es2015']
}
}
]
}
Don't forget to install these modules:
不要忘记安装这些模块:
npm install babel-loader babel-core babel-preset-es2015 babel-preset-react --save-dev