For instance, I have the following code:
if ("a" !== "a") {
console.log('really?');
}
var a = 5;
Then I write uglifyjs code.js -o code.min.js
. As a result, I have the following:
if("a"!=="a"){console.log("really?")}var a=5;
How do I enable removing the dead code inside the if-statement?
For instance, I have the following code:
if ("a" !== "a") {
console.log('really?');
}
var a = 5;
Then I write uglifyjs code.js -o code.min.js
. As a result, I have the following:
if("a"!=="a"){console.log("really?")}var a=5;
How do I enable removing the dead code inside the if-statement?
Share Improve this question asked Aug 20, 2015 at 4:41 user2991036user2991036 4215 silver badges9 bronze badges 4 |2 Answers
Reset to default 16Despite this question has already got an accepted answer, I think it's worth mentioning that
UglifyJS2
does remove dead codeTo turn this feature on, you need to set appropriate option either in CLI (
uglifyjs --compress unused,dead_code
) or in theoptions
object if you invokeuglify
programmatically (uglify(compress: { unused: true, dead_code: true });
).
As per the README for uglifyjs, the maintainer has shifted development effort to UglifyJS2. The README also says that it only removes:
some unreachable code and warn about it (code that follows a return, throw, break or continue statement, except function/variable declarations).
Uglify2 does a more comprehensive job. I tested your code on the demo site and it does indeed remove the whole if statement. It also supports 'conditional compilation' (or maybe more correctly conditional code removal) by allowing you to define globals at the command line when uglifying.
console.log
? or the entire if block? The former is a perfectly valid code with perfectly valid purpose. It is very different than optimizingif(a == 1 || a == 1)
. Plus I think uglifier does very limited optimization. – CppLearner Commented Aug 20, 2015 at 4:48