How to build minified and uncompressed bundle with webpack?

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

    webpack.config.js:

    const webpack = require("webpack");
    
    module.exports = {
      entry: {
        "bundle": "./entry.js",
        "bundle.min": "./entry.js",
      },
      devtool: "source-map",
      output: {
        path: "./dist",
        filename: "[name].js"
      },
      plugins: [
        new webpack.optimize.UglifyJsPlugin({
          include: /\.min\.js$/,
          minimize: true
        })
      ]
    };
    

    Since Webpack 4, webpack.optimize.UglifyJsPlugin has been deprecated and its use results in error:

    webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead

    As the manual explains, the plugin can be replaced with minimize option. Custom configuration can be provided to the plugin by specifying UglifyJsPlugin instance:

    const webpack = require("webpack");
    const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
    
    module.exports = {
      // ...
      optimization: {
        minimize: true,
        minimizer: [new UglifyJsPlugin({
          include: /\.min\.js$/
        })]
      }
    };
    

    This does the job for a simple setup. A more effective solution is to use Gulp together with Webpack and do the same thing in one pass.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 15:17

    In my opinion it's a lot easier just to use the UglifyJS tool directly:

    1. npm install --save-dev uglify-js
    2. Use webpack as normal, e.g. building a ./dst/bundle.js file.
    3. Add a build command to your package.json:

      "scripts": {
          "build": "webpack && uglifyjs ./dst/bundle.js -c -m -o ./dst/bundle.min.js --source-map ./dst/bundle.min.js.map"
      }
      
    4. Whenever you want to build a your bundle as well as uglified code and sourcemaps, run the npm run build command.

    No need to install uglify-js globally, just install it locally for the project.

    0 讨论(0)
  • 2020-11-29 15:19

    I found a new solution for this problem.

    This uses an array of configuration to enable webpack to build the minified and non-minified version in parallel. This make build faster. No need to run the webpack twice. No need extra plugins. Just webpack.

    webpack.config.js

    const devConfig = {
      mode: 'development',
      entry: { bundle: './src/entry.js' },
      output: { filename: '[name].js' },
      module: { ... },
      resolve: { ... },
      plugins: { ... }
    };
    
    const prodConfig = {
      ...devConfig,
      mode: 'production',
      output: { filename: '[name].min.js' }
    };
    
    module.exports = (env) => {
      switch (env) {
        case 'production':
          return [devConfig, prodConfig];
        default:
          return devConfig;
      }
    };
    

    Running webpack will only build the non-minified version.

    Running webpack --env=production will build the minified and non-minified version at the same time.

    0 讨论(0)
  • 2020-11-29 15:20

    To add another answer, the flag -p (short for --optimize-minimize) will enable the UglifyJS with default arguments.

    You won't get a minified and raw bundle out of a single run or generate differently named bundles so the -p flag may not meet your use case.

    Conversely the -d option is short for --debug --devtool sourcemap --output-pathinfo

    My webpack.config.js omits devtool, debug, pathinfo, and the minmize plugin in favor of these two flags.

    0 讨论(0)
  • 2020-11-29 15:24

    You can run webpack twice with different arguments:

    $ webpack --minimize
    

    then check command line arguments in webpack.config.js:

    var path = require('path'),
      webpack = require('webpack'),
      minimize = process.argv.indexOf('--minimize') !== -1,
      plugins = [];
    
    if (minimize) {
      plugins.push(new webpack.optimize.UglifyJsPlugin());
    }
    
    ...
    

    example webpack.config.js

    0 讨论(0)
提交回复
热议问题