Search code examples
javascriptuglifyjs

Why uglifyjs doesn't remove dead code?


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?


Solution

  • Despite this question has already got an accepted answer, I think it's worth mentioning that

    1. UglifyJS2 does remove dead code

    2. To turn this feature on, you need to set appropriate option either in CLI (uglifyjs --compress unused,dead_code) or in the options object if you invoke uglify programmatically (uglify(compress: { unused: true, dead_code: true });).