Skip to content

Commit

Permalink
fix: avoid re-emitting docgen for re-exported components (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrose504 authored May 24, 2024
1 parent 0d5263d commit 6498da5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/react-rsbuild/src/loaders/react-docgen-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const actualNameHandler: Handler = function actualNameHandler(
documentation,
componentDefinition,
) {
documentation.set('definedInFile', componentDefinition.hub.file.opts.filename)
if (
(componentDefinition.isClassDeclaration() ||
componentDefinition.isFunctionDeclaration()) &&
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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}`)
}
Expand Down
31 changes: 31 additions & 0 deletions sandboxes/react-rsbuild/src/stories/ReExportButton.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Meta, StoryObj } from '@storybook/react'
import { fn } from '@storybook/test'
import ReExportButton from './ReExportButton'

const meta: Meta<typeof ReExportButton> = {
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<typeof meta> = {
args: {
primary: true,
label: 'Button',
},
}
2 changes: 2 additions & 0 deletions sandboxes/react-rsbuild/src/stories/ReExportButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Button as ReExportButton } from './Button'
export default ReExportButton

0 comments on commit 6498da5

Please sign in to comment.