microsoft / ajaxmin

The Microsoft Ajax Minifier enables you to improve the performance of your Ajax applications by reducing the size of your Cascading Style Sheet and JavaScript files.
https://www.nuget.org/packages/AjaxMin
MIT License
32 stars 21 forks source link

Does Ajaxmin support removal of dead code? #3

Closed petrosmm closed 5 years ago

petrosmm commented 5 years ago

In various projects I have worked on, developers leave a lot of dead code in the comments mostly for debugging purposes, however that code get's transported and it does not get removed... Before writing here, I came across this issue with @Taritsyn of the WebMarkUpMin project as well as NUglify JS minifer project. Does ajaxmin support removing dead code -- the 'if (false' statements)?

seankeating commented 5 years ago

It doesn't appear it has the support for removing dead code from if statements like: if (false) { ... }

But it does support removing unused functions.

Here is the test I ran with the latest release (v5.14): Test JavaScript:

function foo() {
    if (false) {
      console.log("I'll never be true.");
    }
    console.log("I'll always be used.");
    function unused() {
      console.log("I'm unused.");
    }
}

AjaxMinifer Command:

AjaxMinifier.exe deadCodeTest.js -out deadCodeTest.min.js

Output:

function foo(){!1&&console.log("I'll never be true.");console.log("I'll always be used.")}
seankeating commented 5 years ago

I think what you are looking for is called "Tree Shaking": https://webpack.js.org/guides/tree-shaking/

I have not tested to verify.

seankeating commented 5 years ago

I tested with roolup.js which supports Tree Shaking.

Test Code:

(function foo() {
    if (false) {
      console.log("I'll never be true.");
    }

    console.log("I'll always be used.");

    function unused() {
      console.log("I'm unused.");
    }
})();

rollup command

rollup deadCodeTest.js --file deadCodeTest.min2.js --format iife

Output:

(function () {
    'use strict';

    (function foo() {
        console.log("I'll always be used.");
    })();
}());
petrosmm commented 5 years ago

Hmm interesting... I have never heard that term before and I will look into it. Thank you!