All Projects → oliviertassinari → Babel Plugin Transform React Remove Prop Types

oliviertassinari / Babel Plugin Transform React Remove Prop Types

Licence: mit
Remove unnecessary React propTypes from the production build. 🎈

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Babel Plugin Transform React Remove Prop Types

Babel Plugin React Remove Properties
Babel plugin for removing React properties. 💨
Stars: ✭ 327 (-63.26%)
Mutual labels:  minification, babel, babel-plugin
Babel Plugin Styled Components
Improve the debugging experience and add server-side rendering support to styled-components
Stars: ✭ 878 (-1.35%)
Mutual labels:  minification, babel, babel-plugin
Grafoo
A GraphQL Client and Toolkit
Stars: ✭ 264 (-70.34%)
Mutual labels:  babel, babel-plugin
Babel Blade
(under new management!) ⛸️Solve the Double Declaration problem with inline GraphQL. Babel plugin/macro that works with any GraphQL client!
Stars: ✭ 266 (-70.11%)
Mutual labels:  babel, babel-plugin
Effectfuljs
JavaScript embedded effects compiler
Stars: ✭ 287 (-67.75%)
Mutual labels:  babel, babel-plugin
babel-plugin-transform-html-import-to-string
Turn HTML imports (and export from) into constant strings
Stars: ✭ 22 (-97.53%)
Mutual labels:  babel, babel-plugin
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (-95.51%)
Mutual labels:  babel, babel-plugin
Babel Plugin Module Resolver
Custom module resolver plugin for Babel
Stars: ✭ 3,134 (+252.13%)
Mutual labels:  babel, babel-plugin
babel-plugin-proposal-pattern-matching
the minimal grammar, high performance JavaScript pattern matching implementation
Stars: ✭ 34 (-96.18%)
Mutual labels:  babel, babel-plugin
Babel Plugin Tailwind Components
Use Tailwind with any CSS-in-JS library
Stars: ✭ 320 (-64.04%)
Mutual labels:  babel, babel-plugin
I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (-56.29%)
Mutual labels:  babel, babel-plugin
babel-plugin-transform-replace-expressions
A Babel plugin for replacing expressions with other expressions
Stars: ✭ 23 (-97.42%)
Mutual labels:  babel, babel-plugin
babel-plugin-source-map-support
A Babel plugin which automatically makes stack traces source-map aware
Stars: ✭ 41 (-95.39%)
Mutual labels:  babel, babel-plugin
babel-plugin-syntax-pipeline
Allow parsing of pipeline operator |>
Stars: ✭ 23 (-97.42%)
Mutual labels:  babel, babel-plugin
core-web
like core-js but for Web APIs (based on polyfill.io)
Stars: ✭ 34 (-96.18%)
Mutual labels:  babel, babel-plugin
Babel Plugin Console
Babel Plugin that adds useful build time console functions 🎮
Stars: ✭ 278 (-68.76%)
Mutual labels:  babel, babel-plugin
Babel Plugin Sitrep
Log all assignments and the return value of a function with a simple comment
Stars: ✭ 442 (-50.34%)
Mutual labels:  babel, babel-plugin
Static Site Boilerplate
A better workflow for building modern static websites.
Stars: ✭ 1,633 (+83.48%)
Mutual labels:  minification, babel
Static Html Webpack Boilerplate
🔮 modern tooling for old-school static webpage development
Stars: ✭ 226 (-74.61%)
Mutual labels:  minification, babel
Babel Plugin Css Modules Transform
Extract css class names from required css module files, so we can render it on server.
Stars: ✭ 318 (-64.27%)
Mutual labels:  babel, babel-plugin

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.

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$/]

classNameMatchers

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

createReactClassName

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.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].