Unable to strip comments in webpack bundle js file

15,429

Solution 1

UglifyJsPlugin don't remove @licence comments even if you set comments: false for legal reasons. You can read about it on webpack GitHub issue.

If you want to remove this kind of comments (on your own risk) you should search for other loaders like webpack-comment-remover-loader or stripcomment-loader.

Solution 2

This is what you need:

new UglifyJsPlugin({
    comments: false,
}),

From here.


Here is the line from the Webpack and @Everettss is right.

File: /webpack/lib/optimize/UglifyJsPlugin.js
097:                        let output = {};
098:                        output.comments = Object.prototype.hasOwnProperty.call(options, "comments") ? options.comments : /^\**!|@preserve|@license/;
099:                        output.beautify = options.beautify;
100:                        for(let k in options.output) {
101:                            output[k] = options.output[k];
102:                        }

and you may check the regular expression which confirms what Sokra stated.

enter image description here

I am not sure about UglifyJsPlugin, but usually if you privide the legal statement somewhere else you should eliminate all the comments.

If your lawyer confirms this is OK, you may try to tweak the /*! so the regular expression fails and the comments will be not there any more.

Solution 3

For webpack 4 this was working for me:

// webpack.config.js

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

module.exports = {
  // other config properties...

  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        uglifyOptions: {
          output: {
            comments: false
          }
        }
      })
    ]
  }
};

You can find more information in the official docs.

Share:
15,429

Related videos on Youtube

Koushik Das
Author by

Koushik Das

Full stack developer with Php, Laravel in the backend to React JS in the front end to PostgreSQL, MySQL in database to AWS, DigitalOcean and Google Cloud in server administration. Currently, diving more into the world of python 😀

Updated on July 09, 2022

Comments

  • Koushik Das
    Koushik Das almost 2 years

    I have been trying to strip comments in the webpack bundled js file. I have tried several methods but it's not working still and I get comments like

    "/**\n * Copyright 2013-present, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under the BSD-style license found in the\ ...
    

    For this the bundled file is getting huge. Currently huge at 1.6mb size. I have tried this

    new webpack.optimize.UglifyJsPlugin({
            sourceMap: false,
            compress: {
                sequences: true,
                dead_code: true,
                conditionals: true,
                booleans: true,
                unused: true,
                if_return: true,
                join_vars: true,
                drop_console: true
            },
            mangle: {
                except: ['$super', '$', 'exports', 'require']
            },
            output: {
                comments: false
            }
        })
    

    also this

    new webpack.optimize.UglifyJsPlugin({
            compress: { warnings: false },
            sourceMap: false,
            output: false
        })
    

    Also set the enviroment to production

    set NODE_ENV=production
    

    I am not able to understand where I am wrong. Please help. Thanks in advance.