I have some code with a UMD pattern like this:
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.returnExports = factory();
}
}(this, function () {
function sum(a, b)
{
return a + b;
}
return { sum: sum};
}));
$('body').text(returnExports.sum(32,3));
And I want to compile this code with the Google Closure Compiler with the ADVANCED_OPTIMIZATIONS
flag. I'm not allowed to change to code above. When I compile this code, the Closure Compiler doesn't recognize that root.returnExports
equals window.returnExports
and compiles the code into this:
function a() {
return {
b: function(b, c) {
return b + c
}
}
}
"function" === typeof define && define.c ? define([], a) : "object" === typeof module && module.a ? module.a = a() : a();
$("body").text(returnExports.b(32, 3));
which of course throws an error as returnExports
(line 9) is not defined. The jQuery function $('body').text(...);
is declared in an extern file, so that's not the problem.
I saw there was another question about Closure Compiler and UMD pattern: Google closure compiler and UMD pattern , but that didn't help me.
How can I tell the Closure Compiler that root.returnExports
equals window.returnExports
?
NOTE:
I want returnExports
to be renamed to something like a
or b
, instead of keeping it's name.