![](/img/trans.png)
[英]Google Closure Compiler - Dead code removal based on externs
[英]Debug code removal using Google Closure Compiler
如果通過高級優化運行以下代碼,我仍然可以在代碼中看到調試語句。
var log = console.info.bind(console);
(function() {
/** @const */
var DEBUG = false;
log('Brady', createRank({
max: 100,
debug: DEBUG
}));
})();
function createRank(options) {
if (options.debug) {
log('This should be in debug mode only');
}
if(typeof alert == 'function'){
alert(options);
}
return (Math.random() * options.max) | 0;
}
高級模式編譯后輸出
(function() {
var a = console.info.bind(console),
b = {
max: 100,
debug: !1
};
b.debug && a("This should be in debug mode only");
"function" == typeof alert && alert(b);
a("Brady", Math.random() * b.max | 0);
})();
如何使用高級模式擺脫調試消息?
如果將DEBUG變量定義為全局變量,並且將記錄語句括起來,例如
如果(DEBUG){log('debug message'); }
那么它將起作用,但是如果我們不希望將其用作全局變量,而是通過參數將值傳遞給各個模塊/函數,則有辦法使其起作用。
這是當前優化集及其運行時間的限制。 優化是在編譯時間和優化之間進行權衡的,做出的選擇不一定適合每種代碼模式。
在這種特殊情況下,問題在於“屬性崩潰”對於全局作用域僅發生一次,也就是在函數內聯發生之前(針對功能局部對象的“屬性崩潰”在主優化循環中發生)。 為了刪除示例中的代碼,“折疊屬性”至少需要再次運行一次,或者需要增強函數本地版本(更為保守)才能在全局范圍內運行。
此處也對此進行了討論: https : //github.com/google/closure-compiler/issues/891
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.