webapck 搭建 react redux 项目

最近在学习react,redux 已经redux在react使用。

1.建立目录目里

public 

  index.html //存在容器的根节点

src

  actions  //redux的action

  components //redux的components 

  containers  //redux的containers  

  reducers    //redux的reducers   

webpack.config.js

2.这里重点讲webpack的配置

const path = require('path');
var webpack = require('webpack');
const htmlWebapckPlugin = require('html-webpack-plugin');

const uglifyWebpackPlugin = require('uglifyjs-webpack-plugin');
const pageAgeJson = require('./package.json')

let HtmlPlugin = new htmlWebapckPlugin({
    template: './public/index.html',
    filename: 'index.html',
    inject: true,
    chunks: 1
});

let devFlagPlugin = new webpack.DefinePlugin({
    __DEV__: JSON.stringify(JSON.parse(process.env.DEBUG || 'false'))
});


module.exports = {
    mode: 'development', //production  development
    entry: './src/index.js',
    // {
    //     // bundle1: './src/index.js',
    //     // bundle2: './src/index2.js',
    //     // bundle3: './src/bableTest.js'
    //     // first: './src/first.js',
    //     // second: './src/second.js',
    //     // client:'./src/index.js',
    //     // vendor: Object.keys(pageAgeJson.dependencies)

    // },
    output: {
        path: path.resolve(__dirname, 'dist'),
        //filename: 'bundle.js'
        filename: '[name].js'
    },
    optimization:{
        splitChunks: {
            cacheGroups: {
              vendor: {
                chunks: 'initial',
                name: 'vendor',
                test: 'vendor',
                enforce: true
              },
            }
          },
          runtimeChunk: true
    },
    plugins: [
        HtmlPlugin,
        //new uglifyWebpackPlugin(),
        devFlagPlugin
    ],
    module: { //要打包的第三方模块
        rules: [{
            test: /\.js|jsx$/,
            use: 'babel-loader',
            exclude: /node_modules/
        }, {
            test: /\.css$/,

            //1 后面加上modules
            //use: ['style-loader', 'css-loader?modules&localIdentName=[name][local][hash:base64:5]']
            //2 options:{moudules:true}
            use: [{
                loader: 'style-loader'
            }, {
                loader: "css-loader",
                options: {
                    modules: true,
                    localIdentName: '[name][local][hash:base64:3]'
                }
            }]

        }, {
            test: /\.jpg|png|gif|svg/,
            use: [{
                loader: 'url-loader',
                options: {
                    limit: 8192
                }
            }]
        }]
    },

    devServer: {
        host: '127.0.0.1',
        port: 8888
    },
    externals:{
        data:'data',
        $:'jquery'
    }
}

3.搭建环境的依赖的包:

{
  "name": "webpack4",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "dev": " cross-env DEBUG=true webpack-dev-server  --open --hot --process --colors  "
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "bundle-loader": "^0.5.6",
    "cross-env": "^5.2.0",
    "css-loader": "^1.0.0",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.22.1",
    "uglifyjs-webpack-plugin": "^1.3.0",
    "url-loader": "^1.1.0",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5",
    "rimraf": "^2.6.2"
  },
  "dependencies": {
    "bootstrap": "^3.3.7",
    "jquery": "^3.3.1",
    "prop-types": "^15.6.2",
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0"
  }
}

4.babelrc 文件

{
"presets": ["env", "stage-2","react"],
"plugins": ["transform-runtime"]
}

5 npm的包安装完,在根目录 npm run dev 就好了。

猜你喜欢

转载自www.cnblogs.com/cylblogs/p/9548807.html