手动配置webpack之React

手动配置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 @ 2018-07-25 17:58 DaivdAndLemon 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值