使用 Closure Compiler 时导出库方法的最佳方法是什么?
Closure Compiler 文档明确指出:“不要使用 Externs 而不是 Exports”。因为 Extern 使用起来非常方便,所以我遇到了一个问题:
function Lib(){
//some initialization
}
Lib.prototype = {
invoke : function(str){
//this function is called from outside to invoke some of Lib's events
}
}
当将 Closure Compiler 与 ADVANCED_OPTIMIZATIONS 一起使用时,函数调用会从源代码中删除。可以通过两种方式来防止这种情况: 在原型定义之后添加这一行:
Lib.prototype['invoke'] = Lib.prototype.invoke;
但这在输出代码的末尾添加了丑陋的代码和平:
Lib.prototype.invoke = Lib.prototype.g;
我设法通过将这一行添加到构造函数来摆脱这一点:
this.invoke = this.invoke;
并将这一行添加到 externs 文件:
/**
* @param {String} str
*/
Lib.prototype.invoke = function(str){};
这样,闭包编译器无法从输出代码中删除invoke函数,因为它是在构造函数中自行分配的,而且它无法重命名它,因为它是在externs文件中定义的。 那么巫婆的方法更好吗?
Closure Compiler documentation clearly states: "Don't use Externs instead of Exports". Because Externs are very handy to use, I came down with a problem:
function Lib(){
//some initialization
}
Lib.prototype = {
invoke : function(str){
//this function is called from outside to invoke some of Lib's events
}
}
When using Closure Compiler with ADVANCED_OPTIMIZATIONS, function invoke is removed from the source. This could be prevented in two ways:
Adding the line after prototype definition:
Lib.prototype['invoke'] = Lib.prototype.invoke;
But this adds an ugly peace of code at the end of the output code:
Lib.prototype.invoke = Lib.prototype.g;
I managed to get rid of this by adding this line to the constructor:
this.invoke = this.invoke;
And this line to the externs file:
/**
* @param {String} str
*/
Lib.prototype.invoke = function(str){};
This way, Closure Compiler can't remove invoke function from the output code, because it is assigned by itself in the constructor, and also, it can't rename it, because it is defined in the externs file.
So witch method is better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您始终使用 JSDoc,则可以使用
@export
标签:并使用
--generate_exports
标志调用编译器。这要求您包含
base.js
< /a> 从 Google Closure 库,或将goog.exportSymbol
和goog.exportProperty
复制到您的代码库。If you use JSDoc consistently, you could use the
@export
tag:and call the compiler with the
--generate_exports
flag.This requires you to either include
base.js
from the Google Closure library, or to copygoog.exportSymbol
andgoog.exportProperty
to your codebase.就我个人而言,我喜欢在 externs 文件中定义接口并让我的内部类实现它们。
您仍然导出构造函数本身,但不导出接口方法。
Personally, I like defining interfaces in externs file and having my internal classes implement them.
You still export the constructor itself, but not the interface methods.