简体   繁体   English

如何在使用库时利用闭包编译器?

[英]How to take advantage of closure compiler when using a library?

I've recently been playing with the awesome tool from google that does some code-optimization and partial execution, for instance it would take something like: 我最近一直在使用谷歌的一个很棒的工具来做一些代码优化和部分执行,例如它会采取类似的方式:

//Just an alias for an elementByID selector
function $(bar){
    return document.getElementById(bar);
}

//Call the selector
alert($("foo").value);

And shorten it into alert(document.getElementById("foo").value); 并将其缩短为alert(document.getElementById("foo").value); , which is fairly awesome from an optimization viewpoint. ,从优化的角度来看,这是非常棒的。

I'm only explaining this because I would have thought this concept works for larger libraries such as jQuery, which basically attempts to abstract away a bunch of things JavaScript does, like select by IDs. 我只是在解释这个,因为我认为这个概念适用于更大的库,例如jQuery,它基本上试图抽象出JavaScript所做的一些事情,比如通过ID选择。

In a quick test, I loaded the whole jQuery production file up to the compiler and appended a single bit of text at the end of it: alert($("#foo").val()); 在快速测试中,我将整个jQuery生成文件加载到编译器并在其末尾添加了一些文本: alert($("#foo").val());

And quite tragically, the compiler wasn't able to map through jQuery's design and result with the simple example I had above, but rather my output is about 85kb of text with alert($("#foo").K()); 而且很可悲的是,编译器无法通过我上面的简单示例来映射jQuery的设计和结果,而是我的输出大约是85kb的带有alert($("#foo").K());的文本alert($("#foo").K()); stuck on the end. 坚持到底。 Basically, I just have minified code, and didn't take advantage of the awesome feature demonstrated above. 基本上,我只是缩小了代码,并没有利用上面演示的令人敬畏的功能。

So my question is, if I do end up using a library, how can I code in such a way that closure compiler is able to simplify my code to it's native JS (or something more effective than 85kb of unused code)? 所以我的问题是,如果我最终使用库,我怎样才能以这样的方式编写代码,即闭包编译器能够将我的代码简化为原生JS(或者比85kb的未使用代码更有效)? Alternatively, what design should someone take if they wanted to make a small library that plays nice? 或者,如果他们想要制作一个很好的小型图书馆,那么有人应该采取什么样的设计?

AFAIK, jQuery is not (yet) written to be optimized by the Closure Compiler's Advanced Mode. AFAIK,jQuery尚未编写为由Closure Compiler的高级模式优化。 It has an "externs" file which will enable its public properties and classes not to be renamed, but it does not enable most of the optimizations (eg dead code removal) as you've discovered. 它有一个“externs”文件,它可以使其公共属性和类不被重命名,但它不会像你发现的那样启用大多数优化(例如死代码删除)。 Which is quite a pity, because the jQuery object, if property written, does lends itself quite readily and nicely to the Closure Compiler's prototype virtualization feature. 这是非常可惜的,因为jQuery对象(如果属性编写)确实非常适合Closure Compiler的原型虚拟化功能。

Slightly off-topic 稍微偏离主题

If you are not tied to jQuery, you may consider the Dojo Toolkit, which can be modified to be used with the Closure Compiler while enabling most optimizations (especially dead-code removal). 如果你不依赖于jQuery,你可以考虑使用Dojo Toolkit,它可以被修改为与Closure Compiler一起使用,同时支持大多数优化(尤其是死代码删除)。

See this document for details. 请参阅此文档了解详细信息

jQuery takes special pains to minify itself and in the process makes itself opaque to the Closure Compiler. jQuery需要特别努力来缩小自身,并且在此过程中使自己对Closure Compiler不敏感。

Closure Library is an example of a library that is written to make good use of the Closure Compiler. Closure Library是为了充分利用Closure Compiler而编写的库的示例。

Generally, the compiler does best with prototype inheritance and simple structures: 通常,编译器最适合原型继承和简单结构:

/** @constructor */ function Class () {} Class.prototype.f = function() {}; / ** @constructor * / function Class(){} Class.prototype.f = function(){};

With an explicitly exported interface: window['MyLib'] = { 'method', method }; 使用显式导出的接口:window ['MyLib'] = {'method',method};

Generally, advanced mode only makes sense for a library if it has a small external interface relative to the amount of internal code. 通常,如果库具有相对于内部代码量较小的外部接口,则高级模式仅对库有意义。 However, I definitely would encourage writing your library so that it can be consumed by an advanced mode compiled project (this requires separating out the export used when it is used as a stand-alone library, if they library itself is minified using advanced mode). 但是,我绝对鼓励编写您的库,以便它可以被高级模式编译项目使用(这需要分离出用作独立库时使用的导出,如果它们使用高级模式缩小库本身) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Closure Compiler时,导出库方法的最佳方法是什么? - What is the best way to export library method, when using Closure Compiler? Google闭包编译器:使用工厂时如何进行继承 - Google closure compiler: How to do inheritance when using a factory 使用闭包编译器时,如何使用静态方法创建一个类数组? - How to have an array of classes with static methods when using the closure compiler? 使用Closure编译器时扩展错误 - Extending Error when using Closure Compiler 封闭编译器:如何确定何时内联? - Closure compiler: How does it decided when to inline? html5,带封闭编译器和/或闭包库的angularJS - html5, angularJS with closure compiler and/or closure library 如何避免Google Closure库/编译器中的循环依赖错误 - How to avoid circular-dependency errors in Google Closure Library/Compiler 使用带有闭包编译器的 jQueryUI - using jQueryUI with closure compiler 使用具有高级优化的闭包编译器时,如何在 javascript 中保留全局变量? - How to preserve global variables in javascript when using Closure compiler with advanced optimization? 即使使用对象,如何强制Google Closure Compiler重命名方法 - How to force Google Closure Compiler to rename methods even when using objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM