手动配置webpack之React

 

安装

    1.安装react转译相关依赖包:

        npm安装:
            npm install --save-dev babel-core babel-loader babel-preset-es2015 babel-preset-react

        (推荐) yarn安装:
            yarn add --dev babel-core babel-loader babel-preset-es2015 babel-preset-react

    2.webpack相关依赖:
        yarn add --dev webpack webpack-cli webpack-dev-server

    3.webpack插件:
        yarn add --dev html-webpack-plugin clean-webpack-plugin

    4.react相关:
        yarn add react react-dom

配置

    项目结构:

|__src
   |____dist
        index.html
        main.bungle.js (动态生成)
   |____
        index.js
        app.jsx
   index.html
   webpack.config.js
   package.json

    文件配置:

package.json

{
  "name": "0501",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack-dev-server --open"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "clean-webpack-plugin": "^0.1.19",
    "express": "^4.16.3",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.16.2",
    "webpack-cli": "^3.1.0",
    "webpack-dev-middleware": "^3.1.3",
    "webpack-dev-server": "^3.1.5"
  },
  "dependencies": {
    "react": "^16.4.1",
    "react-dom": "^16.4.1"
  }
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Development</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

webpack.config.js

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')   //生成dist/index.html文件的插件,具体见webpack插件
const CleanWebpackPlugin = require('clean-webpack-plugin')  //删除dist文件夹 webpack插件

module.exports = {
    entry: path.resolve(__dirname, './src/index.js'),
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].bundle.js',
        publicPath: '/'
    },
    devtool: 'inline-source-map',
    mode: 'production',
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node-modules/,
                loader: 'babel-loader',
                options: {
                    presets: ['es2015', 'react']
                }
            },
            {
                test: /\.js$/,
                exclude: /node-modules/,
                loader: 'babel-loader',
                options: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({
          template: './index.html',  //在生成html时,是基于这个模板生成的
          title: 'Development',
          hash: true
        })
    ]
}

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './app.jsx'

ReactDOM.render(
    <App></App>,
    document.getElementById('root')
);

app.jsx

import React from 'react'

export default class App extends React.Component {
    constructor () {
        super()
        this.state = {
            txt: '11111111'
        }
    }

    render () {
        const txt = this.state.txt
        return (
            <div>
                {txt}
            </div>
        )
    }
}

描述:

npm run build:生成dist目录和index.html、打包后的js文件
npm run start:开发时用

 

posted @   DaivdAndLemon  阅读(234)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示
显示目录
目录