Woodgnome
Woodgnome

Reputation: 2391

Closure compiler: How to declare object and all of its properties as extern?

I'm trying to compile my Google Chrome extension which makes use chrome.i18n.getMessage() and a couple of other chrome properties.

I'm compiling using the Java library and have a externs.js file that I am including with the --externs parameters

I'm wondering whether or not it's possible to declare chrome as an extern, without having to specify all the properties I want to preserver?

I've tried the following 3 approaches so far:

Example 1:

/** @const */
var chrome = {}; // chrome.i18n.getMessage() gets renamed to chrome.a.b()

Example 2:

/** @const */
window.chrome = {}; // chrome.i18n.getMessage() gets renamed to chrome.a.b()

Example 3:

/* chrome.i18n.getMessage() is preserved, but chrome.runtime.connect() is renamed
 * to chrome.b.c()
 */
var chrome = {
  i18n: {
    getMessage: function(){}
  }
};

Upvotes: 2

Views: 294

Answers (1)

Woodgnome
Woodgnome

Reputation: 2391

I went with the third example while fixing bugs introduced after compiling and eventually ran into some difficulties while defining more and more properties of chrome. My thought then was to see if someone else created an extern file for chrome which lead me to Googles source for the Closure Compiler. Google has been nice enough to create externs files for several well known libraries:

https://code.google.com/p/closure-compiler/source/browse/#git%2Fexterns https://code.google.com/p/closure-compiler/source/browse/#git%2Fcontrib%2Fexterns

Closure Compiler Externs Extractor might also be useful, if your library is not listed above: http://www.dotnetwise.com/Code/Externs/index.html

Looking through contrib/externs/chrome_extensions.js the answer to my question seems to be: You can't.

Looks like everything (or at least the parts you are calling into) need to be explicitly defined in the externs file, to be certain no renaming is performed.

Upvotes: 1

Related Questions