I am compiling the following code with ADVANCED_OPTIMIZATIONS using Google Closure Compile :
(function() {
/** @const */
var DEBUG = false;
var Namespace = {};
window['Namespace'] = Namespace;
(function() {
/**
* @constructor
*/
function Test(tpl) {
Helper.debug('Test');
}
Namespace['Test'] = Test;
})();
var Helper =
(function(){
/**
* @constructor
*/
function Helper(){
this.debug = function(arg){
if(DEBUG){
console.log(arg);
}
}
};
return new Helper;
})();
})();
My intention was for the compiler to strip all Helper.debug messages when DEBUG == false
, and to rename the debug function to a short name when DEBUG == true
. I'm hoping for something like this from the compiler:
DEBUG == false
:
var a={};window.Namespace=a;a.Test=function(){};
DEBUG == true
:
var a={};window.Namespace=a;a.Test=function(){console.log("Test")};
I end up with this instead:
DEBUG == false
:
var a={};window.Namespace=a;a.Test=function(){b.debug("Test")};var b=new function(){this.debug=function(){}};
DEBUG == true
:
var a={};window.Namespace=a;a.Test=function(){b.debug("Test")};var b=new function(){this.debug=function(c){console.log(c)}};
In neither case is the debug
function renamed. I figure it should be, since it is not exported, nor accessible (as far as I can tell) from Namespace
. It's only called from the Namespace.Test()
constructor. If I don't call it from there, Closure strips the debug function (because it is not used anywhere), but I want to be able to call it through the functions in Namespace, and still have it be renamed.
I've tried various versions of the above code. Using prototype.debug on Helper
, moving the Helper constructor to the same scope as Namespace, etc. As long as the debug function is attached to my Helper object though, I can't find a way to get my desired output from the compiler.
If I don't use the Helper object, and just declare debug
as a function, I get exactly my desired output, however this is just an example and I really have many functions which are attached to the Helper object and I would like them all to be renamed to short names. Example code which gives me my desired output:
(function() {
/** @const */
var DEBUG = false;
var Namespace = {};
window['Namespace'] = Namespace;
(function() {
/**
* @constructor
*/
function Test(tpl) {
debug('Test');
}
Namespace['Test'] = Test;
})();
function debug(arg){
if(DEBUG){
console.log(arg);
}
}
})();