当前位置: 代码迷 >> 综合 >> React +webpack+babel 配置jsx成功案例
  详细解决方案

React +webpack+babel 配置jsx成功案例

热度:85   发布时间:2023-11-24 10:37:36.0

package.json文件

{
    "name": "reactProject","version": "1.0.0","description": "1.npm init -y 初始化一个项目形成包管理文件\r 2.npm i webpack -D 安装webpack打包工具\r 3.npm i webpack-cli -D 安装webpack打包工具\r 4.建立webpack.config.js文件,配值打包(src/index.js,disst/main.js)\r 5.npm i webpack-dev-server -D 实时浏览\r 6.packjson中配值一下执行的脚本 \"dev\":\"webpack-dev-server\"\r 7.webpack-dev-server打包的文件在内存中看不到(位于根目录)\r 8.对于html文件放入内容使用 npm i html-webpack-plugin -D","main": "webpack.config.js","scripts": {
    "test": "echo \"Error: no test specified\" && exit 1","dev": "webpack-dev-server --open"},"repository": {
    "type": "git","url": "https://gitee.com/zsProjects/reactProject.git"},"keywords": [],"author": "","license": "ISC","devDependencies": {
    "babel-core": "^6.26.3","babel-loader": "^7.1.5","babel-plugin-transform-runtime": "^6.23.0","babel-preset-env": "^1.7.0","babel-preset-react": "^6.24.1","babel-preset-stage-0": "^6.24.1","html-webpack-plugin": "^3.2.0","webpack": "^4.42.0","webpack-cli": "^3.3.11","webpack-dev-server": "^3.10.3"},"dependencies": {
    "react": "^16.13.0","react-dom": "^16.13.0"}
}

web.config.js

const path = require('path')
const HtmlWebPackPlugin= require('html-webpack-plugin')
const htmlpugin = new HtmlWebPackPlugin({
    template:path.join(__dirname,'./src/index.html'),filename:'index.html'
})
module.exports ={
    mode:'development',plugins: [htmlpugin],module: {
    rules: [{
    test:/\.js|jsx$/, use: 'babel-loader', exclude: /node_modules/}]}
}

.babelrc

{
    "presets": ["env","react", "stage-0"],"plugins": ["transform-runtime"]
}

只需要将这三个文件复制黏贴到自己的项目,然后npm install即可
babel版本不同是个大坑,我这个版本组合可以使用仅供参考!!!

  相关解决方案