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

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版本不同是個大坑,我這個版本組合可以使用僅供參考!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章