使用 Nx 的 @nx/storybook:build Executor 构建 Storybook 生产站点:完整配置指南
2026/9/12 9:17:07 网站建设 项目流程

使用 Nx 的 @nx/storybook:build Executor 构建 Storybook 生产站点:完整配置指南

【免费下载链接】nxThe Monorepo Platform that amplifies both developers and AI agents. Nx optimizes your builds, scales your CI, and fixes failed PRs automatically. Ship in half the time.项目地址: https://gitcode.com/GitHub_Trending/nx/nx

本篇技术指南聚焦 Nx 仓库中@nx/storybook插件提供的@nx/storybook:buildexecutor(对应开发模式为@nx/storybook:storybook),讲解如何在project.json中为其配置 target、产出静态站点,并覆盖非 Angular 项目的docsMode文档站点模式、Angular 项目的browserTarget与样式注入等实战配置。读完本文,你将掌握在 Nx 工作区中构建 Storybook 生产包(dist/storybook/*)的完整配置方法,理解outputs缓存声明、configurations命名配置的用法,并了解该 executor 的底层实现与后续迁移路径。

一、认识 @nx/storybook:build:生产模式静态构建

@nx/storybook:build是 Nx 工作区中用来"以生产模式构建 Storybook"的 executor。它由packages/storybook插件注册,在 executors.json 中可见其注册信息:

{ "executors": { "build": { "implementation": "./dist/src/executors/build-storybook/build-storybook.impl", "schema": "./dist/src/executors/build-storybook/schema.json", "description": "Build Storybook." } } }

其参数校验定义位于 packages/storybook/src/executors/build-storybook/schema.json,从 schema 可以看到该 executor 的核心约束:

  • 必填项configDir(Storybook 配置文件所在目录);
  • outputDir:构建产物输出目录(配合outputs声明即可被 Nx 缓存);
  • 支持stylesstylePreprocessorOptionsdocsModeloglevelquietwebpackStatsJsondebugWebpackdisableTelemetry等可选参数。

从实现文件 build-storybook.impl.ts 可以看到它的执行流程:先做配置目录存在性检查,然后根据已安装的 Storybook 版本(8.2.0 及以上从storybook/internal/core-server导入,更早版本回退到@storybook/core-server)调用storybookCore.build({ ...options, mode: 'static' }),将NODE_ENV默认置为production,构建结束后输出产物路径:

logger.info(`NX Storybook files available in ${buildOptions.outputDir}`);

二、基础用法:配置 build-storybook target

在 Nx 中,executor 通过project.json中的 target 配置被调用。以下是一个最小可用的build-storybooktarget 配置(对应ui项目):

{ "ui": { "targets": { "build-storybook": { "executor": "@nx/storybook:build", "outputs": ["{options.outputDir}"], "options": { "outputDir": "dist/storybook/ui", "configDir": "libs/ui/.storybook" }, "configurations": { "ci": { "quiet": true } } } } } }

配置要点:

  • executor:声明使用@nx/storybook:build
  • outputs:声明产物路径为{options.outputDir},这样 Nx 可以将该 task 的产物纳入缓存与远程缓存管理,命中缓存时直接恢复产物而无需重跑;
  • options.outputDir:构建产物的输出目录,例如dist/storybook/ui
  • options.configDir:Storybook 配置文件(.storybook/main.js.storybook/preview.js等)所在目录,这是唯一必填参数;
  • configurations.ci:命名配置,运行时可切换,例如--configuration=ci时附加quiet: true以抑制冗长输出。

配置完成后,通过 Nx 命令运行该 target:

nx run ui:build-storybook

由于outputs声明了产物路径,重复执行时 Nx 会利用缓存跳过构建;若需要强制重建,可使用nx run ui:build-storybook --skip-nx-cache。若需要读取额外命名配置,可追加--configuration=ci

三、executor 支持的完整参数说明

依据 schema.json,@nx/storybook:build支持以下参数:

参数类型默认值说明
configDirstring加载 Storybook 配置的目录,必填
outputDirstring存放构建产物的目录
stylesarray全局样式,参与构建;每项可以是字符串路径或{ input, bundleName, inject }对象
stylePreprocessorOptionsobject传给样式预处理器的选项,其includePaths为要包含的路径(相对工作区根解析)
docsModebooleanfalse仅使用 addon-docs 构建文档站点
docsboolean以文档模式启动 Storybook
loglevelstringinfo日志级别,可选sillyverboseinfowarnerrorsilent
quietboolean抑制冗长的构建输出
webpackStatsJsonboolean/stringfalse将 Webpack Stats JSON 写入磁盘
debugWebpackboolean显示最终 webpack 配置以便调试
disableTelemetryboolean禁用 Storybook 遥测

其中styles数组项(schema 中的extraEntryPoint定义)支持两种形式:

"styles": [ "src/styles.css", { "input": "src/theme.css", "bundleName": "theme", "inject": true } ]

bundleName用于给该额外入口命名打包,inject(默认true)控制该 bundle 是否被引用进 HTML。

四、非 Angular 项目:docsMode 文档站点模式

对于非 Angular 项目(React、Vue、Web Components 等),可以通过将docsMode设为true构建一个仅包含文档的站点,配合@storybook/addon-docs插件使用。配置示例如下:

{ "storybook": { "executor": "@nx/storybook:build", "options": { "port": 4400, "configDir": "libs/ui/.storybook", "docsMode": true }, "configurations": { "ci": { "quiet": true } } } }

在该模式下,Storybook 不会渲染 stories 的画布,而是以文档页(MDX / 自动生成的 docs 页)为主体输出静态站点,适合把组件文档直接发布为静态站点。docsMode的默认值为false,需要显式开启;port在构建场景下主要用于内部启动上下文(例如配合调试),核心仍是输出静态文件。从源码结构看,docsMode会原样透传给 Storybook 核心的build调用(见 build-storybook.impl.ts 中对options的展开传递)。

五、Angular 项目:使用原生 @storybook/angular:build-storybook

对于 Angular 项目,Nx 生成的默认配置并不走@nx/storybook:build,而是直接使用 Storybook 官方的@storybook/angular:build-storybookexecutor,以便完整复用 Angular builder 的能力(如browserTargetcompodoc等)。

5.1 默认配置

{ "build-storybook": { "executor": "@storybook/angular:build-storybook", "outputs": ["{options.outputDir}"], "options": { "outputDir": "dist/storybook/ngapp", "configDir": "apps/ngapp/.storybook", "browserTarget": "ngapp:build", "compodoc": false }, "configurations": { "ci": { "quiet": true } } } }

要点:

  • browserTarget:指向项目现有的 Angular 构建 target(ngapp:build),Storybook 构建时先据此编译应用;
  • compodoc:是否运行 Compodoc 生成组件文档,默认示例中为false

5.2 修改 browserTarget:无 build target 时指向 build-storybook

当项目没有独立的buildtarget 时,可以把browserTarget指向build-storybook自身,让 Storybook 构建自举编译流程:

{ "build-storybook": { "executor": "@storybook/angular:build-storybook", "outputs": ["{options.outputDir}"], "options": { "outputDir": "dist/storybook/ngapp", "configDir": "apps/ngapp/.storybook", "browserTarget": "ngapp:build-storybook", "compodoc": false }, "configurations": { "ci": { "quiet": true } } } }

这种写法对没有常规应用构建 target 的库项目尤为实用。从源码佐证看,Nx 的 Storybook 工具函数 utilities.ts 在检测项目 targets 时,会将@storybook/angular:build-storybook@nx/storybook:build均识别为storybookBuildTarget,并区分仅官方 Angular application/browser builder 才记录为ngBuildTarget@nx/angular:*的打包类 executor 不支持样式与额外选项,因此更适合交给 build-storybook 承载这些配置),这与"通过 browserTarget 切换构建来源"的实践相互印证。

5.3 添加样式与预处理器选项

Angular 项目还可以通过styles数组注入全局样式,并通过stylePreprocessorOptions.includePaths指定预处理器(如 SCSS)的额外查找路径,用法与 Angular builder 保持一致:

{ "build-storybook": { "executor": "@storybook/angular:build-storybook", "outputs": ["{options.outputDir}"], "options": { "outputDir": "dist/storybook/ngapp", "configDir": "apps/ngapp/.storybook", "browserTarget": "ngapp:build-storybook", "compodoc": false, "styles": ["some-styles.css"], "stylePreprocessorOptions": { "includePaths": ["some-style-paths"] } }, "configurations": { "ci": { "quiet": true } } } }

注意,stylesstylePreprocessorOptions@nx/storybook:build(schema.json 中同样定义)与@storybook/angular:build-storybook中均可使用,但后者依赖 Angular builder 的样式解析管线,路径解析与 Angular 构建保持一致。

六、底层实现与容错机制

两个 executor 的核心实现都在packages/storybook/src/executors/下:

  • build-storybook.impl.ts:构建实现,mode: 'static'NODE_ENV默认production
  • storybook.impl.ts:开发服务器实现,mode: 'dev'NODE_ENV默认development,并回传portbaseUrl供下游使用。

两者在启动前都会调用storybookConfigExistsCheck(configDir, projectName)(定义于 utilities.ts),如果configDir不存在或不是目录,会抛出带指引的错误:

Could not find Storybook configuration for project <project>. Please generate Storybook configuration using the following command: nx g @nx/storybook:configuration --name=<project>

这意味着 target 中的configDir必须指向一个真实存在的.storybook目录,未生成配置前应先执行nx g @nx/storybook:configuration生成。

七、已知弃用与迁移建议

需要特别留意的是,@nx/storybook:build@nx/storybook:storybook这两个 executor 当前已被标记为弃用,计划在 Nx v24 中移除。弃用声明同时存在于 schema.json 与 deprecation.ts 中:

The `@nx/storybook:build` executor is deprecated and will be removed in Nx v24. Run `nx g @nx/storybook:convert-to-inferred` to migrate to the `@nx/storybook/plugin` inferred targets.

对应地,运行这两个 executor 时也会输出警告日志(见 deprecation.ts 中的STORYBOOK_EXECUTOR_DEPRECATION_MESSAGESTORYBOOK_BUILD_EXECUTOR_DEPRECATION_MESSAGE)。

官方推荐的迁移方式是执行:

nx g @nx/storybook:convert-to-inferred

该生成器(源码位于 packages/storybook/src/generators/convert-to-inferred)会将显式的 executor target 转换为@nx/storybook/plugin推断出的 target。以 build 目标为例,其转换逻辑(build-post-target-transformer.ts)会:

  • 从原 target 的options中读取configDir(缺省时按{projectRoot}/.storybook推断);
  • options与各命名configurations中的属性(如docsModestaticDir)迁移到 Storybook 的配置文件(main.ts/vite.config等)中;
  • 让 target 改由插件自动推断管理。

因此在新建配置时,应优先考虑直接使用推断式插件;阅读历史项目或旧文档时,则需理解本文上述 executor 配置的语义,以便顺利完成迁移。

八、小结

@nx/storybook:build提供了一条将 Storybook 构建为生产静态站点的标准化路径:通过outputs声明接入 Nx 缓存体系,通过configDir/outputDir控制输入输出,通过configurations定义 CI 等场景的变体;非 Angular 项目可用docsMode输出纯文档站点,Angular 项目则建议走@storybook/angular:build-storybook并善用browserTargetstyles/stylePreprocessorOptions。同时要留意其已进入弃用窗口,新项目应使用@nx/storybook/plugin推断 target,存量配置可通过nx g @nx/storybook:convert-to-inferred平滑迁移。

【免费下载链接】nxThe Monorepo Platform that amplifies both developers and AI agents. Nx optimizes your builds, scales your CI, and fixes failed PRs automatically. Ship in half the time.项目地址: https://gitcode.com/GitHub_Trending/nx/nx

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询