1

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.

Murphybro2
  • 2,207
  • 1
  • 22
  • 36
  • Looks like a bug in minify.js to me. What's the reason to initialize the `thisObject` with `$.noop` and override that later in the first place? If you have all the information that allows you to decide whether you want to use `$.noop` or something else right during the construction of the function, decide it then and there, and not a couple of lines later. – Tomalak Jun 16 '21 at 16:58
  • I know it's not necessary. It was done to easily highlight all the functions that will be available for use in a busy "class". I can easily get round it, but at this point I was just interested if there was a reason :) – Murphybro2 Jun 16 '21 at 19:09
  • 1
    As I said, it looks like a bug in minifyjs to me. A different minimizer probably does better. – Tomalak Jun 16 '21 at 19:15

0 Answers0