简体   繁体   中英

Error when compiling code with Google Closure Compiler

I'm compiling my code with the Google Closure compiler.

The code I am compiling is the relevant parts from the closure library, Hammer, Hammer-Time and some of my own code.

The compilation works until I add Hammer to the compilation, at which point when I run my JavaScript I get the following error:

Uncaught TypeError: n.add is not a function

which refers to:

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++
    }
}

If I rename the add function in Hammer I do not get this problem but I was wondering if there was an alternative to that?

The full closure compiler command is:

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

Hammer.js is written with JSDoc comments which are close to but not fully compatible with the comments used by Google Closure Compiler. I'm guessing this is the source of your error but it would take more debugging to be sure. See this page about Debugging Compiled Code if you still want to try to compile Hammer.js along with your code. I suspect you would need to modify the Hammer.js code somewhat to get it to work with Closure Compiler.

The other option is to create an "extern" file that tells Closure Compiler the API for Hammer. After compiling your code with Closure Compiler, the calls to Hammer are left unminified, and you would load Hammer (the already minified version found on the Hammer website) on the target HTML page before loading your code. See How do I write an externs file? .

One example of how the doc comments differ between JSDoc and Closure Compiler is: JSDoc uses @param {Number} whereas Closure Compiler regards that as a reference to a wrapper type and uses @param {number} for the primitive number type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM