Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid re-emitting docgen for re-exported components #9

Merged
merged 2 commits into from
May 24, 2024
Merged

Avoid re-emitting docgen for re-exported components #9

merged 2 commits into from
May 24, 2024

Conversation

wrose504
Copy link
Contributor

In our codebase we will often define a component in one file, then re-export it from index.ts. In some cases, the re-export uses a name that does not match the original component. In these cases, react-docgen-loader emits invalid code that tries to assign docgen info to the actual name from the original component, which fails.

// components/Button/Button.tsx
import React from 'react';
export const Button = () => (<button type='submit'/>);
export default Button;

// components/Button/index.ts
import ButtonComponent from './Button';
export default ButtonComponent;

// components/Bouton/index.ts
import {Button as Bouton} from '../Button/Button';
export {Bouton};

// App.tsx
import Button from './components/Button';
import Bouton from './components/Bouton';

const App = () => (
  <form><Button/><Bouton/></form>
);

Currently invalid code is written into components/Button/index.ts and components/Bouton/index.ts that tries to assign to Button.__docgenInfo, when Button is not a valid identifier in either file.

The react-webpack implementation of react-docgen-loader avoids this problem by checking that the resolved component definition comes from the initial source file. See https://github.com/storybookjs/storybook/blob/e812f77def55aa7aeda70dddcc378c736412472d/code/presets/react-webpack/src/loaders/react-docgen-loader.ts#L25

This PR adds the same logic here, which fixes the issue.

@fi3ework
Copy link
Member

Thanks for the sync up with storybook's latest code. 🫶

@wrose504 wrose504 requested a review from fi3ework May 23, 2024 18:30
@fi3ework fi3ework merged commit 6498da5 into rspack-contrib:main May 24, 2024
1 check passed
@fi3ework
Copy link
Member

fi3ework commented May 24, 2024

Thank you!

@fi3ework fi3ework added the sync Sync up changes from storybookjs/storybook label May 24, 2024
@wrose504 wrose504 deleted the fix--non-local branch May 24, 2024 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sync Sync up changes from storybookjs/storybook
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants