3

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?

lpetrucci
  • 1,285
  • 4
  • 22
  • 40

1 Answers1

0

Yes you can do it with regex, You can check this post or go to mangle.properties for complete documentation.

terserOptions: {
    mangle: {
        properties: {
            regex: /(^P1|^p1|^_p1)[A-Z]\w*/
        }
    }
}
Alex
  • 676
  • 9
  • 20
AIRGG
  • 13
  • 4