diff --git a/lib/csf-tools/src/CsfFile.test.ts b/lib/csf-tools/src/CsfFile.test.ts index 80d2bc2ca052..ed6b3d4d5675 100644 --- a/lib/csf-tools/src/CsfFile.test.ts +++ b/lib/csf-tools/src/CsfFile.test.ts @@ -407,6 +407,30 @@ describe('CsfFile', () => { __id: foo-bar--a `); }); + + it('as default export', () => { + expect( + parse( + dedent` + const meta = { title: 'foo/bar' }; + export const A = () => {}; + export { + meta as default, + A + }; + `, + true + ) + ).toMatchInlineSnapshot(` + meta: + title: foo/bar + stories: + - id: foo-bar--a + name: A + parameters: + __id: foo-bar--a + `); + }); }); describe('error handling', () => { diff --git a/lib/csf-tools/src/CsfFile.ts b/lib/csf-tools/src/CsfFile.ts index 88a8fbd5eec3..c7939544dbe4 100644 --- a/lib/csf-tools/src/CsfFile.ts +++ b/lib/csf-tools/src/CsfFile.ts @@ -297,8 +297,30 @@ export class CsfFile { node.specifiers.forEach((specifier) => { if (t.isExportSpecifier(specifier) && t.isIdentifier(specifier.exported)) { const { name: exportName } = specifier.exported; - self._storyAnnotations[exportName] = {}; - self._stories[exportName] = { id: 'FIXME', name: exportName, parameters: {} }; + if (exportName === 'default') { + let metaNode: t.ObjectExpression; + const decl = t.isProgram(parent) + ? findVarInitialization(specifier.local.name, parent) + : specifier.local; + + if (t.isObjectExpression(decl)) { + // export default { ... }; + metaNode = decl; + } else if ( + // export default { ... } as Meta<...> + t.isTSAsExpression(decl) && + t.isObjectExpression(decl.expression) + ) { + metaNode = decl.expression; + } + + if (!self._meta && metaNode && t.isProgram(parent)) { + self._parseMeta(metaNode, parent); + } + } else { + self._storyAnnotations[exportName] = {}; + self._stories[exportName] = { id: 'FIXME', name: exportName, parameters: {} }; + } } }); }