Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent UglifyJS from changing function arguments' names

When deploying my app on a test server, I came with an error where UglifyJS did change a npm dependency module's functions arguments, where the names of these arguments are important; some functions are called dynamically and I am using function-arguments to map certain values to certain function arguments based on their corresponding names.

Is it possible to tell UglifyJS to skip certain modules, or files, etc.?

like image 596
Yanick Rochon Avatar asked Apr 15 '26 13:04

Yanick Rochon


1 Answers

According to the documentation you can use mangle options to for that:

  • mangle (default true) — pass false to skip mangling names, or pass an object to specify mangling options

Object options:

  • except - pass an Array of identifiers that should be excluded from mangling
  • toplevel — mangle names declared in the toplevel scope (disabled by default).
  • eval — mangle names visible in scopes where eval or with are used (disabled by default).
  • keep_fnames -- default false. Pass true to not mangle function names. Useful for code relying on Function.prototype.name.

//tst.js
var globalVar;
function funcName(firstLongName, anotherLongName)
{
  var myVariable = firstLongName +  anotherLongName;
}

UglifyJS.minify("tst.js").code;
// 'function funcName(a,n){}var globalVar;'

UglifyJS.minify("tst.js", { mangle: { except: ['firstLongName'] } }).code;
// 'function funcName(firstLongName,a){}var globalVar;'

UglifyJS.minify("tst.js", { mangle: { toplevel: true } }).code;
// 'function n(n,a){}var a;'

I tried to find an option to mangle the function names and skip mangling the arguments and didn't fun that so far (not saying there isn't...)

like image 57
Dekel Avatar answered Apr 17 '26 02:04

Dekel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!