【发布时间】:2018-06-16 10:37:27
【问题描述】:
我正在关注关于 RGR 的相对较旧的教程(从 2014 年开始)。我必须使用更新版本的 React、Webpack 和 Babel,所以存在一些差异。到目前为止一切正常,除了当我尝试将 JSX 编译到 webpack 中时,它给了我一个构建错误。
ERROR in ./public/js/app.js
Module build failed: SyntaxError: Unexpected token (7:15)
5 | class Hello extends React.Component {
6 | render() {
7 | return <h3>Hello Webpack!</h3>;
| ^
8 | }
9 | }
10 |
下面是我的 React 文件:
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class Hello extends React.Component {
render() {
return <h3>Hello Webpack!</h3>;
}
}
ReactDOM.render(<Hello />, document.getElementById('react'));
这是我的 webpack.config.js 文件
module.exports = {
entry: "./public/js/app.js",
output: {
path: __dirname + "/public",
filename: "bundle.js"
},
module: {
loaders: [
{test: /\.js$/,
loader: 'babel-loader' }
]
}
}
另外,这是我的 package.json 文件
{
"name": "rgrjs",
"version": "1.0.0",
"description": "a collection of educational resources about React, GraphQL, and Relay",
"main": "index.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/krisxcrash/rgr-stack.git"
},
"author": "kristine martin",
"license": "ISC",
"bugs": {
"url": "https://github.com/krisxcrash/rgr-stack/issues"
},
"homepage": "https://github.com/krisxcrash/rgr-stack#readme",
"dependencies": {
"babel-loader": "^7.1.2",
"create-react-class": "^15.6.2",
"express": "^4.16.2",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1"
}
}
谁能告诉我为什么它在返回后没有读取<h3> 并在 webpack 尝试捆绑时导致错误?
【问题讨论】:
-
你试过包装
Hello Webpack!
;在括号中,例如 ' return {Hello Webpack!
; }' ? -
我相信你需要配置你的 babel-loader 来寻找 jsx。 Twilio 的这篇博文可能会有所帮助:twilio.com/blog/2015/08/…。具体来说,关于“使用 Webpack 捆绑一切”的部分
-
@Dream_Cap 我刚刚尝试将 html 括在括号中,但它不起作用。
-
@DerekHopper 让我试试 - 待定!
-
@DerekHopper 所以我把所有东西都打包好了。更新了 /\.jsx?$/,但它似乎仍然不起作用。我尝试将文件名更改为 .jsx,但在重新捆绑 webpack 时出现问题。
标签: reactjs webpack jsx babeljs babel-loader