Removing propTypes on Production

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

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.

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.

... lines 1 - 3
... 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!

... lines 1 - 29
.configureBabel((babelConfig) => {
if (Encore.isProduction()) {
... 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.

Leave a comment!

  • 2019-07-01 Skylar Scotlynn Gutman

    Thank you for your help :)

  • 2019-07-01 Vladimir Sadicov

    with version your solution is the best as I see, if you upgrade to version 0.26, than you will be able to use my example.

  • 2019-07-01 Skylar Scotlynn Gutman

    ├─ @symfony/webpack-encore@0.21.1

  • 2019-07-01 Vladimir Sadicov

    Hey Skylar Scotlynn Gutman

    Which version of @symfony/webpack-encore is installed? You can check this with

    yarn list | grep encore

    I think I provided newer example, and it should be used only with newer version of encore.


  • 2019-06-29 Skylar Scotlynn Gutman

    Thank you for the help. But....

    This is the error I get:

    Running webpack ...

    Error: Invalid option "corejs" passed to configureBabel(). Valid keys are exclude

    :( The babel section of webpack.config.js looks like this:
    .configureBabel((babelConfig) => {
    if (Encore.isProduction()) { babelConfig.plugins.push('transform-react-remove-prop-types'); }
    corejs: 2

    Thanks for your help!!

  • 2019-06-28 Vladimir Sadicov

    Hey Skylar Scotlynn Gutman

    You can configure corejs version like this:

    .configureBabel(function(babelConfig) {
    // your babelConfig
    }, {
    corejs: 2 // or set useBuiltIns: false

    hope it will help


  • 2019-06-26 Skylar Scotlynn Gutman

    Hey guys,

    I am getting a warning when using the latest libraries.
    WARNING: We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option.

    The only thing that works is adding this line to .configureBabel
    babelConfig.presets[0][1].corejs = 2;

    Is there a better way to handle this?