question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to disable babel-plugin-transform-react-remove-prop-types

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

I was upgrading from v.3 to v.6 and one of the changes was related to the next/babel preset. The preset contains the babel-plugin-transform-react-remove-prop-types plugin, which previously was easy to remove (filtering based on names). The plugin needs to be removed because propTypes carry metadata used in the documentation and also because of Object.keys(propTypes), which is useful for omitting known props when passing them to the child:

class Foo extends Component {
  static propTypes = {
    onZing: PropTypes.func.isRequired,
  };

  render() {
    // Note that onZing is not used in render, but rather in another method;
    // In my experience more complicated components benefit greatly from Object.keys(propTypes)
    return (
      <Bar
        {...omit(this.props, Object.keys(Foo.propTypes)
        onClick={this.handleClick}
      />
    );
  }

  handleClick = () => {
    this.props.onZing();
  };
}

Right now filtering plugins by names is not possible, so I’d like to have an option to remove the plugin.

Describe the solution you’d like

I’d like to be able to pass an option to the preset that would prevent the inclusion of babel-plugin-transform-react-remove-prop-types even in production environment. It could be used as such:

process.env.NODE_ENV === 'production' &&
get(opts, 'react-remove-prop-types', true) &&
require('babel-plugin-transform-react-remove-prop-types')

where get is lodash/get or its equivalent.

Describe alternatives you’ve considered

For now I have this condition in place:

plugins = plugins.filter(
  plugin => (
    !plugin.default ||
    !plugin.default.toString().includes('react-remove-prop-types')
  )
)

This is far from ideal, because it depends on the contents of the plugin functions. Some of the functions are bound and have [native code] as a source, so it’s only by luck that the plugin can be discovered.

Additional context

As usual, I volunteer to prepare a PR.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
fatfiszcommented, Jul 24, 2019

babelrc.js, but that was over a year ago. I concur that in general it’s a bad pattern to rely on propTypes.

0reactions
balazsorban44commented, Jan 31, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-plugin-transform-react-remove-prop-types - npm
Remove unnecessary React propTypes from the production build. Latest version: 0.4.24, last published: 4 years ago.
Read more >
Ability to disable babel-plugin-transform-react-remove-prop ...
I tried to implement your solution in my next.config.js but it did not work. I am using PropTypes to document components automatically, which ......
Read more >
How to remove React PropTypes from production rollup build?
You can use transform-react-remove-prop-types babel plugin with options removeImport: true . So this condition finally remove prop-types ...
Read more >
Optimizing React apps: Hardcore edition - DEV Community ‍ ‍
PropTypes are incredibly helpful during development, but they are of no use for your users. You can use babel-plugin-transform-react-remove-prop ...
Read more >
babel/plugin-transform-typescript
However, this plugin does not add the ability to type-check the JavaScript passed to ... When set to true , the transform will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found