2

I have this function on an object that I need to trace REALLY BADLY, along with the parent caller of the invocation and arguments passed to the caller.

This well works until minified:

var foo = {
    FunctionToBeLogged: function GiveMeAName() {
        console.log('> %s called from %s - args: %o',
                    arguments.callee.name,
                    arguments.callee.caller.name,
                    arguments.callee.caller.arguments);
  }
}

var bar = {
  A: function A(something) {
    foo.FunctionToBeLogged('nothing', 12, true);
  },  
  B: function B(whatever, doesntMatter) {
    foo.FunctionToBeLogged('nothing', 12, true);
  }
}

bar.A(1.2, 'Fred', { });    // > GiveMeAName called from A - args: [1.2, "Fred", Object]
bar.B('Barney', 42, false); // > GiveMeAName called from B - args: ["Barney", 42, false]

Minification gets rid of the these names and my output becomes:

bar.A(1.2, 'Fred', { });    // >  called from  - args: [1.2, "Fred", Object]
bar.B('Barney', 42, false); // >  called from  - args: ["Barney", 42, false]

I really don't want to go and create function declarations and assignments because I have TONS of them (I inherited this code with 7,564... and I can easily run some regex sub to name the function expressions.)

What can I do to prevent the minifier to get rid of my these function names?

Fernando Espinosa
  • 4,625
  • 1
  • 30
  • 37
  • Run the code un-minified....? Don't develop on production machines, and don't minify on development machines. – Ryan Apr 26 '16 at 18:25
  • Hahaha.. sorry, I forgot to say: We can deploy unminified scripts up to Dev and Integration environments, but we need to deploy all JS scripts minified starting from our Internal-QA environment and subsequent phases. We need to keep tracing these calls up to Staging. – Fernando Espinosa Apr 26 '16 at 18:31
  • what are you using to minify the code? – omarjmh Apr 26 '16 at 18:35

1 Answers1

2

To achieve this you can pass specific names to not be mangled, for example in UglifyJS:

To avoid that, you can use --reserved-file to pass a filename that should contain the names to be excluded from mangling

In that file you would have a list of names you do not want to be changed like so:

{
  "vars": [ "define", "require", ... ],
  "props": [ "length", "prototype", ... ]
}

Uglify Mangle options docs...

omarjmh
  • 13,632
  • 6
  • 34
  • 42