-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Dead code eliminate React.PropTypes checkers in production #6401
Conversation
To clarify, I understand that we already don’t check |
Seems reasonable. 👍 @facebook-github-bot shipit |
TODO before merging: check if new propTypes have been added. (e.g. #6377) |
@gaearon updated the pull request. |
I'm not convinced we should do this. It makes our API a lie and if we're going to do something like this, we should consider just removing PropTypes entirely in prod. I'm not convinced that's a good idea either though. In an ideal world I'd say we just let the code get removed by dead code removal in a packager but leave our API alone. Not sure that's a real option right now though. |
Let's make it throw always? Maybe we could first warn too by passing a secret extra argument to every checker and warning if it isn't present (because that means you called the checker directly). |
Make all proptype checkers throw? Would we try/catch that in dev? |
Or I guess you're saying if the extra arg is there we return the error but if it's not we throw the error we would have returned? |
As a temporary step: pass an extra fifth arg to every checker, warn when it's not passed (because that means people are calling PropTypes checkers manually and may break after the change). Permanently: always throw if any PropTypes checker is called in prod. Then you at least notice if your code is calling PropTypes in prod. |
Warning seems less dangerous; less likley to change the flow of control in unexpected ways. |
The point is to change the control flow if you're using it in an unexpected way. See Sebastian's XSS example. |
Let’s close and maybe revisit this some time later since there’s no clear conclusion. |
@gaearon This is going to be the case for almost every issue. The key is to push through to a conclusion if you think it's important. There's a plausible way forward here. @spicyj's proposal seems reasonable. We're not stalled and there are no pending dependencies so I'd suggest keep it open or an issue open so that a third party contributor can push it through. |
@gaearon wouldn't removing |
@bloodyowl it will. Anyway removing |
This PR doesn’t “remove” PropTypes—it makes them no-ops in terms of validation. So this PR doesn’t make any difference for context API. |
I filed #7131 as the first step to this. |
Resurrected in #7651. |
I was looking if we can shave off some kilobytes here and there and noticed that we ship
PropTypes
code that we never actually run in production. I tried replacing it with a noop function with identical API and it seems to save 933 bytes off the gzippedreact.min.js
and 1035 bytes off the gzippedreact-with-addons.min.js
:(The dev builds grew larger though, as we branch on
__DEV__
now.)As far as I can see this would only break tools that rely on introspection of PropTypes in the prod build but I don’t think if we ever supported this usage scenario anyway. Is there any other reason that I missed why this might be a bad idea?