UglifyJS2 removes wanted comments at the end of a block or file

断了今生、忘了曾经 提交于 2019-12-10 23:34:38

问题


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 */.


回答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.



来源:https://stackoverflow.com/questions/32592288/uglifyjs2-removes-wanted-comments-at-the-end-of-a-block-or-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!