6

I would like to use the Webpack UglifyJSPlugin to only remove comments from my bundle. I currently have the following UglifyJSPlugin configuration:

webpackPlugins = [
    new DedupePlugin(),
    new UglifyJsPlugin({
        compress: false,
        minimize: false,
        outputs: {
            comments: false
        }
    })
]

However, this still seems to minify the entire bundle. Is there another option to remove comments I am not taking advantage of? Is there a way to do this properly with UglifyJSPlugin?

BTC
  • 3,802
  • 5
  • 26
  • 39

4 Answers4

9

This is what you need:

new UglifyJsPlugin({
    comments: false,
}),
geniuscarrier
  • 4,199
  • 2
  • 21
  • 15
4

Using webpack 2, the settings below worked for me.

new webpack.optimize.UglifyJsPlugin({
  output: {
    comments: false,
    beautify: true,
  },
  mangle: false,
  compress: false,
}),
xiao
  • 1,718
  • 4
  • 23
  • 31
  • Yes, I added ```, optimization: { minimize: true, minimizer: [ new UglifyJsPlugin({ uglifyOptions: { output: { comments: false, beautify: true, }, mangle: false, compress: false, } }) ] }```to the bottom of the webpack config js and it worked. – moltenform Apr 24 '20 at 00:30
2

What you're looking for is probably "beautify" combined with "mangle".

"Beautify" will output indented code instead of a one-line file, so you want this to be true. "mangle" will make your code as short as possible (e.g. by abbreviating variable names), so you want this to be false.

For more info about these two options see the UglifyJS README

webpackPlugins = [
    new DedupePlugin(),
    new UglifyJsPlugin({
        beautify: true,
        mangle: false
    })
]
ericornelissen
  • 161
  • 2
  • 8
1

Webpack 5 now has a slightly different config file:

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
                uglifyOptions: {
                    output: {
                        comments: false,
                        beautify: true,
                    },
                    mangle: false,
                    compress: false
                },
            }),
        ],
    },
    ...
}

UglifyjsWebpackPlugin Docs

bordeaux
  • 333
  • 3
  • 15