资讯专栏INFORMATION COLUMN

react+webpack+redux+react-router环境搭建

yzzz / 451人阅读

摘要:第一步,新建一个文件夹,然后初始化,会生成一个记录整个项目的依赖包信息安装新建以下目录文件,安装包文件内容文件内容,作为整个项目的主入口接下来开始配置四大组成部分入口打包后的文件存放地方,会自动新建打包后输出

第一步,新建一个文件夹,然后npm init -yes初始化,会生成一个package.json记录整个项目的依赖包信息

安装webpack cnpm i webpack webpack-dev-server --save-dev

新建以下目录文件,

安装reactcnpm i react react-dom redux react-redux redux-thunk react-router --save-dev

index.html文件内容:




    
    react-weChat


index.jsx文件内容,作为整个项目的主入口

import ReactDOM from "react-dom";
import React, {Component} from "react";
 
class App extends Component {
    render() {
        return 
Hello React !!!
} } ReactDOM.render( , document.getElementById("root") )

接下来开始配置webpack.config.js:
webpack四大组成部分:entry,output,module,plugins

const srcDir = __dirname + "/src";
const distDir = __dirname + "/dist";

module.exports = {
    entry: [
        srcDir + "/index.jsx" //入口
    ],
    output: {
        path: distDir,//打包后的文件存放地方,会自动新建
        filename: "index.[hash:7].js"//打包后输出的文件名,后面跟7位随机hash值
    },
    devtool: "source-map",
    devServer: {
        contentBase: "./public",//本地服务器所加载的页面的目录
        historyApiFallback: true,//不跳转
        inline: true,//实时刷新
        port: 8090, //端口号
        hot: true
    },
    module:{},
    plugins:[]




再安装解析jsxloader及插件去解析jsxes6语法:cnpm i babel-cli babel-loader babel-preset-env babel-preset-react babel-plugin-react-transform react-transform-hmr --save-dev

module: {
        rules: [
            {
                test: /(.jsx|.js)$/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: [
                            "env", "react"
                        ]
                    }
                },
                exclude: /node_modules/
            },



  //.babelrc
    {
      "presets": [
        "react",
        "env"
      ],
      "env": {
        "development": {
          "plugins": [
            [
              "react-transform",
              {
                "transforms": [
                  {
                    "transform": "react-transform-hmr",
                    "imports": [
                      "react"
                    ],
                    "locals": [
                      "module"
                    ]
                  }
                ]
              }
            ]
          ]
        }
      }
    }

cnpm i html-webpack-plugin --save-dev

const HtmlWebpackPlugin = require("html-webpack-plugin");//生成新的html文件
plugins: [
        
        new HtmlWebpackPlugin({//根据模板引入css,js最终生成的html文件
            filename: "index.html",//生成文件存放路径
            template: "./public/index.html",//html模板路径
        }),

cnpm i extract-text-webpack-plugin clean-webpack-plugin --save-dev

const ExtractTextPlugin = require("extract-text-webpack-plugin");//分离css和js
const CleanWebpackPlugin = require("clean-webpack-plugin");//清缓存


new ExtractTextPlugin("style.css"),//独立导出css
    new CleanWebpackPlugin("dist/*.*", {
        root: __dirname,
        verbose: true,
        dry: false
    })


cnpm i style-loader css-loader postcss-loader autoprefixer --save-dev

            {
                test: /.css$/,
                use: [
                    {
                        loader: "style-loader" //在html中插入