JSPM

  • Created
  • Published
  • Downloads 4449890
  • Score
    100M100P100Q231339F
  • License MIT

Remove unnecessary React propTypes from the production build

Package Exports

  • babel-plugin-transform-react-remove-prop-types

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (babel-plugin-transform-react-remove-prop-types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

babel-plugin-transform-react-remove-prop-types

Remove unnecessary React propTypes from the production build.

npm version npm downloads Build Status

Dependencies DevDependencies

Installation

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.

Example

In

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

Baz.propTypes = {
  className: React.PropTypes.string
};

Out

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

Usage

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

Via CLI

babel --plugins transform-react-remove-prop-types script.js

Via Node API

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'],
      },
    ],
  ],
});

Options

mode

  • remove (default): the propTypes definitions are removed from the source code.
  • wrap: the propTypes definitions are wrapped with the following code:
if (process.env.NODE_ENV !== "production") {
  // ...
}

The wrap mode is targeting react libraries like material-ui. It's not intended to be used in userland.

ignoreFilenames

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'],

Is it safe?

If you are using the propTypes in a conventionnal 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 he end-up removing TouchableHighlight.propTypes.

Make sure you are:

  • Not using that pattern in your souce code. If you do, explicitly export the propTypes to work around that limitation.
  • Not parsing the node_modules. If you do, test that things are 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.

License

MIT