From bba540dad7544e13a7214556e362fef06bbc4cae Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Tue, 24 Oct 2023 20:04:29 +0200 Subject: [PATCH] Update Storybook entry patterns and support pattern object (fixes #312) --- .../package1/components/epic/component.fable.tsx | 0 .../package1/components/epic/component.stories.mdx | 0 .../packages/package1/components/storybook/main.ts | 11 ++++++++++- src/plugins/storybook/README.md | 2 +- src/plugins/storybook/index.ts | 7 +++++-- src/plugins/storybook/types.ts | 4 +++- test/plugins/storybook.test.ts | 2 +- test/workspaces-plugin-config.test.ts | 4 ++-- 8 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 fixtures/workspaces-plugin-config/packages/package1/components/epic/component.fable.tsx create mode 100644 fixtures/workspaces-plugin-config/packages/package1/components/epic/component.stories.mdx diff --git a/fixtures/workspaces-plugin-config/packages/package1/components/epic/component.fable.tsx b/fixtures/workspaces-plugin-config/packages/package1/components/epic/component.fable.tsx new file mode 100644 index 000000000..e69de29bb diff --git a/fixtures/workspaces-plugin-config/packages/package1/components/epic/component.stories.mdx b/fixtures/workspaces-plugin-config/packages/package1/components/epic/component.stories.mdx new file mode 100644 index 000000000..e69de29bb diff --git a/fixtures/workspaces-plugin-config/packages/package1/components/storybook/main.ts b/fixtures/workspaces-plugin-config/packages/package1/components/storybook/main.ts index ea53116b4..ceee7c28b 100644 --- a/fixtures/workspaces-plugin-config/packages/package1/components/storybook/main.ts +++ b/fixtures/workspaces-plugin-config/packages/package1/components/storybook/main.ts @@ -1,3 +1,12 @@ module.exports = { - stories: ['../*.tales.js'], + stories: [ + '../*.tales.js', + { + directory: '../epic', + }, + { + files: '*.fable.tsx', + directory: '../epic', + }, + ], }; diff --git a/src/plugins/storybook/README.md b/src/plugins/storybook/README.md index d38da3bf1..fca211d75 100644 --- a/src/plugins/storybook/README.md +++ b/src/plugins/storybook/README.md @@ -14,7 +14,7 @@ or `devDependencies`: { "storybook": { "config": [".storybook/{main,test-runner}.{js,ts}"], - "entry": [".storybook/{manager,preview}.{js,jsx,ts,tsx}", "**/*.stories.{js,jsx,ts,tsx}"], + "entry": [".storybook/{manager,preview}.{js,jsx,ts,tsx}", "**/*.@(mdx|stories.@(mdx|js|jsx|mjs|ts|tsx))"], "project": [".storybook/**/*.{js,jsx,ts,tsx}"] } } diff --git a/src/plugins/storybook/index.ts b/src/plugins/storybook/index.ts index f31552e0c..cfc8c1a9a 100644 --- a/src/plugins/storybook/index.ts +++ b/src/plugins/storybook/index.ts @@ -17,7 +17,7 @@ export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDepen export const CONFIG_FILE_PATTERNS = ['.storybook/{main,test-runner}.{js,ts}']; /** @public */ -export const STORIES_FILE_PATTERNS = ['**/*.stories.{js,jsx,ts,tsx}']; +export const STORIES_FILE_PATTERNS = ['**/*.@(mdx|stories.@(mdx|js|jsx|mjs|ts|tsx))']; /** @public */ export const ENTRY_FILE_PATTERNS = ['.storybook/{manager,preview}.{js,jsx,ts,tsx}', ...STORIES_FILE_PATTERNS]; @@ -33,7 +33,10 @@ const findStorybookDependencies: GenericPluginCallback = async (configFilePath, typeof localConfig?.stories === 'function' ? await localConfig.stories(STORIES_FILE_PATTERNS) : localConfig?.stories; - const relativePatterns = stories?.map(pattern => relative(cwd, join(dirname(configFilePath), pattern))); + const relativePatterns = stories?.map(pattern => { + if (typeof pattern === 'string') return relative(cwd, join(dirname(configFilePath), pattern)); + return relative(cwd, join(dirname(configFilePath), pattern.directory, pattern.files ?? STORIES_FILE_PATTERNS[0])); + }); const patterns = [...(config?.entry ?? []), ...(relativePatterns ?? [])]; const entryPatterns = (patterns.length > 0 ? patterns : ENTRY_FILE_PATTERNS).map(toEntryPattern); diff --git a/src/plugins/storybook/types.ts b/src/plugins/storybook/types.ts index 696783dc1..14c3229ff 100644 --- a/src/plugins/storybook/types.ts +++ b/src/plugins/storybook/types.ts @@ -1,5 +1,7 @@ +type Stories = (string | { directory: string; files?: string; titlePrefix?: string })[]; + export type StorybookConfig = { - stories?: string[] | ((patterns: string[]) => Promise); + stories?: Stories | ((patterns: string[]) => Promise); addons?: (string | { name: string })[]; core?: { builder?: string; diff --git a/test/plugins/storybook.test.ts b/test/plugins/storybook.test.ts index 398b8ab9e..878b9d047 100644 --- a/test/plugins/storybook.test.ts +++ b/test/plugins/storybook.test.ts @@ -10,7 +10,7 @@ test('Find dependencies in Storybook configuration (main.js)', async () => { const dependencies = await storybook.findDependencies(configFilePath, {}); assert.deepEqual(dependencies, [ 'entry:.storybook/{manager,preview}.{js,jsx,ts,tsx}', - 'entry:**/*.stories.{js,jsx,ts,tsx}', + 'entry:**/*.@(mdx|stories.@(mdx|js|jsx|mjs|ts|tsx))', '@storybook/addon-essentials', '@storybook/addon-a11y', '@storybook/addon-knobs/preset', diff --git a/test/workspaces-plugin-config.test.ts b/test/workspaces-plugin-config.test.ts index c8490a118..652022856 100644 --- a/test/workspaces-plugin-config.test.ts +++ b/test/workspaces-plugin-config.test.ts @@ -15,8 +15,8 @@ test('Use root plugin config in workspaces', async () => { assert.deepEqual(counters, { ...baseCounters, - total: 22, - processed: 22, + total: 23, + processed: 23, }); });