在 React 组件上抛出 Webpack 抛出错误<div>



每次运行npm run build 时都会出现此错误

Failed to compile.
./node_modules/react-d3-graph/src/components/graph/Graph.jsx 698:6
Module parse failed: Unexpected token (698:6)
You may need an appropriate loader to handle this file type, 
currently no loaders are configured to process this file. See https://webpack.js.org/concepts#
loaders
|
|     return (
>       <div id={`${this.state.id}-${CONST.GRAPH_WRAPPER_ID}`}>
|         <svg name={`svg-container-${this.state.id}`} style={svgStyle} onClick={this.onClickGraph}>
|           {defs}

这是在我的JSX文件Graph.JSX的分叉repo中返回的。据我所知,经过几个小时的研究,我已经在webpack.config.js中添加了JSX进行测试。(js|jsx(。我也想过添加其他插件或预设,但我所做的似乎都不起作用。这是我的包.json

{
"name": "my-app",
"version": "0.1.0",
"main": "./build/index.js",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"csvtojson": "^2.0.10",
"d3": "^5.5.0",
"d3-selection": "^2.0.0",
"html-webpack-root-plugin": "^0.10.0",
"react": "^16.4.1",
"react-data-table-component": "^7.0.0-alpha-5",
"react-dom": "^17.0.1",
"react-file-reader": "^1.1.4",
"react-request": "^3.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.2",
"react-scroll-wheel-handler": "^2.0.1",
"styled-components": "^5.2.1",
"use-neo4j": "^0.3.5",
"web-vitals": "^1.0.1",
"write-json-file": "^4.3.0",
"xlsx": "^0.16.9",
"xtypejs": "^0.7.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"api": "npx json-server --watch .\src\components\limit.json --port 8000"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/plugin-proposal-class-properties": "^7.16.0",
"@babel/plugin-proposal-object-rest-spread": "^7.16.0",
"@babel/plugin-syntax-jsx": "^7.16.0",
"@babel/plugin-transform-react-jsx": "^7.16.0",
"@babel/preset-env": "^7.16.0",
"@babel/preset-react": "^7.16.0",
"@babel/preset-typescript": "^7.16.0",
"axios": "^0.21.1",
"babel-loader": "^8.2.3",
"babel-plugin-transform-class-properties": "^6.24.1",
"chart.js": "^3.5.1",
"html-webpack-plugin": "^5.5.0",
"react-chartjs-2": "^3.0.5",
"react-charts": "^2.0.0-beta.7",
"react-d3-graph": "github:<username>/react-d3-graph",
"react-modal": "^3.14.3",
"react-router-scroll": "^0.4.4",
"react-use": "^17.2.4",
"webpack": "^4.46.0",
"webpack-bundle-analyzer": "^4.5.0"
}
}

这是我的webpack.config.js

const webpack = require("webpack");
module.exports = {
devtool: 'inline-source-map',
entry: [
'webpack-hot-middleware/client',
'./client/client.js'
],
output: {
path: require('path').resolve('./dist'),
filename: 'bundle.js',
publicPath: '/'
},
loader:
'babel-loader',
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
"@babel/plugin-syntax-jsx",
],
module: {
rules: [
{
test: /.(js|jsx)$/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', { targets: "defaults" }]
]
}
}
}
]
}
};

我真的不知道为什么HTML标记会抛出错误。这是我的.babelrc

{
"presets": [
[ "@babel/preset-env", {
"modules": false,
"targets": {
"browsers": [
"last 2 Chrome versions",
"last 2 Firefox versions",
"last 2 Safari versions",
"last 2 iOS versions",
"last 1 Android version",
"last 1 ChromeAndroid version",
"ie 11"
]
}
} ],
"@babel/preset-react"
],
"plugins": [ "@babel/plugin-proposal-class-properties" ]
}

很明显,我已经下载了babel加载程序,并将其添加到我的webpack.config.js文件中,但即使这样也无济于事。

Webpack是一个捆绑器,它将利用/调用其他一些东西来捆绑您的代码。为了做到这一点,你需要为你在webpack中使用的大多数东西加载程序。如果你有css,那么你需要一个css加载程序,这就是webpack用来传输和捆绑css的。如果你有javascript,尤其是更新的es6或jsx,那么webpack不会理解它,你必须为它提供一些能够真正解释和理解代码的加载器。最常用的是babel加载程序和用于typescript的ts加载程序。

当前在代码中发生的情况是,webpack找到文件并加载它,但不知道它在说什么。你必须安装babel或ts加载程序,或者两者都安装,然后你必须将其添加到配置中的webpack加载程序部分,然后webpack将调用这些加载程序来传输你的代码并捆绑它。

我最终没有弄清楚为什么加载程序没有注册,相反,我只是将repo下载为gzip,提取出来,放在我的app/src文件夹中,并更改了一些包,这很好。

最新更新