stories
(必要)
類型
| (string | StoriesSpecifier)[]
| async (list: (string | StoriesSpecifier)[]) => (string | StoriesSpecifier)[]
設定 Storybook 從指定位置載入 stories。目的是讓您將 story 檔案與其記錄的元件並列
•
└── components
├── Button.ts
└── Button.stories.ts
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
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 (樣式比對字串) 陣列找到的 stories。
Stories 會依照它們在陣列中定義的順序載入。這可讓您控制 stories 在側邊欄中顯示的順序
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
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;
使用設定物件
此外,您可以自訂 Storybook 設定,以根據設定物件載入您的 stories。此物件的類型為 StoriesSpecifier
,定義如下。
例如,如果您想從 packages/components
目錄載入 stories,您可以將 stories
設定欄位調整為以下內容
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
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
從專案根目錄開始尋找 story 檔案的位置。
StoriesSpecifier.files
類型:string
預設值:'**/*.@(mdx|stories.@(js|jsx|mjs|ts|tsx))'
一個相對於 StoriesSpecifier.directory
的 glob (不含開頭的 ./
),用於比對要載入的檔案名稱。
StoriesSpecifier.titlePrefix
類型:string
預設值:''
當啟用自動標題時,用於產生你的 stories 標題時使用的前綴。
使用自訂實作
💡 Storybook 現在會靜態分析設定檔以提升效能。使用自訂實作載入 stories 可能會降低效能或破壞此能力。
您也可以調整 Storybook 的設定並實作自訂邏輯來載入您的 stories。例如,假設您正在處理一個專案,其中包含一個傳統載入 stories 的方式無法解決的特定模式。在這種情況下,您可以如下調整您的設定
// Replace your-framework with the framework you are using (e.g., react-webpack5, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';
import type { StoriesEntry } from '@storybook/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;