对于使用babel loader和eslint的webpack,我有点新鲜 我正在尝试编译一个非常基本的应用程序 我得到这个奇怪的语法错误,我无法弄清楚
这是我的index.js,我得到编译错误
const store = configureStore()
render(
<Router>
<Root store={ store } />
</Router>,
document.getElementById('console_root')
)
这是我得到的错误:
ERROR in ./src/index.js
Module build failed: SyntaxError: Unexpected token (19:2)
17 |
18 | render(
> 19 | <Router>
| ^
20 | <Root store={ store } />
21 | </Router>,
22 | document.getElementById('console_root')
npm ERR! code ELIFECYCLE
npm ERR! errno 2
这是我的webpack.config.js文件
var webpack = require('webpack')
var path = require('path')
module.exports = {
entry: {
main: path.resolve(__dirname, 'src', 'index.js')
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
output: {
path: path.resolve(__dirname, '..'),
publicPath: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader', 'eslint-loader']
},
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
}
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
})
]
}
这些是我的依赖项:
"dependencies": {
"material-ui": "^1.0.0-beta.26",
"material-ui-icons": "^1.0.0-beta.17",
"prop-types": "^15.6.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-redux": "^5.0.6",
"react-router-dom": "^4.2.2",
"redux": "^3.7.2"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-eslint": "^8.1.2",
"babel-loader": "^7.1.2",
"css-loader": "^0.28.7",
"eslint": "^4.14.0",
"eslint-loader": "^1.9.0",
"eslint-plugin-react": "^7.5.1",
"react-scripts": "^1.0.17",
"sass-loader": "^6.0.6",
"style-loader": "^0.19.1",
"webpack": "^3.10.0"
}
任何想法我做错了什么?
答案 0 :(得分:2)
好像您错过了将react
预设添加到 webpack.config.js 文件或 .babelrc 。
首先应安装它:npm i babel-preset-react --save-dev
然后将react
预设添加到您的webpack配置中,如下所示
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['react']
}
}
}
]
}
或者创建一个名为 .babelrc 的新文件并将其添加到那里
// .babelrc
{
"presets": ["react"]
}
它应该适合你。
答案 1 :(得分:1)
我想您需要创建一个包含内容的.babelrc
文件
{
"presets": [
"es2015",
"react"
],
}
这将告诉babel将es6转换为es5并且还告诉我们有一个反应项目并且它将相应地处理。对于这些,你需要两个包来完成这项工作
npm install babel-preset-es2015 --save
npm install babel-preset-react --save