jQuery noop causing JS minify to remove code in .NET Framework 4.8 web app

149 Views Asked by At

When I publish my .NET app, one of my JS files doesn't minify correctly. I have several hundred lines of code in the file but end up with a near empty function after the process has completed. I have gone through it and determined it is down to a $.noop that I use, without it the process works fine. To demonstrate this I have broken it down into a simple example that shows how it affects the file.

var MyApp = {};
MyApp.EmailPopup = (function () {
    function Test() {
        // do lots of jquery stuff
        alert('hi');
    }

    var thisObject = {
        Show: $.noop
    };

    thisObject.Show = function () {
        Test();
    };

    return thisObject;
})();

When minified the call to Test is removed as shown:

var MyApp={};MyApp.EmailPopup=function(){return{Show:$.noop}}();

However if I remove the $.noop function and add an empty function instead like so:

var MyApp = {};
MyApp.EmailPopup = (function () {
    function Test() {
        // do lots of jquery stuff
        alert('hi');
    }

    var thisObject = {
        Show: function () { } // this has changed
    };

    thisObject.Show = function () {
        Test();
    };

    return thisObject;
})();

Then I get the desired minified version:

var MyApp={};MyApp.EmailPopup=function(){return{Show:function(){alert("hi")}}}();

In the real app, by it not including the equivalent Test function I am losing hundreds of lines of code. Can someone explain why using $.noop prevents it from working, but initialising to an empty function or null works? It is a .NET 4.8 web application, it uses jQuery 3.3.1 and I build it with Visual Studio 2019.

0

There are 0 best solutions below