javascript - 为什么 Google 的闭包编译器会留下一些不必要的空格或换行符?

标签 javascript google-closure google-closure-compiler minify

我注意到每次我使用Google's Closure Compiler Service时,它在页面右侧显示的编译代码中留下了一些不必要的空格。这些对应于已编译代码的托管版本中的换行符。

例如(注意换行符,每个换行符似乎都是不必要的):

http://troy.onespot.com/static/stack_overflow/closure_spaces.js

到目前为止,我只是手动删除它们,但我很好奇它们为什么会在那里。是为了限制托管版本代码的行长度以使其更具可读性吗? 编译器是否足够聪明,可以故意保留或插入这些内容以最大化 GZIP 压缩效果?

我知道它们对文件大小的影响微不足道,但是由于花费了大量精力来缩小源脚本中的每个最后字节,因此它们存在的原因是违反直觉的。

最佳答案

引用Closure Compiler FAQ :

Why are there random line feeds in compiled scripts?

The Closure Compiler intentionally adds line breaks every 500 characters or so. Firewalls and proxies sometimes corrupt or ignore large JavaScript files with very long lines. Adding line breaks every 500 characters prevents this problem. Removing the line breaks has no effect on a script's semantics. The impact on code size is small, and the Compiler optimizes line break placement so that the code size penalty is even smaller when files are gzipped.

你知道这很聪明! :)

关于javascript - 为什么 Google 的闭包编译器会留下一些不必要的空格或换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3018049/

相关文章:

javascript - 为什么我的 javascript for 循环不起作用? for (var i=1; i<5; i++;) { document.write ("A statement has run "); }

javascript - 如何更改 Safari 自动播放设置的默认设置?

javascript - `goog.require` 的包含顺序是什么?

javascript - 与 Closure Compiler 一起工作的 JavaScript 类的实现

javascript - 未设置 PHP GET 变量

javascript - 过度依赖预处理器是一个坏习惯吗?

javascript - goog.require 和 goog.addDependency 有什么区别?

javascript - 为什么这行谷歌闭包看起来像?

javascript - 闭包编译器删除的内容超出了我想要删除的内容

node.js - 使用 grunt 运行 Google Closure 编译器