使用webpack构建react - 配置篇

首先配置webpack配置文件: ./config/dev.js

const path = require("path");
const WebpackHtmlPlugin = require('webpack-html-plugin');
//配置webpack-html-plugin 插件
const webpackHtmlPlugin = new WebpackHtmlPlugin({
    filename:"index.html",
    template:path.resolve(__dirname,"../","index.html"),
    inject:"body"
});

module.exports = {
    entry:{
        app:path.resolve(__dirname,"../src","main.js")
    },
    output:{
        path:path.resolve(__dirname,"../dist"),
        filename:"js/[name].js"
    },
    plugins:[
        webpackHtmlPlugin
    ],
    module:{
        rules:[
            {
                test:/\.(js|jsx)$/,
                loader:"babel-loader"
            }
        ]
    },
    devServer:{//配置本地开发测试环境
        contentBase:path.resolve(__dirname,"../dist"), //配置本地服务器的启动根目录
        port:3000,
        inline:true,
    }
}

依赖配置清单

{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "npm run dev",
    "dev": "webpack-dev-server --config ./config/dev.js --progress --open",
    "build": "webpack --config ./config/build.js --progress --color"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "6.26.0",
    "babel-loader": "7.1.2",
    "babel-preset-env": "1.6.1",
    "babel-preset-react": "6.24.1",
    "less": "^3.9.0",
    "less-loader": "^5.0.0",
    "webpack": "^3.12.0",
    "webpack-html-plugin": "^0.1.1"
  },
  "dependencies": {
    "react": "^16.9.0",
    "react-dom": "^16.9.0"
  }
}