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?
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.
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.