From be4ea718dbfc1a7f0282e7009a5372e36aba3767 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Wed, 16 Aug 2023 16:49:47 +0800 Subject: [PATCH] Merge pull request #23852 from storybookjs/shilman/fix-story-indexer Index: Fix `*.story.*` CSF indexing (cherry picked from commit 377f7f9c0a022ae073ab0e93792d4e15dd88b15c) --- .../core-server/src/presets/common-preset.ts | 2 +- .../src/utils/StoryIndexGenerator.test.ts | 30 +++++++++++++++++++ .../src/utils/__mockdata__/src/F.story.ts | 7 +++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 code/lib/core-server/src/utils/__mockdata__/src/F.story.ts diff --git a/code/lib/core-server/src/presets/common-preset.ts b/code/lib/core-server/src/presets/common-preset.ts index fcb4b5681571..7e13ade9ddf0 100644 --- a/code/lib/core-server/src/presets/common-preset.ts +++ b/code/lib/core-server/src/presets/common-preset.ts @@ -195,7 +195,7 @@ export const features = async ( }); export const csfIndexer: Indexer = { - test: /\.stories\.(m?js|ts)x?$/, + test: /\.(stories|story)\.(m?js|ts)x?$/, index: async (fileName, options) => (await readCsf(fileName, options)).parse().indexInputs, }; diff --git a/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts b/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts index 7110743b7003..b1097c168a86 100644 --- a/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts +++ b/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts @@ -99,6 +99,36 @@ describe('StoryIndexGenerator', () => { `); }); }); + describe('single file .story specifier', () => { + it('extracts stories from the right files', async () => { + const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry( + './src/F.story.ts', + options + ); + + const generator = new StoryIndexGenerator([specifier], options); + await generator.initialize(); + + expect(await generator.getIndex()).toMatchInlineSnapshot(` + Object { + "entries": Object { + "f--story-one": Object { + "id": "f--story-one", + "importPath": "./src/F.story.ts", + "name": "Story One", + "tags": Array [ + "autodocs", + "story", + ], + "title": "F", + "type": "story", + }, + }, + "v": 4, + } + `); + }); + }); describe('non-recursive specifier', () => { it('extracts stories from the right files', async () => { const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry( diff --git a/code/lib/core-server/src/utils/__mockdata__/src/F.story.ts b/code/lib/core-server/src/utils/__mockdata__/src/F.story.ts new file mode 100644 index 000000000000..bb14d42c7112 --- /dev/null +++ b/code/lib/core-server/src/utils/__mockdata__/src/F.story.ts @@ -0,0 +1,7 @@ +const component = {}; +export default { + component, + tags: ['autodocs'], +}; + +export const StoryOne = {};