Remove unnecessary React propTypes from the production build. balloon

License: MIT

Language: JavaScript

Keywords: babel, babel-plugin, minification, proptypes, react


Remove unnecessary React propTypes from the production build.

npm version npm downloads Build Status

Dependencies DevDependencies


npm install --save-dev babel-plugin-transform-react-remove-prop-types

The problem solved

Remove React propTypes from the production build, as they are only used in development. You can save bandwidth by removing them.



const Baz = (props) => (
  <div {...props} />

Baz.propTypes = {
  className: PropTypes.string


const Baz = (props) => (
  <div {...props} />

With comment annotation

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 */ = {}


Via .babelrc (Recommended)


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

Via Node API

without options:

require('babel-core').transform('code', {
  plugins: [

with options:

require('babel-core').transform('code', {
  plugins: [
        mode: 'wrap',
        ignoreFilenames: ['node_modules'],



  • remove (default): the propTypes definitions are removed from the source code.
  • wrap: the propTypes 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: the propTypes 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 if mode is set to remove:
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.

Regular expressions

If you are using Babel 7 or newer and your config is stored in babel.config.js, you can also use a regular expression to describe modules, which should be removed.

This would be particularly useful when using custom prop types validators, implemented as part of your own source code. For example

import CustomPropTypes from '../../prop-types/my-own-validator'
import OtherCustomPropTypes from '../../prop-types/my-other-validator'

would be removed with the following setting

additionalLibraries: [/\/prop-types\/.*$/]

If you use an index file

import CustomPropTypes from '../../prop-types'

you could set it up as

additionalLibraries: [/\/prop-types$/]


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"]


Use this option to set a custom name for the import of the create-react-class package that is different than createReactClass.

Given this example:

import createClass from 'create-react-class';

You would use:

createReactClassName: 'createClass'

Is it safe?

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));

⚠️ The plugin is breaking that code if it ends up removing 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.



Project Statistics

Sourcerank 20
Repository Size 651 KB
Stars 844
Forks 56
Watchers 12
Open issues 9
Dependencies 552
Contributors 26
Tags 43
Last updated
Last pushed

Top Contributors See all

Nikita Gusakov Joe Lencioni Greenkeeper Fabrizio Castellarin Aziz Khambati Samuel Reed Piotr Kuczynski Tony Quetano Sebastian Silbermann Vlad Zhukov Vikram Rangaraj Oleksandr Fediashov Anthony Ng Carlos Ravelo Luke Page Willem D'Haeseleer Javier Sánchez - Marín Bryan Smith Benjamin Bouvier Jonny Buchanan

Packages Referencing this Repo

Remove unnecessary React propTypes from the production build
Latest release 0.4.24 - Updated - 844 stars
Remove unnecessary static properties of class from the production build
Latest release 0.0.1 - Published - 844 stars

Recent Tags See all

v0.4.24 February 01, 2019
v0.4.23 January 16, 2019
v0.4.22 January 15, 2019
v0.4.21 December 03, 2018
v0.4.20 November 06, 2018
v0.4.19 October 15, 2018
v0.4.18 September 20, 2018
v0.4.17 September 20, 2018
v0.4.16 September 20, 2018
v0.4.15 August 22, 2018
v0.4.14 July 29, 2018
v0.4.13 February 07, 2018
v0.4.12 December 17, 2017
v0.4.11 December 17, 2017
v0.4.10 October 12, 2017

Interesting Forks See all

Remove unnecessary React propTypes from the production build. balloon
JavaScript - MIT - Last pushed - 1 stars
Remove React propTypes and defaultProps from the production build. balloon
JavaScript - Updated - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2019-02-01 21:40:28 UTC

Login to resync this repository