11

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.

Koushik Das
  • 9,678
  • 3
  • 51
  • 50
  • 1
    Answered here: http://stackoverflow.com/questions/35846011/webpack-use-uglifyjsplugin-to-only-remove-comments – prosti Jan 30 '17 at 15:06
  • check this: http://stackoverflow.com/questions/41040266/remove-console-logs-with-webpack-uglify/41041580#41041580 – Mayank Shukla Jan 30 '17 at 15:14

4 Answers4

11

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.

Everettss
  • 15,475
  • 9
  • 72
  • 98
4

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.

Community
  • 1
  • 1
prosti
  • 42,291
  • 14
  • 186
  • 151
4

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.

laszlo-horvath
  • 1,852
  • 2
  • 19
  • 20
0

If you want to remove @words as well, and cannot find a solution online, i would suggest you to perform a find and replace.
Write a regex to find and replace it with a blank character.

rahul_sann
  • 304
  • 3
  • 9