UglifyJS2在块或文件的末尾删除所需的注释 [英] UglifyJS2 removes wanted comments at the end of a block or file

查看:1290
本文介绍了UglifyJS2在块或文件的末尾删除所需的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的丑陋的javascript中保留一些具体的意见。有没有办法让UglifyJS2 --comments参数保留所有想要的评论?

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?

示例foo.js:

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

/*KEEPME*/

使用此Uglifyjs2命令行(使用uglifyjs2版本2.4.24):

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

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

一个foo.min.js包含:

generates a foo.min.js containing:

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

UglifyJs2似乎删除在文件或块的结尾的注释,即使他们适合正则表达式,应该保留它们。 Uglify文档声明,由于各种压缩选项,不能保留所有注释。看起来像示例中的注释应该可以保留。禁用各种压缩选项似乎仍然会删除这些注释。

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

推荐答案

https://github.com/mishoo/UglifyJS2/issues/88 ,因为这是一个已知的bug在uglifyjs。在当前状态下,uglifyjs不能在(子)树的末尾保留注释(对于程序员:这意味着文件结束,函数结束和那种东西)

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)

注意:我有答案与链接只有在stackoverflow删除,但我觉得这些问题应该解决。如果此答案不符合您的要求,请随意移除此答案。

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.

这篇关于UglifyJS2在块或文件的末尾删除所需的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆