2

I have a need to keep some specific comments in my uglified javascript. Is there a way to make the UglifyJS2 --comments parameter keep all wanted comments?

Example foo.js:

function foo()
{
    // don't keep this 
    /* delete this */
    /* KEEPME */
    for (var i=0; i < 10; i++)
    {
    alert('alert #'+i);
    }
/* KEEPME */
}

/*KEEPME*/

Using this Uglifyjs2 command line (with uglifyjs2 version 2.4.24):

node uglifyjs2 --comments "/KEEPME/i" -o foo.min.js foo.js

generates a foo.min.js containing:

function foo(){/* KEEPME */
for(var i=0;i<10;i++){alert("alert #"+i)}}

UglifyJs2 seems to remove comments at the end of a file or block even if they fit the regular expression that is supposed to preserve them. The Uglify documentation states that not all comments can be preserved due to various compression options. It seems like the comments in the example should be preservable. Disabling various compression options still seems to remove these comments.

Adding other necessary code statements at the end of the file or end of the block will cause the comments to be preserved.

If I could get it to preserve all my wanted comments, that'd be great. I would also settle instead for a way to remove all //comments and just keep all /* comments */.

truefish
  • 141
  • 1
  • 6

1 Answers1

1

See https://github.com/mishoo/UglifyJS2/issues/88 as this is a known bug in uglifyjs. In its current state, uglifyjs can't keep comments at the end of a (sub)tree (for programmers: that means end of file, end of function and that kind of stuff)

Note: I've got answers with links only removed on stackoverflow, though I feel that these kind of issues should be solved there. Feel free to remove this answer if this answer doesn't fit your requirements.

avdg
  • 308
  • 1
  • 12