javascripthtmlminifyterserhtml-minifier

How to mangle function names used in event attributes with html-minifier-terser?


I am using the latest https://github.com/terser/html-minifier-terser 7.0.0 (https://www.npmjs.com/package/html-minifier-terser)

The readme states

Minify JavaScript in script elements and event attributes (uses Terser)

I am trying the following minifyJS options in attemt to "mangle" function names across the whole HTML:

{
    compress:{drop_console:true},
    mangle:{toplevel:true},
    nameCache:{}
}

However, while the script function is mangled to n(), the corresponding <tag on...="javascriptfunction();"> attribute event handler does not get mangled, resulting in "Uncaught ReferenceError: ... is not defined"

What am I missing?


Solution

  • It works if the function definition comes first (i.e. script before body).
    Workarounds: