Closure Compiler,缺少高级优化方法?
Closure Compiler, missing method with advanced optimizations?
我在 angularjs 应用程序中使用闭包编译器。我的 JS 编译没有错误(或警告),并且可以通过 SIMPLE 优化正常工作。具体来说,我启用了以下 warnings/checks:
--jscomp_warning=checkTypes \
--jscomp_error=checkVars \
--jscomp_warning=deprecated \
--jscomp_error=duplicate \
--jscomp_warning=globalThis \
--jscomp_warning=missingProperties \
--jscomp_warning=undefinedNames \
--jscomp_error=undefinedVars \
但是,当我尝试使用高级优化进行编译时,出现以下错误:
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)
...
这似乎与事件处理代码有关(通过查看源映射):
/**
* @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_];
};
但这看起来真的很奇怪。毕竟,有一个 assert a couple lines up(显然)通过了。我 认为 我试图保留断言 (-D goog.asserts.ENABLE_ASSERTS
),即使断言被优化掉了,我也不明白为什么它会与 SIMPLE 优化一起工作(其中断言仍然存在)。此外,如果我使用高级优化和 --debug
进行编译,代码仍然有效,这看起来像是开始了命名空间折叠的过程,但并没有一直进行下去。
有趣的是,如果我尝试添加一些 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_];
};
我看到 typeof listener
是 'function'
。但如果是这样的话,我们最初是如何到达这里的呢? (当然 goog.isFunction(listener)
应该在那个时候返回 true
...)。
我有点不知所措...
好吧,似乎在尝试调试这个问题无数个小时之后,答案就在我 post 问题(再次)之后落到了我的腿上。
我很不幸 goog.isFunction
编译成符号 ga
-- 这与全局使用跟踪库 "Google Analytics".
冲突
我的解决方案是在我的外部实习生中加入 universal_analytics_api.js
,一切似乎都很好。
我在 angularjs 应用程序中使用闭包编译器。我的 JS 编译没有错误(或警告),并且可以通过 SIMPLE 优化正常工作。具体来说,我启用了以下 warnings/checks:
--jscomp_warning=checkTypes \
--jscomp_error=checkVars \
--jscomp_warning=deprecated \
--jscomp_error=duplicate \
--jscomp_warning=globalThis \
--jscomp_warning=missingProperties \
--jscomp_warning=undefinedNames \
--jscomp_error=undefinedVars \
但是,当我尝试使用高级优化进行编译时,出现以下错误:
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)
...
这似乎与事件处理代码有关(通过查看源映射):
/**
* @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_];
};
但这看起来真的很奇怪。毕竟,有一个 assert a couple lines up(显然)通过了。我 认为 我试图保留断言 (-D goog.asserts.ENABLE_ASSERTS
),即使断言被优化掉了,我也不明白为什么它会与 SIMPLE 优化一起工作(其中断言仍然存在)。此外,如果我使用高级优化和 --debug
进行编译,代码仍然有效,这看起来像是开始了命名空间折叠的过程,但并没有一直进行下去。
有趣的是,如果我尝试添加一些 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_];
};
我看到 typeof listener
是 'function'
。但如果是这样的话,我们最初是如何到达这里的呢? (当然 goog.isFunction(listener)
应该在那个时候返回 true
...)。
我有点不知所措...
好吧,似乎在尝试调试这个问题无数个小时之后,答案就在我 post 问题(再次)之后落到了我的腿上。
我很不幸 goog.isFunction
编译成符号 ga
-- 这与全局使用跟踪库 "Google Analytics".
我的解决方案是在我的外部实习生中加入 universal_analytics_api.js
,一切似乎都很好。