Hi,
I've been using UglifyJS2 for quite some time, and it's a wonderful tool, but I recently came accross the following case :
var log = function (message) {
if (DEBUG) {
console.log(message);
}
};
log("something I'd like to show only on debug");
Here, I log using the console, but the real case is more complex, and I don't use the console.
Anyway, in this example, when compressed with DEBUG=false the log function is indeed empty, but the call to log("...") is still here.
It would be great if all calls to empty functions would be removed, as well as the function itself (being empty, it does nothing, return nothing, so it should be pretty safe to remove those :p)
Use compress option? -c
Yes, of course. Using the small provided sample you can see by yourself that the call to log is not removed, even though the function is empty (with DEBUG=false of course)
Edit: Copying the provided sample in a test.js file and then running uglifyjs test.js --compress --define DEBUG=false --output test.out.js will produce the following code :
var log=function(message){};log("something I'd like to show only on debug");
As you can see, the function is empty, the call is here and does absolutely nothing. This optimization would be reaaally usefull (at least to me :p) to completely strip development related functionalities from production code with just a define.
I see what you mean.
if (DEBUG) {
log = function(message) {
console.log(message);
}
}
DEBUG && log("something I'd like to show only on debug");
Thanks for the suggestion, but as I wrote in my first message (where I also wrote that I _compressed_) : my use case is more complex than the provided example, and this trick cannot be applied easily.
Also, writing everywhere DEBUG && somefunction(...); is a bit less 'agreable' to the eye than just somefunction(...). And if I have more complex defines patterns, I can end up with things like !DEBUG && PROFILE && .... which is error prone and difficult to read and maintain.
I'm not really asking for help. I currently use those kind of tricks. This ticket is a feature request :)
Yeah, I see the value in your suggestion.
:+1:
+1!!! I need this!
Though the whole --define option doesn't quite work because it doesn't override what's in the script, generally I'd be setting DEBUG to false so that my logger function would then be empty. UglifyJS2 could then remove the logger function and its calls.
Who can we tag to take action on this? This isn't the first time it was requested/suggested:
Any news about this feature, more than a year later? AFAIK it has not been implemented?
$ cat -n log.js
1 (function(){
2 var log = function (message) {
3 if (DEBUG) {
4 console.log(message);
5 }
6 };
7 console.log("foo");
8 log("something I'd like to show only on debug");
9 console.log("bar");
10 })();
$ uglifyjs log.js -c pure_funcs="'[log]'" --define DEBUG=false
WARN: Condition always false [null:1,6]
WARN: Dropping unreachable code [log.js:4,12]
WARN: Dropping side-effect-free statement [log.js:8,4]
WARN: Dropping unused variable log [log.js:2,8]
!function(){console.log("foo"),console.log("bar")}();
The IIFE is necessary to get rid of the top level var log. JS bundlers like browserify and webpack employ them in packaging up JS files.
Closing as existing solution is available.
Is the solution pure_funcs? @alexlamsl
Most helpful comment
The IIFE is necessary to get rid of the top level
var log. JS bundlers likebrowserifyandwebpackemploy them in packaging up JS files.