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

Allow React component displayName and propTypes to be removed in production builds #51

Closed
re-thc opened this issue Aug 25, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@re-thc
Copy link

re-thc commented Aug 25, 2020

Hi, can proptypes be removed from the production builds? Saves ~3kb. It's good for development but would be better if it's not exported out. Thanks.

Can be done via a babel plugin.

@jaydenseric
Copy link
Owner

jaydenseric commented Aug 26, 2020

Yes, it is a good idea to avoid prop-types in production bundles, but the actual elimination of it should happen when a user builds their project; we don't publish separate development and production versions of graphql-react. We should however make sure what we publish works well with tooling such as Webpack via Next.js.

I had just assumed that Next.js eliminates prop-types in production builds for both project code and dependencies, but I do wonder if maybe that's not true - it would be good to test it out.

To be sure, we should conditionally define propTypes on components via a process.env.NODE_ENV !== 'production' condition, like how other packages such as react-redux does it:

https://github.com/reduxjs/react-redux/blob/f0124d71f959224fd1aebc02d50ce0069e58c5e5/src/components/Provider.js#L36-L37

I'm not a fan of using process.env.NODE_ENV because that won't work without a bundler; process is undefined in a browser. Maybe there should be a more complicated typeof process !== 'undefined' && process.env.NODE_ENV check.

@jaydenseric jaydenseric added the enhancement New feature or request label Aug 26, 2020
@jaydenseric
Copy link
Owner

So, this would be a breaking change for anyone that doesn't have a bunder that deals with process being undefined in browsers (or any non-Node.js environment really):

Screen Shot 2020-09-17 at 1 24 09 pm

Although it's a little more verbose, this is not a breaking change:

Screen Shot 2020-09-17 at 1 23 13 pm

Here they are compared in a Next.js production build:

Screen Shot 2020-09-17 at 1 28 37 pm

The propTypes are successfully eliminated for both, although the second option causes slightly more complicated code for some reason beyond my understanding.

@jaydenseric jaydenseric changed the title Remove PropTypes from production build Allow propTypes to be removed in production builds Sep 17, 2020
@jaydenseric jaydenseric changed the title Allow propTypes to be removed in production builds Allow React component displayName and propTypes to be removed in production builds Sep 17, 2020
@jaydenseric
Copy link
Owner

This improvement has been published in v11.1.0 🚀

denisp22 added a commit to denisp22/graphql-react that referenced this issue Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants