缩小后的 JavaScript 中的注释会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9467945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:46:35  来源:igfitidea点击:

What happens to comments in minified JavaScript?

javascriptminify

提问by Rondel

What happens to comments in a minifiedJavaScript file? How can the browser know when the end of the comment is when everything is condensed to one line? Take this little example, I have Google tracking code like this:

缩小后的JavaScript 文件中的注释会发生什么?浏览器如何知道注释的结束是什么时候所有内容都压缩为一行?拿这个小例子来说,我有这样的谷歌跟踪代码:

//Google tracking
var _gaq = _gaq || []; 
_gaq.push(['_setAccount', '123456']); 

The minified version pulls everything into one line

缩小版将所有内容都归为一行

// Google tracking var _gaq = _gaq || []; _gaq.push(['_setAccount', '123456']);

There are more statements, but when I examine the JavaScript code in an editor, it looks like one giant comment (more or less). Is there a hidden character that tells the browser when to end a comment or is this code just not being executed?

有更多的语句,但是当我在编辑器中检查 JavaScript 代码时,它看起来像是一个巨大的注释(或多或少)。是否有一个隐藏字符告诉浏览器何时结束评论或者这段代码只是没有被执行?

采纳答案by Mike Samuel

Minifiers strip comments or insert line-breaks. For example, Closure Compiler's FAQsays:

缩小器去除注释或插入换行符。例如,Closure Compiler 的 FAQ说:

Can I use the Closure Compiler together with other JavaScript minifiers?

Yes. Closure Compiler reads in any valid JavaScript and generates valid JavaScript, so you can apply the Closure Compiler to a JavaScript file either before or after you run the file through a different minifier.

Remember that Closure Compiler and other minifiers might have expectations about the incoming code. A minifier that strips comments may remove licenses or annotation information needed by another tool, for example.

我可以将 Closure Compiler 与其他 JavaScript minifiers 一起使用吗?

是的。Closure Compiler 读取任何有效的 JavaScript 并生成有效的 JavaScript,因此您可以在通过不同的 minifier 运行文件之前或之后将 Closure Compiler 应用于 JavaScript 文件。

请记住,Closure Compiler 和其他压缩器可能对传入的代码有预期。例如,去除注释的压缩器可能会删除另一个工具所需的许可证或注释信息。

Sometimes you really need a comment in which case they put in a line break.

有时你真的需要一个注释,在这种情况下他们会换行。

I have copyright notices or open source license text that must appear in my source code. How do I keep the Closure Compiler from stripping this text out?

Closure Compiler supports the JSDoc @licensetag. Add the @licensetag to any JSDoc comment to preserve the comment in the compiler output. See Annotating JavaScript for the Closure Compiler for more information.

我的源代码中必须出现版权声明或开源许可文本。我如何防止 Closure Compiler 剥离此文本?

Closure Compiler 支持 JSDoc@license标签。将@license标记添加到任何 JSDoc 注释以保留编译器输出中的注释。有关更多信息,请参阅为闭包编译器注释 JavaScript。

Minifiers also tend to break lines occasionally, because some interpreter's source code parsers crashed or performed really slowly on really long lines.

Minifiers 也倾向于偶尔断行,因为一些解释器的源代码解析器在很长的行上崩溃或执行非常缓慢。

https://bugzilla.mozilla.org/show_bug.cgi?id=634444

https://bugzilla.mozilla.org/show_bug.cgi?id=634444

Previously, because we were dealing with chunks, there was a limit to how much of a line an error message could include. But now the error message contains the entire line. If you have very long lines and lots of errors, it's a recipe for high memory usage, especially since we call js_DeflateString()on the error message string, resulting in two copies of it (one made of jschars, the other made of chars).

On the site in question, heaps of errors occurred on a line containing 122,895 chars, resulting in over 1G of chars (at 3 bytes per char!) being put into error messages.

以前,因为我们处理的是块,所以错误消息可以包含的行数是有限制的。但现在错误消息包含整行。 如果你有很长的行和很多错误,这是高内存使用的一个秘诀,特别是因为我们调用js_DeflateString()错误消息字符串,导致它的两个副本(一个由jschars 组成,另一个由chars 组成)。

在有问题的站点上,在包含 122,895 个字符的一行上发生了大量错误,导致超过 1G 的chars(每个 3 个字节char!)被放入错误消息中。

回答by Paul

JavaScript single-line comments end at the first new-line character reached. A minifier will remove comments before removing newlines so that it won't break your code.

JavaScript 单行注释在到达的第一个换行符处结束。minifier 将在删除换行符之前删除注释,这样它就不会破坏您的代码。

For an example, if you paste

例如,如果您粘贴

//GOOGLE TRACKING
var _gaq = _gaq || []; 
_gaq.push(['_setAccount', '123456']); 

into Closure Compilerwith simple optimizations you get

通过简单的优化进入Closure Compiler

var _gaq=_gaq||[];_gaq.push(["_setAccount","123456"]);

With advanced optimizations you get:

通过高级优化,您可以获得:

var a=a||[];a.push(["_setAccount","123456"]);

The comments are removed in both.

两者中的评论都被删除了。