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

Make it easier to properly profile with the React DevTools. #7734

Closed
kentcdodds opened this issue Sep 25, 2019 · 10 comments · Fixed by #7737
Closed

Make it easier to properly profile with the React DevTools. #7734

kentcdodds opened this issue Sep 25, 2019 · 10 comments · Fixed by #7737

Comments

@kentcdodds
Copy link
Contributor

I wrote a blog post about how to properly profile a React App for Performance and I use a react-scripts app to demonstrate how to do it. There are a few steps in there that I'd love to make easier.

Specifically, you have to go into ./node_modules/react-scripts/config/webpack.config.js to update the webpack config to add this to resolve.alias:

'react-dom$': 'react-dom/profiling',
'scheduler/tracing': 'scheduler/tracing-profiling',

And you have to add this to the Terser options:

keep_classnames: true,
keep_fnames: true,

It's a bit of an annoying process and I think we could make it a lot easier (and hopefully encourage people to profile their apps in the process).

Describe the solution you'd like

We'd make a change here:

alias: {
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
'react-native': 'react-native-web',
},

-      alias: {
+      alias: removeEmpty({
         // Support React Native Web
         // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
         'react-native': 'react-native-web',
+        'react-dom$': isEnvProductionProfile ? 'react-dom/profiling' : null,
+        'scheduler/tracing': isEnvProductionProfile ? 'scheduler/tracing-profiling' : null,
+      }),
-      },

Where removeEmpty is something like (borrowed from webpack-config-utils):

function removeEmpty(input) {
  const output = {}
  Object.keys(input).forEach(key => {
    const value = input[key]
    if (typeof value !== 'undefined') {
      output[key] = value
    }
  })
  return output
}

and isEnvProductionProfile is defined as: isEnvProduction && Boolean(process.env.PROFILE_APP).

We'd also add this right above:

keep_classnames: isEnvProductionProfile,
keep_fnames: isEnvProductionProfile,

Describe alternatives you've considered

Currently the alternative is to change it manually (and if you deploy from your local build then you need to remember to remove your edits).

Additional context

I'm bringing this up because I'd love to update my blog post to simply be: "Now, run the build in profiling mode with npx cross-env PROFILE_APP=true npm run build." That would be awesome. And it would make every-day profiling much simpler as well.

@kentcdodds
Copy link
Contributor Author

By the way, I don't really care exactly how this is implemented. If you don't like the environment variable/removeEmpty function/etc, that's fine. I just want to make this easier.

@JacobMGEvans
Copy link
Contributor

Are those steps to implementing this? Could we just make a PR then?

@kentcdodds
Copy link
Contributor Author

I think so!

@jamesb3ll
Copy link

jamesb3ll commented Sep 25, 2019

Instead of removeEmpty function, spread operator could be used:

alias: {
  'react-native': 'react-native-web',
  ...(isEnvProductionProfile && {
    'react-dom$': 'react-dom/profiling',
    'scheduler/tracing': 'scheduler/tracing-profiling',
  }),
}

@JacobMGEvans
Copy link
Contributor

JacobMGEvans commented Sep 26, 2019

Instead of removeEmpty function, spread operator could be used:

alias: {
  'react-native': 'react-native-web',
  ...(isEnvProductionProfile && {
    'react-dom$': 'react-dom/profiling',
    'scheduler/tracing': 'scheduler/tracing-profiling',
  }),
}

Would any type safety be lost? Or would that short circuit be handling that logically equivalent?

@JacobMGEvans
Copy link
Contributor

I started to implement this here is the draft PR: #7737
Currently, I have an issue with the keep_classnames and I am guessing keep_fnames might have some issues.
Here is the current compiler error:
yarn run v1.17.3
$ cd packages/react-scripts && node bin/react-scripts.js build
Creating an optimized production build...
Failed to compile.

Failed to minify the bundle. Error: static/js/main.b21b40a1.chunk.js from Terser
`keep_classnames` is not a supported option
    at /Users/jacobevans/personalProjects/create-react-app/packages/react-scripts/scripts/build.js:176:23
    at finalCallback (/Users/jacobevans/personalProjects/create-react-app/node_modules/webpack/lib/Compiler.js:257:39)
    at /Users/jacobevans/personalProjects/create-react-app/node_modules/webpack/lib/Compiler.js:273:13
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/jacobevans/personalProjects/create-react-app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/jacobevans/personalProjects/create-react-app/node_modules/tapable/lib/Hook.js:154:20)
    at onCompiled (/Users/jacobevans/personalProjects/create-react-app/node_modules/webpack/lib/Compiler.js:271:21)
    at /Users/jacobevans/personalProjects/create-react-app/node_modules/webpack/lib/Compiler.js:681:15
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/jacobevans/personalProjects/create-react-app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/jacobevans/personalProjects/create-react-app/node_modules/tapable/lib/Hook.js:154:20)
    at /Users/jacobevans/personalProjects/create-react-app/node_modules/webpack/lib/Compiler.js:678:31
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/jacobevans/personalProjects/create-react-app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/jacobevans/personalProjects/create-react-app/node_modules/tapable/lib/Hook.js:154:20)
    at /Users/jacobevans/personalProjects/create-react-app/node_modules/webpack/lib/Compilation.js:1423:35
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/jacobevans/personalProjects/create-react-app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/jacobevans/personalProjects/create-react-app/node_modules/tapable/lib/Hook.js:154:20)
    at /Users/jacobevans/personalProjects/create-react-app/node_modules/webpack/lib/Compilation.js:1414:32
Read more here: https://bit.ly/CRA-build-minify

@kevin940726
Copy link
Contributor

@JacobMGEvans I left the comment in the PR :)

@JacobMGEvans
Copy link
Contributor

JacobMGEvans commented Sep 26, 2019

@kevin940726 Pointing out I had the keep_classnames and keep_fnames inside of the
output: { ... } rather than outside of it in the scope of the terserOptions: { ... }

@kentcdodds works as you expected now, I just misread your direction for that part of the implementation.

@kentcdodds
Copy link
Contributor Author

Huzzah!!!! Thanks @iansu! And well done @JacobMGEvans 👏

I understand that my needs are not your deadlines, but if this could be released for my workshop by next Monday that would be awesome 😁

@iansu
Copy link
Contributor

iansu commented Oct 3, 2019

@kentcdodds It's released now!

@lock lock bot locked and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants