Publishing Storybook: One main Storybook instance for all projects
This guide extends the Using Storybook in a Nx workspace - Best practices guide. In that guide, we discussed the best practices of using Storybook in a Nx workspace. We explained the main concepts and the mental model of how to best set up Storybook. In this guide, we are going to see how to put that into practice, by looking at a real-world example. We are going to see how you can publish one single Storybook for your workspace.
This case would work if all your projects (applications and libraries) containing stories that you want to use are using the same framework (Angular, React, Vue, etc). The reason is that you will be importing the stories in a central host Storybook's .storybook/main.ts
, and we will be using one specific builder to build that Storybook. Storybook does not support mixing frameworks in the same Storybook instance.
Let’s see how we can implement this solution:
Steps
Generate a new library that will host our Storybook instance
According to the framework you are using, use the corresponding generator to generate a new library. Let’s suppose that you are using React and all your stories are using the @storybook/react-vite
framework:
The command below uses the as-provided
directory flag behavior, which is the default in Nx 16.8.0. If you're on an earlier version of Nx or using the derived
option, omit the --directory
flag. See the as-provided vs. derived documentation for more details.
❯
nx g @nx/react:library storybook-host --directory=libs/storybook-host --bundler=none --unitTestRunner=none --projectNameAndRootFormat=as-provided
Now, you have a new library, which will act as a shell/host for all your stories.
Configure the new library to use Storybook
Now let’s configure our new library to use Storybook, using the @nx/storybook:configuration
generator. Run:
❯
nx g @nx/storybook:configuration storybook-host --interactionTests=true --uiFramework=@storybook/react-vite
This generator will only create the libs/storybook-host/.storybook
folder. It will also infer the tasks: storybook
, build-storybook
, and test-storybook
. This is all we care about. We don’t need any stories for this project since we will import the stories from other projects in our workspace. So, if you want, you can delete the contents of the src/lib
folder.
If you're on an Nx version lower than 18 or have opted out of using inferred tasks, the storybook
, build-storybook
, and test-storybook
targets will be explicitly defined in the libs/storybook-host/project.json
file.
Import the stories in our library's main.ts
Now it’s time to import the stories of our other projects in our new library's ./storybook/main.ts
.
Here is a sample libs/storybook-host/.storybook/main.ts
file:
1import type { StorybookConfig } from '@storybook/react-vite';
2import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
3import { mergeConfig } from 'vite';
4
5const config: StorybookConfig = {
6 stories: ['../../**/ui/**/src/lib/**/*.stories.@(js|jsx|ts|tsx|mdx)'],
7 addons: ['@storybook/addon-essentials', '@storybook/addon-interactions'],
8 framework: {
9 name: '@storybook/react-vite',
10 options: {},
11 },
12
13 viteFinal: async (config) =>
14 mergeConfig(config, {
15 plugins: [nxViteTsPaths()],
16 }),
17};
18
19export default config;
20
Notice how we only link the stories matching a specific pattern. According to your workspace set-up, you can adjust the pattern, or add more patterns, so that you can match all the stories in all the projects you want.
For example:
1// ...
2const config: StorybookConfig = {
3 stories: [
4 '../../**/ui/**/src/lib/**/*.stories.@(js|jsx|ts|tsx|mdx)',
5 '../../**/src/lib/**/*.stories.@(js|jsx|ts|tsx|mdx)',
6 // ...
7 ],
8 // ...
9};
10
If you're using Angular add the stories in your tsconfig.json
Here is a sample libs/storybook-host/.storybook/tsconfig.json
file:
1{
2 "extends": "../tsconfig.json",
3 "compilerOptions": {
4 "emitDecoratorMetadata": true
5 },
6 "exclude": ["../**/*.spec.ts"],
7 "include": ["../../**/ui/**/src/lib/**/*.stories.ts", "*.ts"]
8}
9
Notice how in the include
array we are specifying the paths to our stories, using the same pattern we used in our .storybook/main.ts
.
Serve or build your Storybook
Now you can serve, test or build your Storybook as you would, normally. And then you can publish the bundled app!
❯
nx storybook storybook-host
or
❯
nx build-storybook storybook-host
or
❯
nx test-storybook storybook-host
Use cases that apply to this solution
Can be used for:
- Workspaces with multiple apps and libraries, all using a single framework
Ideal for:
- Workspaces with a single app and multiple libraries all using a single framework
Extras - Dependencies
Your new Storybook host, essentially, depends on all the projects from which it is importing stories. This means whenever one of these projects updates a component, or updates a story, our Storybook host would have to rebuild, to reflect these changes. It cannot rely on the cached result. However, Nx does not understand the imports in libs/storybook-host/.storybook/main.ts
, and the result is that Nx does not know which projects the Storybook host depends on, based solely on the main.ts
imports.
The good thing is that there is a solution to this. You can manually add the projects your Storybook host depends on as implicit dependencies in your project’s project.json
file:
1{
2 "$schema": "../../node_modules/nx/schemas/project-schema.json",
3 "sourceRoot": "libs/storybook-host/src",
4 "projectType": "library",
5 "tags": ["type:storybook"],
6 "implicitDependencies": [
7 "admin-ui-footer",
8 "admin-ui-header",
9 "client-ui-footer",
10 "client-ui-header",
11 "shared-ui-button",
12 "shared-ui-main",
13 "shared-ui-notification"
14 ]
15}
16