手动配置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:开发时用

原文地址:https://www.cnblogs.com/wenwenwei/p/10017846.html