Create-react-app: React component names mangling

Created on 26 Feb 2018  Â·  12Comments  Â·  Source: facebook/create-react-app

Is this a bug report?

Yes. We aren't able to make automation testing.

Did you try recovering your dependencies?

Yes

Environment

  1. node -v: 9.4.0
  2. npm -v: 5.6.0
  3. yarn --version (if you use Yarn): 1.3.2
  4. npm ls react-scripts (if you haven’t ejected): [email protected]

Then, specify:

  1. Operating system: Windows 10
  2. Browser and version (if relevant):

Steps to Reproduce

  1. create-react-app
  2. yarn build
  3. Open in browser → React DevTools

Expected Behavior

We would like to see unminified component's class names for our staging environment to make end-to-end automated testing. We use these names in our test cases, so we don't want to see them mangled.

Actual Behavior

Class names are mangled.

image

It's easy to fix by adding options to disable mangling in webpack.config.prod.js:

new webpack.optimize.UglifyJsPlugin({
    // ...
    mangle: {
        keep_classnames: true,
        keep_fnames: true
    },
    // ...
}),

Maybe adding the option to .env file is good way here?

Most helpful comment

One thing you can do is to explicitly give some components displayName. Minifier won't remove it.

function Stuff() {}
Stuff.displayName = 'Stuff'

Giving it to every component is overkill and will bloat your bundle. But if you strategically pick a few dozen components (depending on your product size) I think it will give you a good balance.

All 12 comments

[email protected] you're on the wrong repo! 😄

I believe react-scripts-ts is in sync with react-scripts. Actually, if I create app based on react-scripts I'll have exactly same problem.

/cc @wmonk

Oh, I'm sorry -- I misread this. I thought this was the development environment.

What you called "staging environment" is generated using our production build, which is always going to mangle class names in interest of file size.
Offering tunability in yarn build is a slippery slope, so I don't think we'll be exploring it.

Something which built by build server and pubished somewhere on a server. I don't ask to disable it, I would ask to give us an option to disable it.

@Timer @wmonk so, any update on this? I don't want to eject an app just because of one line in webpack config :-)

I don't believe this is a good option to add. Regarding you exact issue I suggest you use data tags to identify your components. Class names - even before minification like this existed - were inherently bad for identification due to the unfixed and subjective nature of naming. Using something like data-test-id="header-button" is preferable as it shows intent, and is unlikely to be refactored away.

Just reread your original post and not sure my comment solves your issue? What are you using the Component Class names for?

We have our own component hierarchy. The idea was to use component's class name to find it in tree. As for me data attributes looks like a hack.
Why do you think it isn't a good option to add? I believe each user of CRA should be able decide about resulting bundle shape.

@Timer @wmonk It seems the issue won't be fixed?

No, sorry. Giving these sort of configuration options are outside of our values.
If you feel strongly about this, you'll need to eject.

One use case where this kind of functionality would be helpful in production is for componentStack error handling in componentDidCatch when sourcemaps are not available client side (e.g. when using Sentry for server side sourcemapping).

As it is, the componentStack is unusable in such cases. @Timer

One thing you can do is to explicitly give some components displayName. Minifier won't remove it.

function Stuff() {}
Stuff.displayName = 'Stuff'

Giving it to every component is overkill and will bloat your bundle. But if you strategically pick a few dozen components (depending on your product size) I think it will give you a good balance.

Was this page helpful?
0 / 5 - 0 ratings