1

I have recently switched from using a hand written compile script and Google Closure Compiler to using Brunch, where I am more or less forced into using UglifyJS.

I have the config options

uglify:
  mangle: 
    toplevel: true
    eval: true
    functions: true
  compress: true

My classes use the prototype style of being built.

ie.

function Car() { }

Car.prototype.startEngine = function() { }

and then (c is in global scope and called once)

var c = new Car();
car.startEngine();

Adding the toplevel:true config option mangles Car, but none of the prototype functions (ie. startEngine) are mangled.

I would like to not change my class structure, but can I get UglifyJS to mangle these function names?

Thanks :)

adam
  • 22,404
  • 20
  • 87
  • 119

2 Answers2

0

Uglify doesn't have the capability to mangle object properties. But you might be able to get closer to what you're looking for by switching to bracket notation with a variable.

function Car() { }

var startEngine = 'startEngine'; // could actually be any random string or number
Car.prototype[startEngine] = function() { }

var c = new Car();
car[startEngine]();

This relies on your first snippet and second one both having access to the startEngine variable, so depending on your project structure you might have to redeclare the variable in an equivalent way or come up with other creative ways to preserve its value across scopes.

The answers at Mangle nested classes and variables with uglifyjs provide more detailed explanations.

Community
  • 1
  • 1
es128
  • 1,047
  • 9
  • 11
0

Yes, now you can. You just need to modify your code a little. See my answer here.

Community
  • 1
  • 1
Fernando Fabreti
  • 4,277
  • 3
  • 32
  • 33