gpt4 book ai didi

javascript - 如何在 Webpack 4 中转译 JSX

转载 作者:行者123 更新时间:2023-11-30 14:22:35 24 4
gpt4 key购买 nike

我已经经历了this solution .我已尝试将解析对象同时放置在规则内部和外部规则中,但我仍然遇到相同的错误。

我也尝试过更新 npm 并重新安装它以防万一,但没有成功。

目录结构:

- src/
- components/
- Card/
- card.jsx
- Sidebar.js
- Dashboard.js
- app.js

因为我使用的是 babel-loader,所以我已经像这样导入了我的 jsx 文件。

从“./Card/Card”导入卡片;

我也尝试过使用“.jsx”导入,但我仍然遇到 Unresolved 相同错误。

运行webpack后报错信息:

 ERROR in ./src/components/Sidebar.js
Module not found: Error: Can't resolve './Card/Card' in
'E:\React\accounting\src\components'
@ ./src/components/Sidebar.js 40:0-31 190:29-33
@ ./src/components/Dashboard.js
@ ./src/app.js
@ multi (webpack)-dev-server/client?http://localhost:8080 ./src/app.js

webpack.config.js 文件:

 const path = require('path');

module.exports = {
entry: './src/app.js',
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
module: {
rules: [{
loader: 'babel-loader',
test: /\.jsx?$/,
exclude: /node_modules/,
resolve: {
extensions: [".jsx", ".js", ".json"]
}
}, {
test: /\.s?css$/,
use: [ 'style-loader', 'css-loader', 'sass-loader' ]
}, {
test: /\.(png|jpg|gif)$/i,
use: [{
loader: 'url-loader',
options: {
limit: 8192
}
}]
}]
},
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: path.join(__dirname, 'public')
}
};`enter code here`

.babelrc 文件

{
"presets": [
"@babel/env",
"@babel/preset-react"
],
"env": {
"production": {
"plugins": [
["emotion", { "hoist": true }]
]
},
"development": {
"plugins": [
["emotion",
{ "sourceMap": true, "autoLabel": true }
],
"transform-class-properties",
"transform-react-jsx"
]
}
}
}

package.json 依赖

"dependencies": {
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"@material-ui/core": "^3.1.1",
"@material-ui/icons": "^3.0.1",
"babel-cli": "^6.26.0",
"babel-loader": "^8.0.2",
"babel-plugin-emotion": "^9.2.10",
"babel-plugin-import-rename": "^1.0.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-react-jsx": "^6.24.1",
"css-loader": "^1.0.0",
"emotion": "^9.2.10",
"file-loader": "^2.0.0",
"live-server": "^1.2.0",
"node-sass": "^4.9.3",
"normalize.css": "8.0.0",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-modal": "^3.5.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.0",
"url-loader": "^1.1.1",
"validator": "^8.0.0",
"webpack": "^4.20.0",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.9"
}

最佳答案

此解决方案适用于我,我提供这些文件作为引用。希望对您有所帮助。

<强>1。 webpack.config.js 文件:

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
main: './src/index.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
mode:'development',
devServer: {
hot: true,
port: 3000
},
module: {
rules: [
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
},
{
test:/\.(js|jsx)?$/,
use: ['babel-loader']
}
]
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'React App',
template: './public/index.html'
})
]
};

<强>2。 .babelrc 文件:

{
"presets": ["env", "react", "stage-2"]
}

<强>3。 package.json 文件:

{
"name": "demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server --open"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"clean-webpack-plugin": "^0.1.19",
"css-loader": "^0.28.11",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.9.0",
"react-hot-loader": "^4.3.3",
"sass-loader": "^7.0.3",
"style-loader": "^0.21.0",
"webpack": "^4.15.0",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"
},
"dependencies": {
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"uuid": "^3.3.2"
}
}

关于javascript - 如何在 Webpack 4 中转译 JSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52497030/

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