question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uglify Mangling Process in `react-scripts build` is Modifying my Application Logic and producing bugs

See original GitHub issue

Is this a bug report?

Possibly. Need help to determine whether or not it is.

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

Uglify, react-scripts build, disable uglify

Environment

System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
    Yarn: 1.10.1 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
  Browsers:
    Chrome: 70.0.3538.67
    Firefox: 62.0
    Safari: 12.0
  npmPackages:
    react: ^16.5.2 => 16.5.2
    react-dom: ^16.5.2 => 16.5.2
    react-scripts: 2.0.3 => 2.0.3
  npmGlobalPackages:
    create-react-app: Not Found

Steps to Reproduce

Unfortunately, I’m on a time crunch so I can’t exactly lay everything out in refined detail. I’m going to try my best to do so in steps based on what I’m experiencing in my application’s production build running on Heroku.

  1. Create a JS map/object whose keys reference the same property name. For example
// ComponentNameAuthorizationMap.js
import ComponentClass1 from '...'
import ComponentClass2 from '...'
import ComponentClass3 from '...'

export const ComponentNameAuthorizationMap = {
   [ComponentClass1.name]: ['authorization1', 'authorization2'],
   [ComponentClass2.name]: ['authorization3'],
   [ComponentClass3.name]: ['authorization2']
}
  1. Import your ComponentNameAuthorizationMap or whatever, into a file in your application.
  2. Execute react-scripts build and run your built application
  3. Put a breakpoint (using your browser’s debugger) on your file that is importing your ComponentNameAuthorizationMap and check the value of that import.
  4. You should see an exported value containing only one key. Not the three that you specified above.

Expected Behavior

My exported object value should not be modified as a result of react-scripts build production optimization.

Actual Behavior

My imported map value was modified and reduced to an object containing only one key.

Reproducible Demo

N/A

Other Comments

From my local observations, I feel like all the name fields that I’m referencing from my imported component classes are being truncated to the same key due to production build optimizations in the react-scripts build process. This then causes the original map to optimize itself down to being a map of only one key-value pair. I was doing some research on this behavior and felt my thoughts were validated by the answer to this SO post:

https://stackoverflow.com/questions/33708197/does-it-make-sense-to-do-both-minify-and-uglify

And I believe the name property exists on all ES6 classes correct? I’m just surprised at this behavior and wondering if anyone else has experienced this in their applications and how to prevent this from happening in CRA2 apps.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Timercommented, Oct 22, 2018

displayName will only exist during development for the dev tools.

edit: actually I’m thinking something else, I’m not sure tbh – give it a try! Since it’s a babel plugin it should probably have the real name, but I’d still advise against this behavior.

1reaction
Timercommented, Oct 22, 2018

i.e.

class ComponentClass2 {
  static NAME = 'ComponentClass2'
}

// later on:
console.log(ComponentClass2.NAME)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing Performance - React
If you're benchmarking or experiencing performance problems in your React apps, make sure you're testing with the minified production build.
Read more >
How to disable Uglify in react build - Stack Overflow
But my reactjs npm run build is still complaining about Uglify. Creating an optimized production build... Failed to compile. static/js/main.
Read more >
uglify-js - npm
One technique is to pass a random number on every compile to simulate mangling changing with different inputs (e.g. as you update the...
Read more >
terser vs uglify-js - npm trends
One technique is to pass a random number on every compile to simulate mangling changing with different inputs (e.g. as you update the...
Read more >
Behind the Scenes: Improving the Repository Infrastructure | React ...
As we worked on React 16, we revamped the folder structure and much of the build tooling in the React repository. Among other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found