Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack use UglifyJSPlugin to ONLY remove comments

Tags:

webpack

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?

like image 287
BTC Avatar asked Mar 07 '16 14:03

BTC


4 Answers

This is what you need:

new UglifyJsPlugin({
    comments: false,
}),
like image 55
geniuscarrier Avatar answered Dec 04 '22 16:12

geniuscarrier


Using webpack 2, the settings below worked for me.

new webpack.optimize.UglifyJsPlugin({
  output: {
    comments: false,
    beautify: true,
  },
  mangle: false,
  compress: false,
}),
like image 44
xiao Avatar answered Dec 04 '22 15:12

xiao


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
    })
]
like image 25
ericornelissen Avatar answered Dec 04 '22 14:12

ericornelissen


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

like image 39
bordeaux Avatar answered Dec 04 '22 14:12

bordeaux