How to build minified and uncompressed bundle with webpack?

前端 未结 14 1164
囚心锁ツ
囚心锁ツ 2020-11-29 14:52

Here\'s my webpack.config.js

var webpack = require(\"webpack\");

module.exports = {

  entry: \"./entry.js\",
  devtool: \"source-map\",
  outp         


        
14条回答
  •  青春惊慌失措
    2020-11-29 15:32

    You can use a single config file, and include the UglifyJS plugin conditionally using an environment variable:

    const webpack = require('webpack');
    const TerserPlugin = require('terser-webpack-plugin');
    
    const PROD = JSON.parse(process.env.PROD_ENV || '0');
    
    module.exports = {
    
      entry: './entry.js',
      devtool: 'source-map',
      output: {
        path: './dist',
        filename: PROD ? 'bundle.min.js' : 'bundle.js'
      },
      optimization: {
        minimize: PROD,
        minimizer: [
          new TerserPlugin({ parallel: true })
      ]
    };
    

    and then just set this variable when you want to minify it:

    $ PROD_ENV=1 webpack
    

    Edit:

    As mentioned in the comments, NODE_ENV is generally used (by convention) to state whether a particular environment is a production or a development environment. To check it, you can also set const PROD = (process.env.NODE_ENV === 'production'), and continue normally.

提交回复
热议问题