Skip to content

Commit

Permalink
Update Storybook entry patterns and support pattern object (fixes #312)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 24, 2023
1 parent 762cc2c commit bba540d
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 8 deletions.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module.exports = {
stories: ['../*.tales.js'],
stories: [
'../*.tales.js',
{
directory: '../epic',
},
{
files: '*.fable.tsx',
directory: '../epic',
},
],
};
2 changes: 1 addition & 1 deletion src/plugins/storybook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/plugins/storybook/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
type Stories = (string | { directory: string; files?: string; titlePrefix?: string })[];

export type StorybookConfig = {
stories?: string[] | ((patterns: string[]) => Promise<string[]>);
stories?: Stories | ((patterns: string[]) => Promise<string[]>);
addons?: (string | { name: string })[];
core?: {
builder?: string;
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/storybook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions test/workspaces-plugin-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ test('Use root plugin config in workspaces', async () => {

assert.deepEqual(counters, {
...baseCounters,
total: 22,
processed: 22,
total: 23,
processed: 23,
});
});

Expand Down

0 comments on commit bba540d

Please sign in to comment.