0

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.

Community
  • 1
  • 1
user7353781
  • 155
  • 2
  • 10
  • This question is an exact duplicate of http://stackoverflow.com/questions/39847332/google-closure-compiler-and-umd-pattern. Why didn't that answer help you? – Chad Killingsworth Mar 29 '17 at 22:16
  • @ChadKillingsworth Because the code still compiled to the code in my question, which is not what I wanted – user7353781 Apr 05 '17 at 14:31

0 Answers0