如何使用Google Closure API删除调试代码?

时间:2015-01-23 13:07:42

标签: javascript google-closure-compiler

The Google Closure documentation(查找ENABLE_DEBUG var)解释了如何通过将标志--define='ENABLE_DEBUG=false'传递给编译器来更改变量。 Google Closure API不支持define选项。同时,the wiki表示REST服务(和API?)支持debug选项。但它如何在代码中使用?创建变量并没有帮助,它保持不变:

/** @define {boolean} */
var debug = true;

2 个答案:

答案 0 :(得分:0)

如果您正在使用Closure Library支持,则可以包含CLOSURE_DEFINES的定义以及您发送给编译器的源的重置。

要查看此操作,您可以执行以下操作:

http://closure-compiler.appspot.com/home

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @use_closure_library true
// ==/ClosureCompiler==

var CLOSURE_DEFINES = {
  'FOO' : true
}

/** @define {boolean} */
var FOO = false;
if (FOO) {
  alert('me');
}

这导致:

alert("me");

答案 1 :(得分:0)

你可以做什么的例子:

/** @define {boolean} */
var MY_DEBUG = false;

if (MY_DEBUG){
    debuga = function(myparam, myparam2){
        console.log(myparam, myparam2);
        // Big debug stuff..
    };
    debugb = function(myparam, myparam2){
        console.log(myparam, myparam2);
        // Big debug stuff..
    };
}
else{
    debuga = function(myparam, myparam2){};
    debugb = function(myparam, myparam2){};
}

debuga("Hello","World");

您可以使用定义MY_DEBUG

控制--define='MY_DEBUG=false'的值

它会产生类似这样的结果

// SIMPLE_OPTIMIZATION            // ADVANCED_OPTIMIZATION
var DEBUG=!1;                     debuga=function(){};
debuga=function(a,b){};           debugb=function(){};
debugb=function(a,b){};           debuga("Hello","World");
debuga("Hello","World");
相关问题