I'm working on a single-file script that is meant to be included on pages through a <script>
tag, it's being built with Vite.js using the Terser minifier.
After making changes I noticed that the built version of my script was suddenly throwing errors whenever I called Google Analytics code.
After doing some digging, I noticed one of the other packages on a site I was including the script on was calling a function called ga()
. The error is happening because Terser is minifying/mangling a function in my script and naming it ga()
, which then conflicts with this other function I have no control over.
I assumed Terser would have an option to either
- not mangle to a specific name
- prefix all mangled functions
But it doesn't seem to have either.
I've managed to fix the problem by adding the following to my config:
minify: "terser",
terserOptions: {
keep_fnames: true,
},
Which stops Terser from mangling ANY function names, but obviously this isn't great as it's wasting a ton of potential for minification.
Is there any way to tell Terser to still mangle all functions, but to do so while also prefixing it with a_
for example?
ga()
, which is the problem I'm having. – Gilboa