admin管理员组文章数量:1287648
I recently built a project using the Dojo toolkit and loved how you can mark a section of code to only be included in the piled version based on an arbitrary conditional check. I used this to export private variables for unit testing or to throw errors vs. logging them. Here's an example of the Dojo format, I'd love to know if there are any special directives like this for the 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;
}
};
})();
Edit
Closure the definitive guide mentions that you can extend the CommandLineRunner to add your own Checks and Optimizations that might be one way to do it. Plover looks promising as it supports custom-passes.
I recently built a project using the Dojo toolkit and loved how you can mark a section of code to only be included in the piled version based on an arbitrary conditional check. I used this to export private variables for unit testing or to throw errors vs. logging them. Here's an example of the Dojo format, I'd love to know if there are any special directives like this for the 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;
}
};
})();
Edit
Closure the definitive guide mentions that you can extend the CommandLineRunner to add your own Checks and Optimizations that might be one way to do it. Plover looks promising as it supports custom-passes.
Share Improve this question edited May 5, 2011 at 22:10 SavoryBytes asked May 5, 2011 at 15:54 SavoryBytesSavoryBytes 36.2k4 gold badges54 silver badges61 bronze badges 1- 1 Do you know you can use the Closure Compiler in Advanced Mode with the Dojo Toolkit? Check out my doc: dojo-toolkit.33424.n3.nabble./file/n2636749/… – Stephen Chung Commented May 6, 2011 at 7:04
2 Answers
Reset to default 10This simple test case worked. Compile with --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 supports "defines", like this:
/** @define {boolean} */
var CHANGABLE_ON_THE_COMMAND_LINE = false;
本文标签:
版权声明:本文标题:javascript - Using Google Closure Compiler can you exclude a section of source code from the compiled version? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741279206a2369911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论