企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
#### 0.react依赖 PS: yarn add react PS: yarn add react-dom #### 1.index.js PS: md src PS: New-Item index.js 打开index.js加入以下代码 ```javascript import React from 'react' import { render } from 'react-dom' window.React = React render( <div>init react</div>, document.getElementById('react-container') ) ``` #### 2.init webpack.config PS: New-Item webpack.config.js 打开webpack.config.js写入一下相关配置 ```javascript var webpack = require("webpack") var path = require("path") process.noDeprecation = true module.exports = { entry: "./src/index.js", output: { path: path.join(__dirname, 'dist', 'assets'), filename: "bundle.js", sourceMapFilename: 'bundle.map' }, devtool: '#source-map', module: { rules: [ { test: /\.js$/, exclude: /(node_modules|bower_components)/, use: { loader: 'babel-loader', options: { presets: ['env','stage-0','react'] } } }, { test: /\.css$/, use: ['style-loader','css-loader', { loader: 'postcss-loader', options: { plugins: () => [require('autoprefixer')] }}] }, { test: /\.scss/, use: ['style-loader','css-loader', { loader: 'postcss-loader', options: { plugins: () => [require('autoprefixer')] }}, 'sass-loader'] } ] }, plugins: [ new webpack.DefinePlugin({ "process.env": { NODE_ENV: JSON.stringify("production") } }) // 此版本webpack已经没有UglifyJsPlugin的压缩了,需要修改package下的scripts就可以了 // 下边就讲到了 /* new webpack.optimize.UglifyJsPlugin({ sourceMap: true, warnings: false, mangle: false })*/ ] } ``` ### 3.完善package ```json "scripts": { "build": "webpack --mode production", "prestart": "npm run build", "start": "opener http://localhost:3001 & httpster -p 3001 -d ./dist" } ``` #### 4.完善index.html 在body最后,加入打包文件链接 ```html <script src="assets/bundle.js"></script> ```