GithubHelp home page GithubHelp logo

trueadm / babel-plugin-transform-react-remove-prop-types Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oliviertassinari/babel-plugin-transform-react-remove-prop-types

1.0 3.0 0.0 204 KB

Remove React propTypes and defaultProps from the production build. :balloon:

License: MIT License

JavaScript 100.00%

babel-plugin-transform-react-remove-prop-types's Introduction

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: PropTypes.string
};

Out

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

Usage

Via .babelrc (Recommended)

.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:
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.

removeImport

  • 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.

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

additionalLibraries

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.

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.

License

MIT

babel-plugin-transform-react-remove-prop-types's People

Contributors

azizhk avatar bitfrost avatar bryanrsmith avatar enoahnetzach avatar greenkeeperio-bot avatar helmus avatar insin avatar lencioni avatar newyork-anthonyng avatar nogsmpls avatar oliviertassinari avatar pkuczynski avatar strml avatar wy-ei avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.