js - Closure编译器和UMD

时间:2017-03-27 18:02:45

标签: javascript google-closure-compiler umd

我有一些像这样的UMD模式的代码:

(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));

我想用带有ADVANCED_OPTIMIZATIONS标志的Google Closure Compiler编译此代码。我不允许更改为上面的代码。编译此代码时,Closure Compiler不会识别root.returnExports等于window.returnExports并将代码编译为:

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));

当然会抛出错误,因为returnExports(第9行)未定义。 jQuery函数$('body').text(...);在extern文件中声明,因此不是问题。

我看到有关于Closure Compiler和UMD模式的另一个问题:Google closure compiler and UMD pattern,但这对我没有帮助。

如何判断Closure Compiler root.returnExports等于window.returnExports

注:

我希望将returnExports重命名为ab,而不是保留其名称。

0 个答案:

没有答案
相关问题