【发布时间】:2018-06-13 00:34:30
【问题描述】:
./client/index.js 中的错误 模块构建失败:SyntaxError: Unexpected token (31:4)
常量根 = () => { 返回(
<ApolloProvider client={client}> ^ <Router history={hashHistory}>
我的 Webpack 配置文件如下:
const path = require('path'),
webpack = require("webpack"),
clientPath = path.join(__dirname, 'client'),
HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: path.join(clientPath, 'index.js'),
output: {
path: __dirname,
filename: 'bundle.js'
},
module: {
rules: [
{
use: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
use: ['style-loader', 'css-loader'],
test: /\.css$/
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'file-loader?hash=sha512&digest=hex&name=[hash].[ext]',
'image-webpack-loader?bypassOnDebug&optimizationLevel=7&interlaced=false'
]
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader'
}
],
loaders: [
{ test: /\.jsx$/, exclude: /node_modules/, loader: "babel-loader" }
]
},
plugins: [
new HtmlWebpackPlugin({
template: 'client/index.html'
})
] };
我无法构建,它会抛出 Unexpected token 错误,而我在代码中没有语法错误,它只是无法识别反应代码样式
我已经尝试在这个地方的 webpack 配置中将 js 更改为 jsx
{
use: 'babel-loader',
test: /\.jsx$/,
exclude: /node_modules/
}
然后它会抛出不同的错误,例如
Module parse failed: /client/index.js Unexpected token (31:4)
You may need an appropriate loader to handle this file type.
【问题讨论】:
-
你的 babel conf 是什么?你需要一些预设来处理 JSX
标签: javascript webpack