NUglify icon indicating copy to clipboard operation
NUglify copied to clipboard

Question: can I exclude parts from nuglify analysis

Open MarcelVersteeg opened this issue 9 months ago • 3 comments

Hi,

This is not directly a bug, but more a question.

In my web project I am using WebMarkupMin to minimize my html output. All html will always contain all required JacaScript and CSS in the same file. There will be no external links to code files requested by the browser. The composition of the entire self-contained document is done upon request, and separate files on my server are inserted in locations in the HTML document. This entire composed self-contained document is then passed to NUglify via WebNarkupMin. I do this because of the following reasons (in order of decreasing importance):

  1. Most of my code files contain placeholders that are filled with actual data only known when a request is made for a document. This way the same script can be used in different situations where the script itself can be kept simple and clean.
  2. All globals in the entire composed script can be uglified and still work, because no “public interface” needs to be preserved.
  3. I don’t need to explicitly wait for all modules to be loaded before I can execute JavaScript code.

In the minification process I also throw exceptions (resulting in an http status 500) when the JavaScript code does not follow some rules. These are reported as warnings by NUglify during the analysis (for example that a parameter of a function is not used). These force me to keep my JavaScript code clean.

In some cases I also include third-party scripts. Of course I cannot (and don’t want) to force them to adhere to my preferred style and some of those modules already come in a minified form. However, the analysis of this code can produce these unwanted warnings during the minification, resulting (in my setup) in an HTTP status 500.

Now my question is: is it possible to exclude specific parts in a large file from producing these warnings, but still take part in the minification process? Or if this can’t be separated, can it be excluded completely, but still be considered as existing globals so the rest of the code knows of the existence? I know the latter could be done by adding these to the global known list of NUglify, btw.

MarcelVersteeg avatar Jun 23 '25 19:06 MarcelVersteeg

Not currently possible, but certainly possible.

You could make some top level global binding exempt from visitation by the various minifying AST node visitors

Shouldnt be that hard, as you say, look how the known globals mechanism works, wont be that different...

trullock avatar Jun 23 '25 21:06 trullock

I think a global binding exempt will be too cumbersome, as some of these third-party scripts have multiple globals defined and also locals should not be minified again.

Therefore, I think it would be better to have some indication around a region of code that needs to be excluded completely, like this:

function UglifyMe1()
{
}

/*NUglify ignore start*/
function DontUglifyMe()
{
}
/*NUglify ignore end*/

function UglifyMe2()
{
}

MarcelVersteeg avatar Jun 24 '25 08:06 MarcelVersteeg

I think that will be harder, but youre welcome to try

trullock avatar Jun 24 '25 13:06 trullock