webpack 不与 babel-loader 捆绑并做出反应
webpack not bundling with babel-loader and react
我正在关注一个相对较旧的 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"
}
}
谁能告诉我为什么它在 return 之后不读取 <h3>
并在 webpack 尝试捆绑时导致错误?
您缺少一些预设:
首先执行 npm 安装:
npm install babel-core babel-preset-es2015 babel-preset-react --save-dev
同时在你的根项目目录中创建一个 .babelrc,并包含这个:
{
"presets": ["es2015","react"]
}
同样在 webpack 配置的加载器部分,为 jsx 包含这个:
{ test: /\.jsx?$/, exclude: /node_modules/, use: ['babel-loader'] }
我正在关注一个相对较旧的 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"
}
}
谁能告诉我为什么它在 return 之后不读取 <h3>
并在 webpack 尝试捆绑时导致错误?
您缺少一些预设:
首先执行 npm 安装:
npm install babel-core babel-preset-es2015 babel-preset-react --save-dev
同时在你的根项目目录中创建一个 .babelrc,并包含这个:
{
"presets": ["es2015","react"]
}
同样在 webpack 配置的加载器部分,为 jsx 包含这个:
{ test: /\.jsx?$/, exclude: /node_modules/, use: ['babel-loader'] }