trullock / NUglify

NUglify is a HTML, JavaScript and CSS minification Library for .NET (fork of AjaxMin + new features)
Other
396 stars 79 forks source link

functions pruned when exported in an object literal #382

Open davethieben opened 9 months ago

davethieben commented 9 months ago

version: 1.20.7 (through LigerShark.WebOptimizer v3.0.396)

Describe the bug if RemoveUnneededCode = true, local functions are being pruned even they are referenced from an object literal export

To Reproduce invoking:

    var uglifyResult = NUglify.Uglify.Js(@"
function abc(def){
    alert(def);
}

export default { abc };
");
    Debug.WriteLine($"result:\r\n{uglifyResult.Code}");

Minified output or stack trace

export default{abc:n}

Excepted output code

function n(n){alert(n)}export default{abc:n}

which is what you get if you set RemoveUnneededCode = false

trullock commented 9 months ago

Good find

Not got a lot of time to look at this but PRs welcome.

It should be relatively easy to trace this though, look where RemoveUnneededCode ends up getting piped, it will be to one of the Visitors after JsParser has run. The code the checks if a particular AST node is needed or not is not paying attention to exports, or rather exports are not declaring their references in a way that the remover pays attention to