-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Prevent PropTypes.js from showing up in production builds #4046
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
Comments
It's for devs who use For example if you grab import { PropTypes } from 'react-router'
class App extends React.Component {
//...
}
App.contextTypes = {
router: PropTypes.routerContext
}; |
Perhaps I'm misunderstood, I was under the impression that proptypes are useless in production? |
That's true they're stripped in production but still - whilst in development, dev's might want the prop types, like in the example above |
I'm wondering about production though - why is the file included in production bundles? |
Oh sorry I misunderstood, good point. |
I suppose it might be because if a dev does not strip It would basically add a requirement for devs to have to strip |
If a dev wants to keep the proptypes, they can manually include them. The proptypes should still be removed by default. |
I agree it should be encouraged. Given this code: import { PropTypes } from 'react-router'
class App extends React.Component {
//...
}
App.contextTypes = {
router: PropTypes.routerContext
}; This will be fine in development, however when the dev does a production build //...
if(__DEV__) {
App.contextTypes = {
router: PropTypes.routerContext
};
} That's what you mean right? |
Or the inverse, where devs specifically include proptypes like: Note that making this change would add an almost 10% win to the final build size of RR! |
@ryanflorence @timdorr Thoughts? |
We should always have a The problem is that even when you strip if (__DEV__) {
import { PropTypes } from 'react'
Router.propTypes = {
// ...
}
} We currently use |
|
I would like to add that I really like the propTypes in the package. It has been extremely helpful to import it and use instead of hand declaring propTypes for context. |
@kwelch I'm aware of the benefits of having propTypes during development. I'm advocating for production builds only. |
Well, I do think this is a worthwhile problem to solve. If you do solve it you'll be a hero for the whole React community. I'd imagine just about everyone has this problem. |
Add in this transform plugin, maybe? |
That won't keep the file from being included @timdorr |
Not sure if you guys are willing to make the change but I think rollup can treeshake those away. I think I saw @ryanflorence a couple days ago that he may use rollup to get some quick wins to shrink build size on twitter. Switching to rollup can help with that. In any case switching to rollup may not be ideal because I'm not sure exactly about how you guys orchestrate your build for react-router. |
The way I see it, there are a few options here:
export const matchContext = if (process.env.NODE_END !== 'production') ? PropTypes.shape({
addMatch: PropTypes.func.isRequired,
removeMatch: PropTypes.func.isRequired
}) : {} |
Proptypes serve two purposes. import { propTypes as rrPropTypes } from 'react-router'
const withRouter = (Component) => {
const WrappedComponent = (props, context) => (
<Component {...props} router={context.router}/>
)
WrappedComponent.contextTypes = {
router: rrPropTypes.routerContext.isRequired
}
return WrappedComponent
} Maybe |
FWIW: if you use webpack DefinePlugin you can strip out conditional expressions based on macro-like free vars. new DefinePlugin({ENV: myNodeEnvVarObtainedInConfig}) The following would get stripped if in your code if the ENV was equal to "prod" if ( ENV != 'prod') {
const PropTypes = require('prop-types');
} |
@TheLarkInn Am I correct in assuming there is no way to do a conditional |
It's a spec requirement. imports (and exports) only exist at the top level of a module. Also, babel enforces it at the parsing level. No way around it. |
I originally opened this issue with a question:
The answer it seems is yes, this file is required in production. While propTypes per se aren't used in production, this library makes extensive use of context, which are defined in the same way as propTypes. Hence these "propTypes" are in fact necessary during production. We will have to wait for better tree shaking and property mangling for greater gains. The only thing we can do in the meantime is to rename some properties to be slightly shorter. See #4083 for more. |
Yes, that is correct the spec prevents it. This could change with |
It seems react-router/PropTypes.js is being includes in builds where NODE_ENV=='production'. Is there a need for this file in production?
The text was updated successfully, but these errors were encountered: