How to build minified and uncompressed bundle with webpack?

前端 未结 14 1126
囚心锁ツ
囚心锁ツ 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:15

    You can format your webpack.config.js like this:

    var debug = process.env.NODE_ENV !== "production";
    var webpack = require('webpack');
    
    module.exports = {
        context: __dirname,
        devtool: debug ? "inline-sourcemap" : null,
        entry: "./entry.js",
        output: {
            path: __dirname + "/dist",
            filename: "library.min.js"
        },
        plugins: debug ? [] : [
            new webpack.optimize.DedupePlugin(),
            new webpack.optimize.OccurenceOrderPlugin(),
            new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
        ],
    };'
    

    And then to build it unminified run (while in the project's main directory):

    $ webpack
    

    To build it minified run:

    $ NODE_ENV=production webpack
    

    Notes: Make sure that for the unminified version you change the output file name to library.js and for the minified library.min.js so they do not overwrite each other.

提交回复
热议问题