Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React with Webpack: PropTypes appearing in final bundle

Per React's documentation on PropTypes:

Note that for performance reasons propTypes is only checked in development mode.

However, when I build my app with Webpack, I see the propTypes that I have defined still in the final bundle.

I am setting the NODE_ENV to production. Is this behavior expected?

My theory:

The PropTypes validation is skipped, although the PropType definitions are still in production code. If this theory is correct, are there any best practices for stripping PropTypes in prod code?

like image 841
Avindra Goolcharan Avatar asked Apr 18 '16 15:04

Avindra Goolcharan


People also ask

Should PropTypes be a dev dependency?

'prop-types' should be listed in the project's dependencies, not devDependencies.

Can we use PropTypes in functional component?

In this example, we are using a class component, but the same functionality could also be applied to function components, or components created by React.memo or React.forwardRef . PropTypes exports a range of validators that can be used to make sure the data you receive is valid.

Should you use PropTypes?

Props and PropTypes are important mechanisms for passing read-only attributes between React components. We can use React props, short for properties, to send data from one component to another. If a component receives the wrong type of props, it can cause bugs and unexpected errors in your app.

Does Webpack bundle React?

Most React apps will have their files “bundled” using tools like Webpack, Rollup or Browserify. Bundling is the process of following imported files and merging them into a single file: a “bundle”. This bundle can then be included on a webpage to load an entire app at once.


1 Answers

Babel plugin that does the trick:

https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types

like image 199
Avindra Goolcharan Avatar answered Nov 07 '22 17:11

Avindra Goolcharan