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

Multiple classes being mangled to "a" #11868

Open
jashsayani opened this issue Jan 2, 2022 · 1 comment · May be fixed by ayishagisel/create-react-app#119
Open

Multiple classes being mangled to "a" #11868

jashsayani opened this issue Jan 2, 2022 · 1 comment · May be fixed by ayishagisel/create-react-app#119

Comments

@jashsayani
Copy link

Describe the bug

I have a Base class called BaseModel, and multiple sub-classes like User, Foo, Bar, etc. They all extend BaseModel. The BaseModel class has some logic like:

static _instances = {};

constructor(initialData) {
    if (!BaseModel._instances[this.constructor.name]) {
      BaseModel._instances[this.constructor.name] = [];
    }
    BaseModel._instances[this.constructor.name].push(this);
}

This allows me to have a reference to all instances of the class. The problem is that deploying the app renames all sub-classes to "a" (instead of using "a", "b", etc. for different sub-classes). Hence, my _instances map has multiple model class instances under the same key, causing issues.

So I have:

class User extends BaseModel {}
class Foo extends BaseModel {}
class Bar extends BaseModel {}

But in production code, the this.constructor.name is "a" for all of them (User, Foo, Bar).

Did you try recovering your dependencies?

N/A

Environment

  current version of create-react-app: 5.0.0
  running from /Users/jashsayani/.npm/_npx/c67e74de0542c87c/node_modules/create-react-app

  System:
    OS: macOS 12.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 16.13.0 - /usr/local/bin/node
    Yarn: Not Found
    npm: 8.1.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 96.0.4664.110
    Edge: Not Found
    Firefox: Not Found
    Safari: 15.2
  npmPackages:
    react: ^17.0.1 => 17.0.1
    react-dom: ^17.0.1 => 17.0.1
    react-scripts: 4.0.1 => 4.0.1
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

  1. Create class BaseModel and copy constructor from above.
  2. Create sub-classes User, Foo, Bar, etc.
  3. Create instances of User, Foo, Bar, etc. (const f = new Foo();)
  4. Notice that value of this.constructor.name is "a" in constructor (when creating instance of Foo, Bar, etc.) with deploy build code (NOT local server)

Expected behavior

Values of this.constructor.name should be "a", "b", "c", etc. based on different sub-classes

Actual behavior

Creating instances of multiple sub-classes results in this.constructor.name to be "a".

@jashsayani
Copy link
Author

Maybe related: webpack-contrib/uglifyjs-webpack-plugin#394

I see people using eject and overriding webpack config to get around this. I do not want to eject, how can I preserve class names for now? (that seems to be the only solution at this time)

The docs for create-react-app dont mention having a config to override build rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant