【问题标题】:Debug code removal using Google Closure Compiler使用 Google Closure Compiler 调试代码删除
【发布时间】:2015-03-30 18:47:04
【问题描述】:

如果我通过高级优化运行以下代码,我仍然可以看到代码中的调试语句。

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 变量被定义为全局变量,并且日志语句被括起来

如果(调试){ 日志('调试消息'); }

那么它会起作用,但是如果我们不希望它作为全局变量,而是通过参数将值传递给各个模块/函数,有没有办法让它起作用。

【问题讨论】:

    标签: javascript gruntjs gulp google-closure-compiler google-closure


    【解决方案1】:

    这是对当前优化集及其运行时间的限制。优化是编译时间和优化之间的权衡,并且所做的选择不一定适合每种代码模式。

    在这种特殊情况下,问题在于“属性折叠”仅在全局范围内发生一次,并且是在函数内联发生之前(函数本地对象的“属性折叠”发生在主优化循环期间)。要删除示例中的代码,“折叠属性”需要至少再运行一次,或者需要增强函数本地版本(更保守)才能在全局范围内运行。

    这也在这里讨论:https://github.com/google/closure-compiler/issues/891

    【讨论】:

    • 看起来这与我们将要回答的问题一样接近
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多