terser / html-minifier-terser

actively maintained fork of html-minifier - minify HTML, CSS and JS code using terser - supports ES6 code
https://terser.org/html-minifier-terser
MIT License
376 stars 30 forks source link

terser option mangle fails on / breaks document with several scripts #35

Closed gnbl closed 4 years ago

gnbl commented 4 years ago

When enabling JavaScript mangling in terser (e.g. --minify-js {"compress":{"drop_console":"true"},"mangle":{"toplevel":"true"}} my page breaks (TypeError: e is not a function).

Test case:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>title</title>
    <script>
        function functiona(param1) {
            return functionb(param1);
        }
    </script>
</head>
<body>
    Some text in body.
    <div id="target">target</div>
    <script>
        function functionb(param2) {
          return param2;
        }
        document.addEventListener("OnDOMContentLoaded", function(){
            console.log("loaded");
            var test = functiona(undefined);
        });
    </script>
</body>
</html>

turns into

<!doctype html>
<html lang=en>
<meta charset=UTF-8>
<meta name=viewport content="width=device-width,initial-scale=1">
<title>title</title>
<script>
    function n(n) {
        return functionb(n)
    }
</script>Some text in body.<div id=target>target</div>
<script>
    function n(n) {
        return n
    }
    document.addEventListener("OnDOMContentLoaded", (function () {
        functiona(void 0)
    }))
</script>

It seems html-minifier-terser minifies these individually, rather than in the same "namespace" - correct? This of course also would explain the TypeError I am seeing with my actual HTML page.

Is there an option to prevent this? If not, and this is not in the scope of the project, is there a workaround?


For reference

gnbl commented 4 years ago

Ah, it looks like nameCache does the trick.