@@ -10,9 +10,9 @@ import {
1010import path from 'pathe' ;
1111
1212import { getCodeSnippet } from './generateCodeSnippet' ;
13- import { getComponentImports } from './getComponentImports' ;
13+ import { getComponents , getImports } from './getComponentImports' ;
1414import { extractJSDocInfo } from './jsdocTags' ;
15- import { type DocObj , getReactDocgen } from './reactDocgen' ;
15+ import { type DocObj } from './reactDocgen' ;
1616import { cachedFindUp , cachedReadFileSync , groupBy , invalidateCache , invariant } from './utils' ;
1717
1818interface ReactComponentManifest extends ComponentManifest {
@@ -48,7 +48,7 @@ export const componentManifestGenerator: PresetPropertyFn<
4848 if ( csf . meta . tags ?. includes ( '!manifest' ) ) {
4949 return ;
5050 }
51- let componentName = csf . _meta ?. component ;
51+ const componentName = csf . _meta ?. component ;
5252 const title = entry . title . replace ( / \s + / g, '' ) ;
5353
5454 const id = entry . id . split ( '--' ) [ 0 ] ;
@@ -62,35 +62,19 @@ export const componentManifestGenerator: PresetPropertyFn<
6262 ? JSON . parse ( cachedReadFileSync ( nearestPkg , 'utf-8' ) as string ) . name
6363 : undefined ;
6464
65- const fallbackImport =
66- packageName && componentName ? `import { ${ componentName } } from "${ packageName } ";` : '' ;
67- const componentImports = getComponentImports ( {
65+ const components = getComponents ( {
6866 csf,
69- packageName,
7067 storyFilePath : absoluteImportPath ,
7168 } ) ;
7269
73- const calculatedImports = componentImports . imports . join ( '\n' ) . trim ( ) ?? fallbackImport ;
74-
75- const component = componentImports . components . find ( ( it ) => {
76- const nameMatch = componentName
77- ? it . componentName === componentName ||
78- it . localImportName === componentName ||
79- it . importName === componentName
80- : false ;
81- const titleMatch = ! componentName
82- ? ( it . localImportName ? title . includes ( it . localImportName ) : false ) ||
83- ( it . importName ? title . includes ( it . importName ) : false )
84- : false ;
85- return nameMatch || titleMatch ;
70+ const component = components . find ( ( it ) => {
71+ return componentName
72+ ? [ it . componentName , it . localImportName , it . importName ] . includes ( componentName )
73+ : title . includes ( it . componentName ) ||
74+ ( it . localImportName && title . includes ( it . localImportName ) ) ||
75+ ( it . importName && title . includes ( it . importName ) ) ;
8676 } ) ;
8777
88- componentName ??=
89- component ?. componentName ?? component ?. localImportName ?? component ?. importName ;
90-
91- const componentPath = component ?. path ;
92- const importName = component ?. importName ;
93-
9478 const stories = Object . keys ( csf . _stories )
9579 . map ( ( storyName ) => {
9680 const story = csf . _stories [ storyName ] ;
@@ -104,7 +88,7 @@ export const componentManifestGenerator: PresetPropertyFn<
10488
10589 return {
10690 name : storyName ,
107- snippet : recast . print ( getCodeSnippet ( csf , storyName , componentName ) ) . code ,
91+ snippet : recast . print ( getCodeSnippet ( csf , storyName , component ?. componentName ) ) . code ,
10892 description : finalDescription ?. trim ( ) ,
10993 summary : tags . summary ?. [ 0 ] ,
11094 } ;
@@ -118,24 +102,29 @@ export const componentManifestGenerator: PresetPropertyFn<
118102 } )
119103 . filter ( ( it ) => it != null ) ;
120104
105+ const fallbackImport =
106+ packageName && componentName ? `import { ${ componentName } } from "${ packageName } ";` : '' ;
107+
108+ const imports = getImports ( { components, packageName } ) . join ( '\n' ) . trim ( ) || fallbackImport ;
109+
121110 const base = {
122111 id,
123112 name : componentName ?? title ,
124113 path : importPath ,
125114 stories,
126- import : calculatedImports ,
115+ import : imports ,
127116 jsDocTags : { } ,
128117 } satisfies Partial < ComponentManifest > ;
129118
130- if ( ! componentPath ) {
131- const error = ! componentName
119+ if ( ! component ?. reactDocgen ) {
120+ const error = ! component
132121 ? {
133122 name : 'No meta.component specified' ,
134123 message : 'Specify meta.component for the component to be included in the manifest.' ,
135124 }
136125 : {
137126 name : 'No component import found' ,
138- message : `No component file found for the "${ componentName } " component.` ,
127+ message : `No component file found for the "${ component . componentName } " component.` ,
139128 } ;
140129 return {
141130 ...base ,
@@ -147,25 +136,21 @@ export const componentManifestGenerator: PresetPropertyFn<
147136 } ;
148137 }
149138
150- const docgenResult = getReactDocgen (
151- componentPath ,
152- component ? component : { componentName : componentName ?? title }
153- ) ;
139+ const docgenResult = component . reactDocgen ;
154140
155141 const docgen = docgenResult . type === 'success' ? docgenResult . data : undefined ;
156142 const error = docgenResult . type === 'error' ? docgenResult . error : undefined ;
157143
158- const metaDescription = extractDescription ( csf . _metaStatement ) ;
159- const jsdocComment = metaDescription || docgen ?. description ;
160- const { tags = { } , description } = jsdocComment ? extractJSDocInfo ( jsdocComment ) : { } ;
161144
162- const manifestDescription = ( tags ?. describe ?. [ 0 ] || tags ?. desc ?. [ 0 ] ) ?? description ;
145+
146+ const jsdocComment = extractDescription ( csf . _metaStatement ) || docgen ?. description ;
147+ const { tags = { } , description } = jsdocComment ? extractJSDocInfo ( jsdocComment ) : { } ;
163148
164149 return {
165150 ...base ,
166- description : manifestDescription ?. trim ( ) ,
151+ description : ( ( tags ?. describe ?. [ 0 ] || tags ?. desc ?. [ 0 ] ) ?? description ) ?. trim ( ) ,
167152 summary : tags . summary ?. [ 0 ] ,
168- import : calculatedImports ,
153+ import : imports ,
169154 reactDocgen : docgen ,
170155 jsDocTags : tags ,
171156 error,
0 commit comments