使用Google Closure Compiler编译代码时出错

时间:2016-04-01 18:55:57

标签: javascript google-closure-compiler google-closure hammer.js google-closure-library

我正在使用Google Closure编译器编译我的代码。

我正在编译的代码是闭包库,Hammer,Hammer-Time和我自己的一些代码中的相关部分。

编译工作直到我将Hammer添加到编译中,此时当我运行JavaScript时出现以下错误:

Uncaught TypeError: n.add is not a function

指的是:

function mb(a, c, e, l, f, k) {
    if (!c)
        throw Error("Invalid event type");
    var h = !!f
      , n = nb(a);
    n || (a[ib] = n = new V(a));
    e = n.add(c, e, l, f, k);
    if (!e.b) {
        l = Jb();
        e.b = l;
        l.src = a;
        l.listener = e;
        if (a.addEventListener)
            a.addEventListener(c.toString(), l, h);
        else if (a.attachEvent)
            a.attachEvent(Kb(c.toString()), l);
        else
            throw Error("addEventListener and attachEvent are unavailable.");
        kb++
    }
}

如果我在Hammer中重命名add函数,我不会遇到这个问题,但我想知道是否有替代方案?

完整闭包编译器命令是:

java -jar compiler.jar --js src/js/closure-library/closure/goog/base.js --js src/js/init.js --js src/js/router.js --js src/js/closure-library/closure/goog/debug/error.js --js src/js/closure-library/closure/goog/dom/nodetype.js --js src/js/closure-library/closure/goog/string/string.js --js src/js/closure-library/closure/goog/asserts/asserts.js --js src/js/closure-library/closure/goog/debug/entrypointregistry.js --js src/js/closure-library/closure/goog/array/array.js --js src/js/closure-library/closure/goog/labs/useragent/util.js --js src/js/closure-library/closure/goog/object/object.js --js src/js/closure-library/closure/goog/labs/useragent/browser.js --js src/js/closure-library/closure/goog/labs/useragent/engine.js --js src/js/closure-library/closure/goog/labs/useragent/platform.js --js src/js/closure-library/closure/goog/useragent/useragent.js --js src/js/closure-library/closure/goog/events/browserfeature.js --js src/js/closure-library/closure/goog/disposable/idisposable.js --js src/js/closure-library/closure/goog/disposable/disposable.js --js src/js/closure-library/closure/goog/events/eventid.js --js src/js/closure-library/closure/goog/events/event.js --js src/js/closure-library/closure/goog/events/eventtype.js --js src/js/closure-library/closure/goog/reflect/reflect.js --js src/js/closure-library/closure/goog/events/browserevent.js --js src/js/closure-library/closure/goog/events/listenable.js --js src/js/closure-library/closure/goog/events/listener.js --js src/js/closure-library/closure/goog/events/listenermap.js --js src/js/closure-library/closure/goog/events/events.js --js src/js/closure-library/closure/goog/dom/browserfeature.js --js src/js/closure-library/closure/goog/dom/tagname.js --js src/js/closure-library/closure/goog/dom/tags.js --js src/js/closure-library/closure/goog/string/typedstring.js --js src/js/closure-library/closure/goog/string/const.js --js src/js/closure-library/closure/goog/html/safestyle.js --js src/js/closure-library/closure/goog/html/safestylesheet.js --js src/js/closure-library/closure/goog/fs/url.js --js src/js/closure-library/closure/goog/i18n/bidi.js --js src/js/closure-library/closure/goog/html/safeurl.js --js src/js/closure-library/closure/goog/html/trustedresourceurl.js --js src/js/closure-library/closure/goog/html/safehtml.js --js src/js/closure-library/closure/goog/dom/safe.js --js src/js/closure-library/closure/goog/html/legacyconversions.js --js src/js/closure-library/closure/goog/math/math.js --js src/js/closure-library/closure/goog/math/coordinate.js --js src/js/closure-library/closure/goog/math/size.js --js src/js/closure-library/closure/goog/dom/dom.js --js src/js/closure-library/closure/goog/dom/classes.js --js src/js/app.js --js src/js/hammer.js --js src/js/hammer-time.js --externs=src/js/app-externs.js --compilation_level=ADVANCED_OPTIMIZATIONS

1 个答案:

答案 0 :(得分:1)

Hammer.js是用JSDoc注释编写的,这些注释与Google Closure Compiler使用的注释接近但不完全兼容。我猜这是你的错误的来源,但需要更多调试才能确定。如果您仍想尝试将Hammer.js与代码一起编译,请参阅此页面关于Debugging Compiled Code。我怀疑你需要稍微修改Hammer.js代码才能使它与Closure Compiler一起工作。

另一个选项是创建一个“extern”文件,告诉Closure Compiler Hammer的API。在使用Closure Compiler编译代码之后,对Hammer的调用保持不受限制,您将在加载代码之前在目标HTML页面上加载Hammer(Hammer网站上已经缩小的版本)。请参阅How do I write an externs file?

JSDoc与Closure Compiler之间文档注释的区别的一个示例是:JSDoc使用@param {Number}而Closure Compiler将其视为对包装器类型的引用,并使用@param {number}作为原始数字类型。