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

发布于 2024-09-05 13:23:20 字数 538 浏览 4 评论 0原文

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

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

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

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

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

I've noticed that every time I use Google's Closure Compiler Service, it leaves a few unnecessary spaces in the compiled code presented on the right-hand side of the page. These correspond to line breaks in the hosted version of the compiled code.

For example (note the line breaks, each of which seems unnecessary):

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

To date, I've just been removing them manually, but I'm curious why they're there. Is it to limit the line length of the hosted version of the code to make it more readable?
Could the compiler be smart enough to leave or insert those intentionally to maximize GZIP compression efforts?

I know that they have a trivial effect on the file size, but with so much effort going into minifying every last byte in the source script, it's counterintuitive why they're there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-09-12 13:23:20

引用闭包编译器常见问题解答

为什么编译的脚本中有随机换行?

闭包编译器有意每 500 个字符左右添加换行符。防火墙和代理有时会损坏或忽略具有很长行的大型 JavaScript 文件。每 500 个字符添加换行符可以防止此问题。删除换行符对脚本的语义没有影响。对代码大小的影响很小,并且编译器优化了换行符位置,以便在对文件进行 gzip 压缩时,代码大小的损失甚至更小。

你知道这很聪明! :)

Quoting the 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.

You knew it was smart! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文