Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set 'constructor.name' to the uglified function name instead of 't'. #1429

Closed
ludbek opened this issue Jan 19, 2017 · 16 comments
Closed

Set 'constructor.name' to the uglified function name instead of 't'. #1429

ludbek opened this issue Jan 19, 2017 · 16 comments

Comments

@ludbek
Copy link

@ludbek ludbek commented Jan 19, 2017

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

@avdg

This comment has been minimized.

Copy link
Contributor

@avdg avdg commented Jan 19, 2017

code example would be nice

@ludbek

This comment has been minimized.

Copy link
Author

@ludbek ludbek commented Jan 30, 2017

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.

@ludbek ludbek closed this Jan 30, 2017
@ludbek ludbek reopened this Jan 30, 2017
@kzc

This comment has been minimized.

Copy link
Contributor

@kzc kzc commented Jan 30, 2017

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.

@ludbek

This comment has been minimized.

Copy link
Author

@ludbek ludbek commented Jan 30, 2017

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.

@kzc

This comment has been minimized.

Copy link
Contributor

@kzc kzc commented Jan 30, 2017

--keep-fnames will work just fine.

@ludbek

This comment has been minimized.

Copy link
Author

@ludbek ludbek commented Jan 30, 2017

--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.?

@kzc

This comment has been minimized.

Copy link
Contributor

@kzc kzc commented Jan 30, 2017

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

@ludbek

This comment has been minimized.

Copy link
Author

@ludbek ludbek commented Jan 30, 2017

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

@kzc

This comment has been minimized.

Copy link
Contributor

@kzc kzc commented Jan 30, 2017

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.

@kzc

This comment has been minimized.

Copy link
Contributor

@kzc kzc commented Jan 30, 2017

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

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

This comment has been minimized.

Copy link
Author

@ludbek ludbek commented Feb 20, 2017

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

@ludbek ludbek closed this Feb 20, 2017
@or2008

This comment has been minimized.

Copy link

@or2008 or2008 commented Nov 22, 2017

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

@kzc

This comment has been minimized.

Copy link
Contributor

@kzc kzc commented Nov 22, 2017

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

@or2008

This comment has been minimized.

Copy link

@or2008 or2008 commented Nov 22, 2017

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.

@ludbek

This comment has been minimized.

Copy link
Author

@ludbek ludbek commented Nov 23, 2017

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

@msqaddura

This comment has been minimized.

Copy link

@msqaddura msqaddura commented Dec 18, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.