diff --git a/lib/csf-tools/src/CsfFile.ts b/lib/csf-tools/src/CsfFile.ts index 3eb5f4b9702a..b054f0a683a2 100644 --- a/lib/csf-tools/src/CsfFile.ts +++ b/lib/csf-tools/src/CsfFile.ts @@ -7,6 +7,7 @@ import * as t from '@babel/types'; import traverse, { Node } from '@babel/traverse'; import { toId, isExportStory } from '@storybook/csf'; +const logger = console; interface Meta { title?: string; includeStories?: string[] | RegExp; @@ -34,6 +35,12 @@ function parseIncludeExclude(prop: Node) { throw new Error(`Unknown include/exclude: ${prop}`); } +const parseTitle = (value: any) => { + if (t.isStringLiteral(value)) return value.value; + logger.warn(`Unexpected meta.title: ${JSON.stringify(value)}`); + return undefined; +}; + const parseMeta = (declaration: t.ObjectExpression): Meta => { const meta: Meta = {}; declaration.properties.forEach((p: Node) => { @@ -48,12 +55,6 @@ const parseMeta = (declaration: t.ObjectExpression): Meta => { }); return meta; }; - -const parseTitle = (value: any) => { - if (t.isStringLiteral(value)) return value.value; - return undefined; -}; - export class CsfFile { _ast: Node; @@ -128,13 +129,16 @@ export class CsfFile { }); // default export can come at any point in the file, so we do this post processing last - self._stories = Object.entries(self._stories).reduce((acc, [name, story]) => { - if (isExportStory(name, self._meta)) { - const id = toId(self._meta.title, name); - acc[name] = { ...story, id, parameters: { ...story.parameters, __id: id } }; - } - return acc; - }, {} as Record); + self._stories = + self._meta && self._meta.title + ? Object.entries(self._stories).reduce((acc, [name, story]) => { + if (isExportStory(name, self._meta)) { + const id = toId(self._meta.title, name); + acc[name] = { ...story, id, parameters: { ...story.parameters, __id: id } }; + } + return acc; + }, {} as Record) + : {}; // no meta = no stories return self; } diff --git a/lib/csf-tools/src/__testfixtures__/nometa.snapshot b/lib/csf-tools/src/__testfixtures__/nometa.snapshot new file mode 100644 index 000000000000..2431f585bb5f --- /dev/null +++ b/lib/csf-tools/src/__testfixtures__/nometa.snapshot @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`csf extract nometa 1`] = ` +{ + "stories": [] +} +`; diff --git a/lib/csf-tools/src/__testfixtures__/nometa.stories.js b/lib/csf-tools/src/__testfixtures__/nometa.stories.js new file mode 100644 index 000000000000..7a79ae771a7b --- /dev/null +++ b/lib/csf-tools/src/__testfixtures__/nometa.stories.js @@ -0,0 +1,8 @@ +import React from 'react'; +import base from 'paths.macro'; + +export const story1 = () => <>story1; +story1.storyName = 'story 1'; + +export const story2 = () => <>story2; +story2.storyName = 'story 2';