gpt4 book ai didi

reactjs - Webpack React 构建在 html 标签上抛出错误

转载 作者:行者123 更新时间:2023-12-03 13:47:57 25 4
gpt4 key购买 nike

每次运行 npm start 时都会出现错误,这是我的错误......

ERROR in ./app/App.js
Module build failed: SyntaxError: Unexpected token (8:3)

6 | render() {
7 | return (
> 8 | <div>
| ^
9 | Testing this
10 | </div>
11 | )

这是一个简单的 react 组件内部。由于某种原因,html 标签在我的构建中导致错误。下面是我的 webpack.config.js 和包 json 文件。

var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');


var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});

module.exports = {
entry: [
'./app/App.js'
],
output: {
path: __dirname + '/public',
filename: "bundle.js"
},
plugins: [HTMLWebpackPluginConfig],
devServer: {
inline:true,
contentBase: './public',
port: 3333
},
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.scss$/,
loader: 'style!css!sass'
}]
}
};

包 json

{
"name": "lr",
"version": "1.0.0",
"description": "",
"main": "App.js",
"scripts": {
"start": "webpack-dev-server"
},
"author": "sam",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"css-loader": "^0.26.1",
"html-webpack-plugin": "^2.25.0",
"sass": "^0.5.0",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-hot-loader": "^1.3.1",
"react-router": "^3.0.0"
}
}

不知道为什么 html 标签会抛出错误。

最佳答案

您需要通知 webpack 您还希望它使用 .jsx 扩展。尝试更新您的测试模式:

{
test : /\.(js|jsx)$/,
loaders: ['babel'],
include: path.join(__dirname, 'src')
}

您还需要一个 .babelrc 文件来通知 babel 您希望它如何执行编译:

{
"presets": ["es2015", "react"]
}

在这里查看有效的实现:https://github.com/mikechabot/react-boilerplate

关于reactjs - Webpack React 构建在 html 标签上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41432655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com