Skip to content Skip to sidebar Skip to footer

What Is The Best Way To Export Library Method, When Using Closure Compiler?

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

Solution 1:

If you use JSDoc consistently, you could use the @export tag:

/**
* @param {String} str
* @export
*/Lib.prototype.invoke = function(str){
     //this function is called from outside to invoke some of Lib's events
};

and call the compiler with the --generate_exports flag.

This requires you to either include base.js from the Google Closure library, or to copy goog.exportSymbol and goog.exportProperty to your codebase.

Solution 2:

Personally, I like defining interfaces in externs file and having my internal classes implement them.

// Externs/** @interface */function IInvoke {};
IInvoke.prototype.invoke;

/** 
 *  @constructor
 *  @implements {IInvoke}
 */functionLib(){ 
  //some initialization  
}
Lib.prototype = {
  invoke : function(str){
     //this function is called from outside to invoke some of Lib's events
  }  
}

You still export the constructor itself, but not the interface methods.

Post a Comment for "What Is The Best Way To Export Library Method, When Using Closure Compiler?"