stories
(必需)
类型
| (string | StoriesSpecifier)[]
| async (list: (string | StoriesSpecifier)[]) => (string | StoriesSpecifier)[]配置 Storybook 以从指定位置加载故事。目的是让您将故事文件与其文档组件放在一起。
•
└── components
├── Button.ts
└── Button.stories.ts
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { StorybookConfig } from '@storybook/your-framework';
const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
};
export default config;如果您想使用不同的命名约定,可以使用 picomatch 支持的语法来修改 glob。
请记住,某些插件可能假定 Storybook 的默认命名约定。
使用 glob 数组
Storybook 将加载项目中的故事,这些故事是通过此 glob 数组(模式匹配字符串)找到的。
故事的加载顺序与其在数组中的定义顺序一致。这使您可以控制故事在侧边栏中显示的顺序。
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { StorybookConfig } from '@storybook/your-framework';
const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: [
'../src/**/*.mdx', // 👈 These will display first in the sidebar
'../src/**/*.stories.@(js|jsx|mjs|ts|tsx)', // 👈 Followed by these
],
};
export default config;使用配置对象
此外,您还可以自定义 Storybook 配置,以基于配置对象加载您的故事。此对象是 StoriesSpecifier 类型,定义如下。
例如,如果您想从 packages/components 目录加载您的故事,可以将 stories 配置字段调整为如下所示:
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { StorybookConfig } from '@storybook/your-framework';
const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: [
{
// 👇 Sets the directory containing your stories
directory: '../packages/components',
// 👇 Storybook will load all files that match this glob
files: '*.stories.*',
// 👇 Used when generating automatic titles for your stories
titlePrefix: 'MyComponents',
},
],
};
export default config;当 Storybook 启动时,它会在 packages/components 目录中查找任何包含 stories 扩展名的文件,并生成 stories 的标题。
StoriesSpecifier
类型
{
directory: string;
files?: string;
titlePrefix?: string;
}StoriesSpecifier.directory
(必需)
类型:string
相对于项目根目录,开始查找故事文件的位置。
StoriesSpecifier.files
类型:string
默认:'**/*.@(mdx|stories.@(js|jsx|mjs|ts|tsx))'
一个相对于 StoriesSpecifier.directory 的 glob(没有前导 ./),用于匹配要加载的文件名。
StoriesSpecifier.titlePrefix
类型:string
默认:''
当 自动生成标题 时,用于生成故事标题的前缀。
使用自定义实现
💡 Storybook 现在会静态分析配置文件以提高性能。使用自定义实现加载故事可能会导致此功能性能下降或中断。
您还可以调整 Storybook 配置并实现自定义逻辑来加载 stories。例如,假设您正在处理一个包含特定模式的项目,而传统的 stories 加载方式无法解决。在这种情况下,您可以按如下方式调整配置:
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { StorybookConfig } from '@storybook/your-framework';
import type { StoriesEntry } from 'storybook/internal/types';
async function findStories(): Promise<StoriesEntry[]> {
// your custom logic returns a list of files
}
const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: async (list: StoriesEntry[]) => [
...list,
// 👇 Add your found stories to the existing list of story files
...(await findStories()),
],
};
export default config;