Developing a Chrome extension involves working with the scripts background.js
and popup.js
.
Within background.js
:
function foo(){
// Performs some action
}
In popup.js
:
var backgroundPage = chrome.extension.getBackgroundPage();
backgroundPage.foo();
If these two files are compiled separately, the backgroundPage.foo()
reference will be broken as 'foo' is renamed differently in both background.js
and popup.js
.
The proper way to address this is by exporting foo
in background.js
using window["foo"] = foo;
, yet avoiding exports for code obfuscation.
An attempt was made to compile both scripts in one run and split them into separate output files using --module
:
java -jar compiler.jar
--compilation_level ADVANCED_OPTIMIZATIONS--js background.js popup.js
--module backgroundMin:1
--module popupMin:1:backgroundMin
However, this method failed as the compiler couldn't recognize that backgroundPage
in popup.js
corresponds to window
in background.js
. Various annotations like @lends
, @borrows
, and @memberOf
were attempted but didn't yield successful results.
Hence, the question remains: Can both background.js
and popup.js
be compiled so that foo()
is renamed without exporting it or breaking the reference from popup.js
?