This Babel plugin, currently at version `0.4.24`, is designed to optimize React applications by effectively removing `propTypes` definitions from the production build. `propTypes` are primarily utilized for development-time type checking and debugging, and thus represent unnecessary overhead in production bundles, contributing to larger file sizes and increased load times. The plugin offers distinct `mode` options: `remove` (the default for most applications), `wrap`, and `unsafe-wrap`. The `wrap` and `unsafe-wrap` modes are specifically tailored for React library authors, enabling them to retain some `propTypes` behavior in development while ensuring optimization for production scenarios. The `remove` mode is generally recommended for direct application authors seeking maximum bundle size reduction. The project exhibits a consistent, albeit at times gradual, release cadence, with recent patches focusing on improved type checking logic, custom `createReactClass` function support, and more robust handling of unused identifier removal. It integrates seamlessly into the Babel ecosystem, typically configured via standard Babel configuration files such as `.babelrc` or `babel.config.js`.
npm install babel-plugin-transform-react-remove-prop-typesVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to integrate and use `babel-plugin-transform-react-remove-prop-types` via Babel's Node API. It transforms a React component, removing its `propTypes` and the `prop-types` import statement in a simulated production environment.
For most React applications, use `mode: 'remove'` or omit the `mode` option entirely (as `remove` is the default). Ensure the plugin is only active in your production build configuration.
Ensure your project is running Babel 7 (or a compatible newer version). Update `@babel/core` and related Babel presets/plugins to their latest versions, often using scoped packages like `@babel/preset-env`.
Add `/* remove-proptypes */` directly after the `propTypes` definition to explicitly mark it for removal, e.g., `Component.propTypes /* remove-proptypes */ = {}`.Use `removeImport: true` when `mode: 'remove'`. Also, ensure your bundler (Webpack, Rollup, etc.) doesn't include `prop-types` globally or as an external dependency in production builds if you intend to fully eliminate it.
Use the `ignoreFilenames` option to exclude `node_modules` from processing: `ignoreFilenames: ['node_modules']`. This ensures the plugin only modifies your own application code.
Switch to `mode: 'remove'` (default) or `mode: 'wrap'`. `mode: 'wrap'` sets `propTypes` to an empty object `{}` in production, preventing runtime errors from undefined access, though it results in a slightly larger bundle than `remove`.Ensure your Babel configuration includes the plugin within an `env.production` block (e.g., in `.babelrc` or `babel.config.js`) and that your build process correctly sets `NODE_ENV=production` when bundling for production.
Add the `removeImport: true` option to the plugin configuration, for example: `['babel-plugin-transform-react-remove-prop-types', { removeImport: true }]`. Ensure this is only used with `mode: 'remove'` for full effect.