oliviertassinari / babel-plugin-transform-react-remove-prop-types

Remove unnecessary React propTypes from the production build. :balloon:
MIT License
897 stars 61 forks source link

propTypes not removed for HOC #106

Closed LeonardoGentile closed 7 years ago

LeonardoGentile commented 7 years ago

Hello, I've just installed this and it seems that for my edge cases this is not working.

The following propTypes are not removed from the build. While all the others declared in the standard way are correctly removed.

CASE 1:

const myPropName = 'whatever';
BaseLink.propTypes = {
  prop1: PropTypes.string
  prop2: PropTypes.node
};

BaseLink.propTypes[myPropName] = PropTypes.object;  //<== This is not removed

CASE 2:

const myPropName = 'whatever';
// Both are not removed
MyComp.wrappedComponent.propTypes = {};
MyComp.wrappedComponent.propTypes[myPropName] = PropTypes.object.isRequired;

Here I used wrappedComponents, because I'm decorating my original component MyComp with mobx-react @observer that will indeed create a new wrapper. So the original component is available as MyComp.wrappedComponent.

CASE 3: This applies to both cases above. I'm importing my PropTypes from the new separate packages

import PropTypes from 'prop-types';

It seems the import is never removed from the build using the default config (supposed to be remove mode).

I'm not sure if this is due to the 2 cases above (still having some propTypes in the build would not allow to remove the import?) or because I import them from the separate package and not from the React core... 🤔

oliviertassinari commented 7 years ago

I would need the full source code of your examples. CASE 1 and CASE 2 are expected from the source code you are sharing. For CASE 3, you could try the removeImport option.

LeonardoGentile commented 7 years ago

@oliviertassinari thanks for the quick reply! Case1: https://github.com/LeonardoGentile/react-mobx-router5/blob/dev/src/modules/BaseLink.js Case2: https://github.com/LeonardoGentile/react-mobx-router5/blob/dev/src/modules/routeNode.js

Notice that your plugin is not in that branch yet cause I'm still experimenting with it right now. For case 3 I'll let you know asap

LeonardoGentile commented 7 years ago

Concerning CASE 3, I've tried multiple times in my babel config:

 "plugins": [
      ["transform-react-remove-prop-types", {
         "mode": "remove",
         "removeImport": true
       }],
      "transform-decorators-legacy",
      "transform-runtime"
    ]

but it seems to have no effect. The import is never removed.

LeonardoGentile commented 7 years ago

Also this won't work:

BaseLink.propTypes[storeName] /* remove-proptypes */ = PropTypes.object;
BaseLink.propTypes['route'] /* remove-proptypes */ = PropTypes.object;
BaseLink.propTypes['previousRoute'] /* remove-proptypes */ = PropTypes.object;
BaseLink.propTypes['isActive'] /* remove-proptypes */ = PropTypes.bool;
// ...
RouteNode.wrappedComponent.propTypes /* remove-proptypes */ = {};
RouteNode.wrappedComponent.propTypes[storeName] /* remove-proptypes */ = PropTypes.object.isRequired;
oliviertassinari commented 7 years ago

Here is a simplified failing case to illustrate #108.