moroshko / react-scanner

Extract React components and props usage from code.
MIT License
564 stars 40 forks source link

Default imports don't show the correct component name #11

Closed sapegin closed 3 years ago

sapegin commented 3 years ago

Similar to #10 but with default imports:

import DesignSystemIcon from '@my/design-system/Icon'

The component name in the stats will be DesignSystemIcon instead of the correct one Icon.

Not sure we should detect the name automatically in this case though. Maybe taking the last part of the path and give an ability to override this behavior would be a good solution?

moroshko commented 3 years ago

I think trying to automagically guess the component name from the module name/path won't scale.

How about adding a getComponentName option?

For example:

{
  ...
  getComponentName: ({ imported, local, moduleName }) => {
    const parts = moduleName.split('/');

    return parts[parts.length - 1];
  }
}

In #10, I basically suggest that the default is ({ imported, local }) => imported || local instead of the current ({ local }) => local.

sapegin commented 3 years ago

Yeah, that would work I guess! 🦄

moroshko commented 3 years ago

getComponentName is available in 0.5.0.

sapegin commented 3 years ago

Yay, it works!

https://blog.sapegin.me/til/react/finding-the-most-used-react-components-with-react-scanner/