-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Remove propTypes in production #1322
Conversation
This will help reduce the bundle size impact. Note that any consumers who are depending on any react-dates components having a `.propTypes` property will no longer work as expected after this change. I enabled this using wrap mode, which adds `process.env.NODE_ENV` checks, which is a pretty standard method for React apps. I believe this will allow this library to have propTypes in development, but for them to be minified out in production builds. To make this safe to enable in this repo, I enabled the react/forbid-foreign-prop-types ESLint rule, which was written specifically for the benefit of this plugin. Here's a diff showing what effect this has on the esm build: https://gist.github.com/lencioni/1b436d365570394ccde659c829ba02c3
.babelrc
Outdated
"production": { | ||
"presets": ["airbnb"], | ||
"plugins": [ | ||
"inline-react-svg", | ||
["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], | ||
["transform-react-remove-prop-types", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this something we could add in babel-preset-airbnb, possibly behind an option, instead of in individual repos?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I think so!
I think we'd still need to make it configurable though, since in some places we want the mode to be remove instead of wrap for instance, and other options might be important to manage on a case by case basis, but we could just have a passthrough option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, a passthrough option sounds great
I originally started by adding this plugin to react-dates, but it seems like we are going to want this in a lot of places, so it makes sense to put it into our preset instead. react-dates/react-dates#1322 For the default options, I decided to go with "wrap" as the mode, since we are likely to use this more frequently in packages that are published and consumed by other packages and apps, which aren't very compatible with the "remove" option. In our apps where this is not a concern, we will want to override this to use the "remove" option in production and likely disable this plugin entirely in development for better build speeds.
I originally started by adding this plugin to react-dates, but it seems like we are going to want this in a lot of places, so it makes sense to put it into our preset instead. react-dates/react-dates#1322 For the default options, I decided to go with "wrap" as the mode, since we are likely to use this more frequently in packages that are published and consumed by other packages and apps, which aren't very compatible with the "remove" option. In our apps where this is not a concern, we will want to override this to use the "remove" option in production and likely disable this plugin entirely in development for better build speeds.
This new version includes an option for removing propTypes, so I am removing that plugin in favor of using the preset option.
@ljharb I just published the new preset with the removePropTypes option and updated this PR to use that instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM But we should treat this as a semver major, just in case
Agreed! @majapw feel free to merge this when you think it makes sense for the next major. |
This will help reduce the bundle size impact. Note that any consumers
who are depending on any react-dates components having a
.propTypes
property will no longer work as expected after this change.
I enabled this using wrap mode, which adds
process.env.NODE_ENV
checks, which is a pretty standard method for React apps. I believe this
will allow this library to have propTypes in development, but for them
to be minified out in production builds.
To make this safe to enable in this repo, I enabled the
react/forbid-foreign-prop-types ESLint rule, which was written
specifically for the benefit of this plugin.
Here's a diff showing what effect this has on the esm build:
https://gist.github.com/lencioni/1b436d365570394ccde659c829ba02c3