Remove unnecessary React propTypes from the production build.
npm install --save-dev babel-plugin-transform-react-remove-prop-types
Remove React propTypes
from the production build, as they are only used in development.
You can save bandwidth by removing them.
In
const Baz = (props) => (
<div {...props} />
);
Baz.propTypes = {
className: PropTypes.string
};
Out
const Baz = (props) => (
<div {...props} />
);
The majority of cases should be addressed by default by this plugin.
In some cases, for example when using HOCs (High Order Components), like react-redux's connect
, or component inheritance (although it's NOT recommended), a comment after the propTypes
definition may be used to force the removal:
Component.propTypes /* remove-proptypes */ = {}
.babelrc
without options:
{
"env": {
"production": {
"plugins": ["transform-react-remove-prop-types"]
}
}
}
with options:
{
"env": {
"production": {
"plugins": [
["transform-react-remove-prop-types", {
"mode": "wrap",
"ignoreFilenames": ["node_modules"]
}]
]
}
}
}
babel --plugins transform-react-remove-prop-types script.js
without options:
require('babel-core').transform('code', {
plugins: [
'transform-react-remove-prop-types',
],
});
with options:
require('babel-core').transform('code', {
plugins: [
[
'transform-react-remove-prop-types',
{
mode: 'wrap',
ignoreFilenames: ['node_modules'],
},
],
],
});
remove
(default): thepropTypes
definitions are removed from the source code.wrap
: thepropTypes
definitions are wrapped with the following code:
Component.propTypes = process.env.NODE_ENV !== "production" ? {
// ...
} : {};
Accessing Component.propTypes.className
won't throw. It's a tradeoff between the size of the output file and the likelihood libraries like react-native-hyperlink breaks.
unsafe-wrap
: thepropTypes
definitions are wrapped with the following code:
if (process.env.NODE_ENV !== "production") {
Component.propTypes = {
// ...
}
}
Accessing Component.propTypes.className
will throw.
The wrap modes are targeting React libraries like material-ui or react-native-web. They are not intended to be used by application authors.
true
: the import statements are removed as well. This option only works ifmode
is set toremove
:
import PropTypes from 'prop-types'
false
(default): does not remove the import statements.
This filter generates a regular expression. Any filenames containing one of the array's strings will be ignored. By default, we match everything.
Following the Is it safe? section, you might encounter a component
depending on the propTypes
at runtime to work.
For this reason, we provide an array options to filter out some files and folders.
For instance, you can ignore all the npm modules:
ignoreFilenames: ['node_modules'],
This option gives the possibility to remove other propTypes
in addition to the canonical prop-types
.
For instance, by default
import PropTypes from 'prop-types'
import ImmutablePropTypes from 'react-immutable-proptypes'
will result in the latter not to be removed, while with:
additionalLibraries: ['react-immutable-proptypes'],
both will be removed.
Use this option to enable this plugin to run on components that extend a class different than React.Component
or React.PureComponent
.
Given this example:
class MyComponent extends BaseComponent {
...
}
You would use:
classNameMatchers: ["BaseComponent"]
If you are using the propTypes
in a conventional way,
i.e by using them to perform type checking on the properties, that plugin should be safe to use.
However, some libraries are accessing the propTypes
on the component directly.
For instance react-native-vector-icons use them to split the properties between two components:
const touchableProps = pick(restProps, Object.keys(TouchableHighlight.propTypes));
TouchableHighlight.propTypes
.
Make sure you are:
- Not using that pattern in your source code.
If you do, explicitly export the
propTypes
to work around that limitation. - Not parsing the
node_modules
. If you do, test that your code is still working before shipping into production.
eslint-plugin-react has a rule forbid-foreign-prop-types that can help you make this plugin safer to use.
MIT