简体   繁体   English

闭包编译器,缺少具有高级优化功能的方法?

[英]Closure Compiler, missing method with advanced optimizations?

I'm using the closure compiler in an angularjs application. 我在angularjs应用程序中使用闭包编译器。 My JS compiles without errors (or warnings) and works fine with SIMPLE optimizations. 我的JS编译时没有错误(或警告),并且可以通过SIMPLE优化正常工作。 Specifically, I have the following warnings/checks enabled: 具体来说,我启用了以下警告/检查:

--jscomp_warning=checkTypes \
--jscomp_error=checkVars \
--jscomp_warning=deprecated \
--jscomp_error=duplicate \
--jscomp_warning=globalThis \
--jscomp_warning=missingProperties \
--jscomp_warning=undefinedNames \
--jscomp_error=undefinedVars \

However, when I try to compile using ADVANCED OPTIMIZATIONS, I get the following error: 但是,当我尝试使用ADVANCED OPTIMIZATIONS进行编译时,出现以下错误:

TypeError: a.handleEvent is not a function
    at Sj.Nh.a.(anonymous function).a.(anonymous function) (http://localhost:10080/main/pattern.dots-0-7-7-258310cc.compiled.js:118:273)
    at $h (http://app.js:120:424)
    at R (http://app.js:119:337)
    at lj (http://app.js:144:380)
    at Sj.f.re (http://app.js:151:622)
    at mo (http://app.js:302:171)
    at to (http://app.js:316:78)
    at link (http://app.js:308:335)
    ...

which appears to be related to event handling code (from looking at the source map): 它似乎与事件处理代码有关(通过查看源映射):

/**
 * @param {Object|Function} listener The listener function or an
 *     object that contains handleEvent method.
 * @return {!Function} Either the original function or a function that
 *     calls obj.handleEvent. If the same listener is passed to this
 *     function more than once, the same function is guaranteed to be
 *     returned.
 */
goog.events.wrapListener = function(listener) {
  goog.asserts.assert(listener, 'Listener can not be null.');

  if (goog.isFunction(listener)) {
    return listener;
  }

  goog.asserts.assert(
      listener.handleEvent, 'An object listener must have handleEvent method.');
  if (!listener[goog.events.LISTENER_WRAPPER_PROP_]) {
    listener[goog.events.LISTENER_WRAPPER_PROP_] =
        function(e) { return listener.handleEvent(e); };
  }
  return listener[goog.events.LISTENER_WRAPPER_PROP_];
};

But this seems really odd. 但这似乎真的很奇怪。 After all, there's an assert a couple lines up that (apparently) passes. 毕竟,有一个断言(显然)通过了一对声明。 I think I've tried to keep the assert ( -D goog.asserts.ENABLE_ASSERTS ) and even if the assert is optimized away, I don't understand why it would work with SIMPLE optimizations (where the assert would still be present). 我已经尝试保留断言( -D goog.asserts.ENABLE_ASSERTS ),即使断言被优化了,我也不明白为什么它可以与SIMPLE优化一起工作(断言仍然存在)。 Also, the code still works if I compile with advanced optimizations and --debug , which looks like it starts the process of namespace collapsing, but doesn't go all the way. 另外,如果我使用高级优化和--debug编译,代码仍然可以正常工作,这看起来像是开始了命名空间崩溃的过程,但并没有完全结束。

Interestingly enough, if I try adding some console.log statements: 有趣的是,如果我尝试添加一些console.log语句:

goog.events.wrapListener = function(listener) {
  goog.asserts.assert(listener, 'Listener can not be null.');

  if (goog.isFunction(listener)) {
    return listener;
  }

  goog.asserts.assert(
      listener.handleEvent, 'An object listener must have handleEvent method.');
  if (!listener[goog.events.LISTENER_WRAPPER_PROP_]) {
    listener[goog.events.LISTENER_WRAPPER_PROP_] =
        function(e) {
          console.log(e);
          console.log(listener);
          console.log(typeof listener);
          console.log(listener.handleEvent);
          console.log(typeof listener.handleEvent);
          return listener.handleEvent(e);
        };
  }
  return listener[goog.events.LISTENER_WRAPPER_PROP_];
};

And I see that the typeof listener is 'function' . 而且我看到typeof listener'function' But if that's the case, how did we get here in the first place? 但是,如果真是这样,我们首先是如何到达这里的? (certainly goog.isFunction(listener) should have returned true at that point ...). (当然goog.isFunction(listener)应该在那时返回true了。)。

I'm a bit at a loss as to what might be going on here... 我对这里可能发生的事情有点不知所措...

Well, It seems that after countless hours of trying to debug this one, the answer falls into my lap right after I post the question (again). 好吧,似乎在尝试调试了无数小时之后,答案再次出现在我的膝盖上(再次)。

I was unlucky enough to have goog.isFunction compile down to the symbol ga -- which conflicts with the global usage tracking library "Google Analytics". 不幸的是,我无法将goog.isFunction编译为符号ga ,它与全局使用情况跟踪库“ Google Analytics(分析)”相冲突。

The solution for me was to just include the universal_analytics_api.js in my externs and all seems to be well with the world. 对我来说,解决方案是在我的外部环境中仅包含universal_analytics_api.js ,这一切似乎都与世隔绝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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