Uglifyjs: Set 'constructor.name' to the uglified function name instead of 't'.

Created on 19 Jan 2017  路  16Comments  路  Source: mishoo/UglifyJS

Uglify converts every constructor.name to t instead of setting them to corresponding uglified function name.

Most helpful comment

I would say it would really be really good to add this 'default behavior' as a note in the readme or options sections as it felt like a heisenbug for me as it occured. I came by this thread as i was trying to find a solution to get the ctor names ( keep_name flags ).

maybe it is really worth mentioning in the readme as not all developers would use uglify for development and only use it for production and then they got surprised like i did :D

All 16 comments

code example would be nice

Suppose I have following two classes.

class Button {
  someMethod () {
    // this without uglify returns "Button"
    // but after uglify returns "t"
     console.log(this.constructor.name);
  } 
}

class Modal {
  someMethod () {
    // this without uglify returns "Modal"
    // but after uglify returns "t"
     console.log(this.constructor.name);
  } 
}

After above code goes throught babel and uglify in webpack. Every constructor.name is turned to t.

Constructor functions have no special significance to Uglify once transpiled to ES5 code. If you want to preserve function names search for --keep-fnames and keep_fnames in the README.

I am not asking to keep the original name but to keep the mangled constructor name. Technically its a bug to strip off the mangled name. obj.constructor.name should be populated with the mangled name.

--keep-fnames will work just fine.

--keep-fnames will keep names of all the functions even if they are not constructors. I want the function name to be mangled and assign the mangled name to obj.constructor.name. Is there any particular reason obj.constructor.name is not mangled name of constructor.?

Uglify is unaware that the ES5 code it is processing was once ES6.

So you mean if the constructors had been written in ES5 the constructor names would have been populated correctly?

ES5 has no notion of constructors. They're just functions.

The flag mentioned above will solve your problem. If you're not satisfied with that, perhaps babili would better suit your needs.

As an alternative to keep_fnames you can also specify which symbol names not to mangle:

uglifyjs -c -m 'except=["Button","Modal"]'

Sorry, Uglify works fine. It has to do with Babel. Closing this :)

@ludbek
I have a similar issue, how did you solve it with babel?

See keep_fnames and keep_classnames in https://github.com/mishoo/UglifyJS2/blob/harmony/README.md

Thanks @kzc,

I am familiar with keep_fnames, actually I am currently using it and it works great.
I just saw ludbek's comment and thought that the proper solution is to config babel somehow.. but I guess I didn't read the comment right.

@or2008 I have not found the solution. I simply use keep_fname as the work around.

I would say it would really be really good to add this 'default behavior' as a note in the readme or options sections as it felt like a heisenbug for me as it occured. I came by this thread as i was trying to find a solution to get the ctor names ( keep_name flags ).

maybe it is really worth mentioning in the readme as not all developers would use uglify for development and only use it for production and then they got surprised like i did :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexlamsl picture alexlamsl  路  4Comments

lhtdesignde picture lhtdesignde  路  3Comments

utdrmac picture utdrmac  路  4Comments

diegocr picture diegocr  路  3Comments

alexlamsl picture alexlamsl  路  5Comments