首页 > Web开发 > 详细

[Webpack] Create Separate webpack Configs for Development and Production with webpack-merge

时间:2019-02-15 23:14:46      阅读:321      评论:0      收藏:0      [点我收藏+]

The development and production modes in webpack optimize the output in different ways. In development mode, the focus is on faster builds and a better developer experience. In production mode, the focus is on highly optimized bundles, leading to a better user experience. The good news is, we can have both. In this lesson, we‘ll separate our webpackconfig into two configurations and update our scripts to run the right config for our needs.

 

Install:

npm i -D webpack-merge

 

Create a webpack.config.base.jf:

const path = require(path)
const HtmlWebpackPlugin = require(html-webpack-plugin)

module.exports = {
  entry: ./src/index.js,
  output: {
    path: path.join(__dirname, dist),
    filename: app.bundle.js
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: babel-loader,
        exclude: /node_modules/,
        options: {
          presets: [@babel/preset-env, @babel/preset-react]
        }
      }
    ]
  },
  plugins: [new HtmlWebpackPlugin({
    template: ./src/index.html
  })]
}

 

webpack.config.dev.js:

const merge = require(webpack-merge)
const baseConfig = require(./webpack.config.base)

module.exports = merge(baseConfig, {
  mode: development
})

 

webpack.config.prod.js:

const merge = require(webpack-merge)
const baseConfig = require(./webpack.config.base)

module.exports = merge(baseConfig, {
  mode: production
})

 

Update scripts to adopt changes:

"scripts": {
    "build": "webpack --config webpack.config.prod.js",
    "dev": "webpack --watch --config webpack.config.dev.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  }

 

[Webpack] Create Separate webpack Configs for Development and Production with webpack-merge

原文:https://www.cnblogs.com/Answer1215/p/10386308.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!