谷歌闭包编译器没有压缩字符串值?

3

有这样的内容:

(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压缩器可以将文件压缩至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中压缩这些字符串变量?为什么它不这样做呢?是因为性能更好的结果吗?

可能是重复的问题:如何防止 Closure 编译器复制字符串 - Chad Killingsworth
1个回答

3
这是Closure Compiler FAQ中的内容。Closure Compiler假定您使用gzip压缩。如果没有,请配置服务器以对代码进行gzip压缩,这是您可能可以执行的最有效和最简单的优化之一。gzip算法通过尝试以最佳方式别名字节序列来工作。手动别名字符串几乎总会使压缩后的代码大小变大,因为它破坏了gzip自身别名算法。因此,当可能时,Closure Compiler几乎总会内联您的字符串,因为这将使压缩后的代码更小。具体详情请参阅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

谢谢,我在GitHub上错过了常见问题解答! - Alvaro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接