构建器 API
Storybook 的架构支持多种构建器,包括 Webpack、Vite 和 ESBuild。构建器 API 是你可以用来为 Storybook 添加新构建器的一组接口。
构建器如何工作?
在 Storybook 中,构建器负责将你的组件和 Stories 编译成可在浏览器中运行的 JS 包。构建器还提供用于交互式开发的开发服务器和用于优化打包的生产模式。
要选用构建器,用户必须将其作为依赖项添加,然后编辑其配置文件 (.storybook/main.js
) 以启用它。例如,使用 Vite 构建器
npm install @storybook/builder-vite --save-dev
export default {
stories: ['../src/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: ['@storybook/addon-docs'],
core: {
builder: '@storybook/builder-vite', // 👈 The builder enabled here.
},
};
构建器 API
在 Storybook 中,每个构建器都必须实现以下 API,暴露以下配置选项和入口点
export interface Builder<Config, Stats> {
start: (args: {
options: Options;
startTime: ReturnType<typeof process.hrtime>;
router: Router;
server: Server;
}) => Promise<void | {
stats?: Stats;
totalTime: ReturnType<typeof process.hrtime>;
bail: (e?: Error) => Promise<void>;
}>;
build: (arg: {
options: Options;
startTime: ReturnType<typeof process.hrtime>;
}) => Promise<void | Stats>;
bail: (e?: Error) => Promise<void>;
getConfig: (options: Options) => Promise<Config>;
corePresets?: string[];
overridePresets?: string[];
}
在开发模式下,start
API 调用负责初始化开发服务器,监视文件系统中的更改(例如,组件和 Stories),然后在浏览器中执行热模块重载。它还提供一个 bail 函数,允许正在运行的进程通过用户输入或错误优雅地结束。
在生产模式下,build
API 调用负责生成 Storybook 的静态构建,如果未提供其他配置,默认将其存储在 storybook-static
目录中。生成的输出应包含用户在浏览器中打开 index.html
或 iframe.html
查看其 Storybook 所需的一切内容,且无需运行其他进程。
实现
在底层,构建器负责服务/构建预览 iframe
,它有一组自己的要求。要完全支持 Storybook,包括 Storybook 自带的核心功能,它必须考虑以下几点。
导入 Stories
stories
配置字段用于在 Storybook 中加载 Story。它定义了一个文件 glob 数组,其中包含组件 Stories 的物理位置。构建器必须能够加载这些文件,监视其更改并相应地更新 UI。
提供配置选项
默认情况下,Storybook 的配置在一个专用文件 (storybook/main.js|ts
) 中处理,用户可以选择自定义以满足其需求。构建器也应通过附加字段或其他适合构建器的方式提供自己的配置支持。例如
import { stringifyProcessEnvs } from './envs';
import { getOptimizeDeps } from './optimizeDeps';
import { commonConfig } from './vite-config';
import type { EnvsRaw, ExtendedOptions } from './types';
export async function createViteServer(options: ExtendedOptions, devServer: Server) {
const { port, presets } = options;
// Defines the baseline config.
const baseConfig = await commonConfig(options, 'development');
const defaultConfig = {
...baseConfig,
server: {
middlewareMode: true,
hmr: {
port,
server: devServer,
},
fs: {
strict: true,
},
},
optimizeDeps: await getOptimizeDeps(baseConfig, options),
};
const finalConfig = await presets.apply('viteFinal', defaultConfig, options);
const envsRaw = await presets.apply<Promise<EnvsRaw>>('env');
// Remainder implementation
}
处理 preview.js 导出
preview.js
配置文件允许用户控制 Story 在 UI 中的渲染方式。这是通过 decorators 命名导出提供的。当 Storybook 启动时,它通过虚拟模块入口将这些命名导出转换为内部 API 调用,例如 addDecorator()
。构建器也必须提供类似的实现。例如
import { virtualPreviewFile, virtualStoriesFile } from './virtual-file-names';
import { transformAbsPath } from './utils/transform-abs-path';
import type { ExtendedOptions } from './types';
export async function generateIframeScriptCode(options: ExtendedOptions) {
const { presets, frameworkPath, framework } = options;
const frameworkImportPath = frameworkPath || `@storybook/${framework}`;
const presetEntries = await presets.apply('config', [], options);
const configEntries = [...presetEntries].filter(Boolean);
const absoluteFilesToImport = (files: string[], name: string) =>
files
.map((el, i) => `import ${name ? `* as ${name}_${i} from ` : ''}'${transformAbsPath(el)}'`)
.join('\n');
const importArray = (name: string, length: number) =>
new Array(length).fill(0).map((_, i) => `${name}_${i}`);
const code = `
// Ensure that the client API is initialized by the framework before any other iframe code
// is loaded. That way our client-apis can assume the existence of the API+store
import { configure } from '${frameworkImportPath}';
import {
addDecorator,
addParameters,
addArgTypesEnhancer,
addArgsEnhancer,
setGlobalRender
} from 'storybook/preview-api';
import { logger } from 'storybook/internal/client-logger';
${absoluteFilesToImport(configEntries, 'config')}
import * as preview from '${virtualPreviewFile}';
import { configStories } from '${virtualStoriesFile}';
const configs = [${importArray('config', configEntries.length)
.concat('preview.default')
.join(',')}].filter(Boolean)
configs.forEach(config => {
Object.keys(config).forEach((key) => {
const value = config[key];
switch (key) {
case 'args':
case 'argTypes': {
return logger.warn('Invalid args/argTypes in config, ignoring.', JSON.stringify(value));
}
case 'decorators': {
return value.forEach((decorator) => addDecorator(decorator, false));
}
case 'parameters': {
return addParameters({ ...value }, false);
}
case 'render': {
return setGlobalRender(value)
}
case 'globals':
case 'globalTypes': {
const v = {};
v[key] = value;
return addParameters(v, false);
}
case 'decorateStory':
case 'renderToCanvas': {
return null;
}
default: {
// eslint-disable-next-line prefer-template
return console.log(key + ' was not supported :( !');
}
}
});
})
configStories(configure);
`.trim();
return code;
}
MDX 支持
Storybook 的 Docs 包含了使用 Webpack 加载器在 MDX 中编写 Stories/文档的能力。构建器也必须知道如何解析 MDX 并调用 Storybook 的特殊扩展。例如
import mdx from 'vite-plugin-mdx';
import { createCompiler } from 'storybook/internal/csf-tools/mdx';
export function mdxPlugin() {
return mdx((filename) => {
const compilers = [];
if (filename.endsWith('stories.mdx') || filename.endsWith('story.mdx')) {
compilers.push(createCompiler({}));
}
return {
compilers,
};
});
}
生成源代码片段
Storybook 会使用与其输入相关的附加元数据来标注组件和 Stories,以便自动生成交互式控件和文档。目前,这是通过 Webpack 加载器/插件提供的。构建器必须重新实现此功能以支持这些特性。
生成静态构建
Storybook 的核心功能之一是能够生成可发布到 Web 托管服务的静态构建。构建器也必须能够提供类似的机制。例如
import { build as viteBuild } from 'vite';
import { stringifyProcessEnvs } from './envs';
import { commonConfig } from './vite-config';
import type { EnvsRaw, ExtendedOptions } from './types';
export async function build(options: ExtendedOptions) {
const { presets } = options;
const baseConfig = await commonConfig(options, 'build');
const config = {
...baseConfig,
build: {
outDir: options.outputDir,
emptyOutDir: false,
sourcemap: true,
},
};
const finalConfig = await presets.apply('viteFinal', config, options);
const envsRaw = await presets.apply<Promise<EnvsRaw>>('env');
// Stringify env variables after getting `envPrefix` from the final config
const envs = stringifyProcessEnvs(envsRaw, finalConfig.envPrefix);
// Update `define`
finalConfig.define = {
...finalConfig.define,
...envs,
};
await viteBuild(finalConfig);
}
开发服务器集成
默认情况下,当 Storybook 在开发模式下启动时,它依赖于其内部开发服务器。构建器需要能够与它集成。例如
import { createServer } from 'vite';
export async function createViteServer(options: ExtendedOptions, devServer: Server) {
const { port } = options;
// Remainder server configuration
// Creates the server.
return createServer({
// The server configuration goes here
server: {
middlewareMode: true,
hmr: {
port,
server: devServer,
},
},
});
}
关闭开发服务器
构建器必须提供一种方法,在进程终止时停止开发服务器;这可以通过用户输入或错误实现。例如
import { createViteServer } from './vite-server';
let server: ViteDevServer;
export async function bail(): Promise<void> {
return server?.close();
}
export const start: ViteBuilder['start'] = async ({ options, server: devServer }) => {
// Remainder implementation goes here
server = await createViteServer(options as ExtendedOptions, devServer);
return {
bail,
totalTime: process.hrtime(startTime),
};
};
HMR 支持
在开发模式下运行时,构建器的开发服务器必须能够在 Story、组件或辅助函数发生更改时重新加载页面。
更多信息
该领域正在快速发展,相关文档仍在完善中,并可能发生变化。如果您有兴趣创建构建器,可以通过查看 Vite、Webpack 或 Modern Web 的 dev-server-storybook 源代码,了解更多关于在 Storybook 中实现构建器的信息。准备就绪后,可以发起 RFC 与 Storybook 社区和维护者讨论您的提案。
了解更多关于构建器的信息
- Vite 构建器 用于使用 Vite 打包
- Webpack 构建器 用于使用 Webpack 打包
- 构建器 API 用于构建 Storybook 构建器