简体   繁体   English

闭包编译器:如何将对象及其所有属性声明为extern?

[英]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. 我正在尝试编译使用chrome.i18n.getMessage()和其他几个chrome属性的Google Chrome扩展程序。

I'm compiling using the Java library and have a externs.js file that I am including with the --externs parameters 我正在使用Java库进行编译,并且具有--externs参数包含的externs.js文件。

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? 我想知道是否可以将chrome声明为extern,而不必指定我要保留的所有属性?

I've tried the following 3 approaches so far: 到目前为止,我已经尝试了以下3种方法:

Example 1: 范例1:

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

Example 2: 范例2:

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

Example 3: 范例3:

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

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 . 我使用第三个示例修复了编译后引入的错误,最终在定义越来越多的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. 然后,我的想法是查看是否有人为chrome创建了extern文件,从而将我引到了Closure Compiler的Google来源。 Google has been nice enough to create externs files for several well known libraries: Google足以为几个知名的库创建externs文件:

https://code.google.com/p/closure-compiler/source/browse/#git%2Fexterns https://code.google.com/p/closure-compiler/source/browse/#git%2Fcontrib%2Fexterns 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 如果您的库未在上面列出,则Closure Compiler Externs Extractor也可能会很有用: 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 . 通过contrib/externs/chrome_extensions.js ,我的问题的答案似乎是: 您不能

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. 看起来需要在externs文件中明确定义所有内容(或至少要调用的部分),以确保不执行重命名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM