From 6498da57b81d43e4e2137f9f266ab7793c5c56bc Mon Sep 17 00:00:00 2001 From: William Rose Date: Thu, 23 May 2024 20:48:16 -0700 Subject: [PATCH] fix: avoid re-emitting docgen for re-exported components (#9) --- .../src/loaders/react-docgen-loader.ts | 7 +++-- .../src/stories/ReExportButton.stories.ts | 31 +++++++++++++++++++ .../src/stories/ReExportButton.ts | 2 ++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 sandboxes/react-rsbuild/src/stories/ReExportButton.stories.ts create mode 100644 sandboxes/react-rsbuild/src/stories/ReExportButton.ts diff --git a/packages/react-rsbuild/src/loaders/react-docgen-loader.ts b/packages/react-rsbuild/src/loaders/react-docgen-loader.ts index 7d40cb8..bb4a63a 100644 --- a/packages/react-rsbuild/src/loaders/react-docgen-loader.ts +++ b/packages/react-rsbuild/src/loaders/react-docgen-loader.ts @@ -31,6 +31,7 @@ const actualNameHandler: Handler = function actualNameHandler( documentation, componentDefinition, ) { + documentation.set('definedInFile', componentDefinition.hub.file.opts.filename) if ( (componentDefinition.isClassDeclaration() || componentDefinition.isFunctionDeclaration()) && @@ -71,7 +72,7 @@ const actualNameHandler: Handler = function actualNameHandler( } } -type DocObj = Documentation & { actualName: string } +type DocObj = Documentation & { actualName: string; definedInFile: string } const defaultHandlers = Object.values(docgenHandlers).map((handler) => handler) const defaultResolver = new docgenResolver.FindExportedDefinitionsResolver() @@ -141,8 +142,8 @@ export default async function reactDocgenLoader( const magicString = new MagicString(source) docgenResults.forEach((info) => { - const { actualName, ...docgenInfo } = info - if (actualName) { + const { actualName, definedInFile, ...docgenInfo } = info + if (actualName && definedInFile === this.resourcePath) { const docNode = JSON.stringify(docgenInfo) magicString.append(`;${actualName}.__docgenInfo=${docNode}`) } diff --git a/sandboxes/react-rsbuild/src/stories/ReExportButton.stories.ts b/sandboxes/react-rsbuild/src/stories/ReExportButton.stories.ts new file mode 100644 index 0000000..7a130dc --- /dev/null +++ b/sandboxes/react-rsbuild/src/stories/ReExportButton.stories.ts @@ -0,0 +1,31 @@ +import type { Meta, StoryObj } from '@storybook/react' +import { fn } from '@storybook/test' +import ReExportButton from './ReExportButton' + +const meta: Meta = { + title: 'Example/Re-export Button', + component: ReExportButton, + parameters: { + docs: { + description: { + component: + 'This story exists to show that re-exporting a component with a different name does not cause problems.', + }, + }, + layout: 'centered', + }, + tags: ['autodocs'], + argTypes: { + backgroundColor: { control: 'color' }, + }, + args: { onClick: fn() }, +} + +export default meta + +export const Primary: StoryObj = { + args: { + primary: true, + label: 'Button', + }, +} diff --git a/sandboxes/react-rsbuild/src/stories/ReExportButton.ts b/sandboxes/react-rsbuild/src/stories/ReExportButton.ts new file mode 100644 index 0000000..e4ecb6f --- /dev/null +++ b/sandboxes/react-rsbuild/src/stories/ReExportButton.ts @@ -0,0 +1,2 @@ +import { Button as ReExportButton } from './Button' +export default ReExportButton