From 0822c9b18b67cf3029383f660acdecff2517d54b Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 29 Nov 2024 09:25:55 +0800 Subject: [PATCH] Merge pull request #29733 from storybookjs/yann/improve-ps-annotations Portable stories: Support multiple annotation notations from preview (cherry picked from commit 020074b3880845cb633c9b4b0a58e2cb1e447dfd) --- .../store/csf/portable-stories.test.ts | 19 +++++++++++++++++++ .../modules/store/csf/portable-stories.ts | 7 ++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/code/core/src/preview-api/modules/store/csf/portable-stories.test.ts b/code/core/src/preview-api/modules/store/csf/portable-stories.test.ts index 6c85760cbd4e..24e286b3b909 100644 --- a/code/core/src/preview-api/modules/store/csf/portable-stories.test.ts +++ b/code/core/src/preview-api/modules/store/csf/portable-stories.test.ts @@ -77,6 +77,25 @@ describe('composeStory', () => { expect(composedStory.parameters.fromAnnotations.asDefaultImport).toEqual(true); }); + it('should compose project annotations when used in named and default exports from the same module', () => { + setProjectAnnotations([ + { + initialGlobals: { namedExportAnnotation: true }, + default: { + parameters: { defaultExportAnnotation: true }, + }, + }, + ]); + + const Story: Story = { + render: () => {}, + }; + + const composedStory = composeStory(Story, meta); + expect(composedStory.parameters.defaultExportAnnotation).toEqual(true); + expect(composedStory.globals.namedExportAnnotation).toEqual(true); + }); + it('should return story with composed annotations from story, meta and project', () => { const decoratorFromProjectAnnotations = vi.fn((StoryFn) => StoryFn()); const decoratorFromStoryAnnotations = vi.fn((StoryFn) => StoryFn()); diff --git a/code/core/src/preview-api/modules/store/csf/portable-stories.ts b/code/core/src/preview-api/modules/store/csf/portable-stories.ts index b9efd7da9792..22b449bd98f0 100644 --- a/code/core/src/preview-api/modules/store/csf/portable-stories.ts +++ b/code/core/src/preview-api/modules/store/csf/portable-stories.ts @@ -61,11 +61,8 @@ function extractAnnotation( // import * as annotations from '.storybook/preview' // import annotations from '.storybook/preview' // in both cases: 1 - the file has a default export; 2 - named exports only - // support imports such as - // import * as annotations from '.storybook/preview' - // import annotations from '.storybook/preview' - // in both cases: 1 - the file has a default export; 2 - named exports only - return 'default' in annotation ? annotation.default : annotation; + // also support when the file has both annotations coming from default and named exports + return composeConfigs([annotation]); } export function setProjectAnnotations(