This course is archived!

While the concepts of this course are still largely applicable, it's built using an older version of Symfony (4) and React (16).

Login to bookmark this video
Buy Access to Course
15.

Removing propTypes on Production

Share this awesome video!

|

Keep on Learning!

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

The whole purpose of propTypes is to help us during development: they don't add any actual functionality to our code. And, for that reason, some people remove the propTypes code when they build their assets for production. It's not a big deal, it just makes your final JS files a little bit smaller.

This is totally optional, but let's do it real quick! Google for "babel-plugin-transform-react-remove-prop-types". Wow! First prize for longest name!

This is a Babel plugin that can remove propTypes. Copy the library name, find your terminal, and get it!

yarn add babel-plugin-transform-react-remove-prop-types --dev

While that's downloading, go back to its docs. Usually, this is configured via a .babelrc file: this activates the plugin on the production environment. Except, because we're using Webpack Encore, it handles the Babel configuration for us.

Tip

If you downloaded the code, the webpack.config.js file will now already have a configureBabel() method at the bottom. Add this between the first {} inside that call instead of adding a new configureBabel() call.

Fortunately, Encore gives us a hook to modify that config. Add .configureBabel() and pass this a function with one arg: call it babelConfig. Now, when Encore builds, it will create our Babel configuration, then call this function so we can modify it. We need to add a new env key, with this config below it. Copy the production, plugins part. Then, add babelConfig.env = and paste. This is safe because, if you logged the babelConfig object, you would see that Encore doesn't include an env key. So, we're not overriding anything.

41 lines | webpack.config.js
// ... lines 1 - 3
Encore
// ... lines 5 - 29
.configureBabel((babelConfig) => {
babelConfig.env = {
"production": {
"plugins": ["transform-react-remove-prop-types"]
}
}
})
// ... lines 37 - 41

Oh wait, actually, I made a mistake! This totally won't work! That's because we can't rely on Babel to know whether or not we're creating our production build. Instead, use if Encore.isProduction(). Then, inside, add the plugin with babelConfig.plugins.push(), copy the plugin name, and paste!

41 lines | webpack.config.js
// ... lines 1 - 29
.configureBabel((babelConfig) => {
if (Encore.isProduction()) {
babelConfig.plugins.push(
'transform-react-remove-prop-types'
);
}
})
// ... lines 37 - 41

Remove the stuff below. This is simpler anyways: if we're building for production, add this handy plugin.

We're not going to build for production right now, but to make sure we didn't break anything, go back to the terminal that runs encore, press Ctrl+C to stop it, then restart:

yarn run encore dev-server

And... no errors! Later, when we execute yarn run encore production, the prop types won't be there.