簡體   English   中英

使用Google Closure Compiler可以從編譯版本中排除一部分源代碼嗎?

[英]Using Google Closure Compiler can you exclude a section of source code from the compiled version?

我最近使用Dojo工具包構建了一個項目,並且喜歡如何將一段代碼標記為僅基於任意條件檢查包含在編譯版本中。 我用它來導出私有變量進行單元測試或拋出錯誤而不是記錄它們 這是Dojo格式的一個例子,我很想知道Google Closure Compiler是否有這樣的特殊指令。

window.module = (function(){

  //private variable
  var bar = {hidden:"secret"};

  //>>excludeStart("DEBUG", true);
    //export internal variables for unit testing 
    window.bar = bar;
  //>>excludeEnd("DEBUG");

  //return privileged methods
  return {
    foo: function(val){
      bar.hidden = val;
    }
  };
})();

編輯

關閉權威指南提到您可以擴展CommandLineRunner以添加您自己的檢查和優化,這可能是一種方法。 Plover看起來很有前途,因為它支持自定義傳遞

這個簡單的測試案例有效。 使用--define DEBUG=false編譯

/**
 * @define {boolean} DEBUG is provided as a convenience so that debugging code
 * that should not be included in a production js_binary can be easily stripped
 * by specifying --define DEBUG=false to the JSCompiler. For example, most
 * toString() methods should be declared inside an "if (DEBUG)" conditional
 * because they are generally used for debugging purposes and it is difficult
 * for the JSCompiler to statically determine whether they are used.
 */
var DEBUG = true;

window['module'] = (function(){

  //private variable
  var bar = {hidden:"secret"};

  if (DEBUG) {
    //export internal variables for unit testing 
    window['bar'] = bar;
  }

  //return privileged methods
  return {
      foo: function(val){
        bar.hidden = val;
      }
  };
})();

console.log(window['bar']);
module.foo("update");
console.log(window['bar']);

Closure Compiler支持“定義”,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM