WebMarkupMin icon indicating copy to clipboard operation
WebMarkupMin copied to clipboard

NUglifyJsMinifierFactory with RenamePairs does not consitently rename properties and methods of JS classes

Open MarcelVersteeg opened this issue 4 months ago • 6 comments

Consider JavaScript like this:

class MyClass
{
    constructor(field)
    {
        this.field = field;
    }

    method()
    {
        field++;
    }

    get property()
    {
        return field;
    }
}

var myVar = new MyClass();
myVar.method();
var property = myVar.property;

Then I configure the WebMarkupMin minification as follows (irrelevant options left out for brevity):

WebMarkupMinServicesBuilder minificationBuilder = services.AddWebMarkupMin(options =>
        {
            ...
        });

// Add the HTML minification
minificationBuilder.AddHtmlMinification(options =>
        {
            options.JsMinifierFactory = new NUglifyJsMinifierFactory(new NUglifyJsMinificationSettings
                    {
                        LocalRenaming = LocalRenaming.CrunchAll,
                        PreserveFunctionNames = false,
                        PreserveImportantComments = false,
                        QuoteObjectLiteralProperties = false,
                        RemoveFunctionExpressionNames = true,
                        RemoveUnneededCode = true,
                        RenamePairs = @"MyClass=a,field=b,method=c,property=d,myVar=e",
                        ReorderScopeDeclarations = true,
                        StrictMode = false,
                        StripDebugStatements = true,
                        TermSemicolons = false,
                        WarningLevel = int.MaxValue
                    });
        });

Now, the resulting uglified JavaScript will become (indentation kept for readability):

class a
{
    constructor(b)
    {
        this.b = b;
    }

    method()
    {
        b++;
    }

    get property()
    {
        return b;
    }
}

var e = new a();
e.c();
var d = e.d

This code does not run correctly, as the actual method and property in the class are not renamed, but the locations where they are called are renamed.

Preferrably, the methods and properties of the class are also renamed (just like the fields), but at least the renaming should be consistent to not break the uglified JavaScript

MarcelVersteeg avatar Oct 01 '24 14:10 MarcelVersteeg