【问题标题】:Google Closure compiler not compressing string values?Google Closure 编译器不压缩字符串值?
【发布时间】:2015-05-13 17:07:02
【问题描述】:

有这样的东西:

(function ($, window, document, undefined) {
    'use strict';
    $.fn.demo = function (options) {

        var active = "active";
        var section = ".bb-demo";

        $(section).addClass(active);
        $(section).addClass(active);
        $(section).addClass(active);
        $(section).addClass(active);

    };
})(jQuery, window, document);

Closure Simple mode 产生 200 字节

(function(a,b,c,d){a.fn.demo=function(b){a(".bb-demo").addClass("active");a(".bb-demo").addClass("active");a(".bb-demo").addClass("active");a(".bb-demo").addClass("active")}})(jQuery,window,document);

YUI compressor 的结果是169 字节

(function(c,b,a,d){c.fn.demo=function(e){var g="active";var f=".bb-demo";c(f).addClass(g);c(f).addClass(g);c(f).addClass(g);c(f).addClass(g)}})(jQuery,window,document);

难道没有办法在 Closure 中压缩这些字符串变量吗? 为什么不这样做? 是因为性能方面的结果更好吗?

【问题讨论】:

标签: javascript compression minify google-closure-compiler yui-compressor


【解决方案1】:

这涵盖了闭包编译器常见问题解答。 https://github.com/google/closure-compiler/wiki/FAQ#closure-compiler-inlined-all-my-strings-which-made-my-code-size-bigger-why-did-it-do-that

Closure Compiler 假定您正在使用 gzip 压缩。如果你 不要,你应该。配置您的服务器以压缩您的代码是其中之一 最有效和最简单的优化 做。 gzip 算法通过尝试在 一种最佳方式。手动别名字符串几乎总是使 压缩后的代码体积更大,因为它颠覆了gzip自己的算法 用于混叠。所以闭包编译器将(几乎)总是内联你的 字符串,因为这将使您的压缩代码 更小。

【讨论】:

  • 谢谢,我错过了github中的FAQ!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 2010-12-13
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多