Publishing Storybook: One Storybook instance per scope

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 Storybook per scope (eg. theme, app, framework) for your workspace.

Sometimes, you have multiple apps and libraries, and each of these is associated with a specific scope. You can read more about grouping libraries and scoping them in the Library Types documentation page, and also in the Code Organization and Naming Conventions documentation section.

In this case, you can have one Storybook instance per scope. If you follow the folder organization convention described above, it is easy to configure Storybook to import all the stories under a specific folder, for example, which are associated with a specific app or scope.

Structure of the folders

Say, for example, that you have a client app, an admin app, and a number of UI libraries, organized under the name of each app. So you would have a folder structure that looks like this:

1happynrwl/ 2โ”œโ”€โ”€ apps/ 3โ”‚ โ”œโ”€โ”€ client/ 4โ”‚ โ”œโ”€โ”€ admin/ 5โ”œโ”€โ”€ libs/ 6โ”‚ โ”œโ”€โ”€ client/ 7โ”‚ โ”‚ โ”œโ”€โ”€ feature/ 8โ”‚ โ”‚ โ”œโ”€โ”€ ui/ 9| โ”‚ โ”‚ โ”œโ”€โ”€ header/ 10| | | | โ”œโ”€โ”€ .storybook/ 11| | | | โ”œโ”€โ”€ src/ 12| | | | | โ”œโ”€โ”€lib 13| | | | | | โ”œโ”€โ”€my-header 14| | | | | | | โ”œโ”€โ”€ my-header.component.ts 15| | | | | | | โ”œโ”€โ”€ my-header.component.stories.ts 16| | | | | | | โ””โ”€โ”€ etc... 17| | | | | | โ””โ”€โ”€ etc... 18| | | | | โ””โ”€โ”€ etc... 19| | | | โ”œโ”€โ”€ README.md 20| | | | โ”œโ”€โ”€ tsconfig.json 21| | | | โ””โ”€โ”€ etc... 22โ”‚ โ”‚ โ””โ”€โ”€ utils/ 23โ”‚ โ”œโ”€โ”€ admin/ 24โ”‚ โ”‚ โ”œโ”€โ”€ feature/ 25โ”‚ โ”‚ โ”œโ”€โ”€ ui/ 26| โ”‚ โ”‚ โ”œโ”€โ”€ dashboard/ 27| | | | โ”œโ”€โ”€ .storybook/ 28| | | | โ”œโ”€โ”€ src/ 29| | | | | โ”œโ”€โ”€ etc.. 30| | | | โ”œโ”€โ”€ README.md 31| | | | โ”œโ”€โ”€ tsconfig.json 32| | | | โ””โ”€โ”€ etc... 33โ”‚ โ”‚ โ””โ”€โ”€ utils/ 34โ”‚ โ””โ”€โ”€ shared/ 35โ”‚ โ”œโ”€โ”€ ui/ 36| โ”‚ โ”œโ”€โ”€ cta/ 37| | | โ”œโ”€โ”€ .storybook/ 38| | | โ”œโ”€โ”€ src/ 39| | | | โ”œโ”€โ”€ etc.. 40| | | โ”œโ”€โ”€ README.md 41| | | โ”œโ”€โ”€ tsconfig.json 42| | | โ””โ”€โ”€ etc... 43โ”‚ โ””โ”€โ”€ utils/ 44โ”œโ”€โ”€ tools/ 45โ”œโ”€โ”€ nx.json 46โ”œโ”€โ”€ package.json 47โ””โ”€โ”€ tsconfig.base.json 48

In this case you can see that we have two deployable applications, client and admin, and we have a number of UI libraries, each associated with a specific app. For example, client-ui-header is a UI library associated with the client app, and admin-ui-dashboard is a UI library associated with the admin app. We also have one more library, the shared-ui-cta library, which is shared between the two apps. The way we have structured our folders is such that any new library that is related to the client app will go in the libs/client folder, and in that folder we have a sub-folder to determine if the new library is related to ui or anything else. The same applies to the admin app. Any library shared between the two apps will live under a subfolder of the libs/shared folder.

Notice how we have already generated Storybook configuration and stories for all of our ui libraries.

Setting up the thematic Storybook instances

Now, we want to have one Storybook instance per thematic scope. This is quite easy to implement since we are following this specific folder structure.

First of all, we need to generate three new libraries (or as many as our "thematic scopes"), which will host all the stories for each specific scope. We can do this following the same steps described above.

It's important to note that if we want to combine stories from different libraries in the same Storybook instance, the stories need to use the same framework.

Let's assume in this case that all our libraries are using Angular.

Generate the libraries

Let's generate three Angular libraries, one for each scope, and let's call them storybook-host-client, storybook-host-admin, and storybook-host-shared. We can do this by running the following commands:

Directory Flag Behavior Changes

The commands 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/angular:lib storybook-host-client --directory=libs/storybook-host-client --projectNameAndRootFormat=as-provided

โฏ

nx g @nx/angular:lib storybook-host-admin --directory=libs/storybook-host-admin --projectNameAndRootFormat=as-provided

โฏ

nx g @nx/angular:lib storybook-host-shared --directory=libs/storybook-host-shared --projectNameAndRootFormat=as-provided

Generate the Storybook configuration for the libraries

Now, we need to generate Storybook configuration for all these new libraries. We don't want to generate stories for these libraries, so we can run the following commands:

โฏ

nx g @nx/storybook:configuration storybook-host-client --uiFramework=@storybook/angular --interactionTests=true

โฏ

nx g @nx/storybook:configuration storybook-host-admin --uiFramework=@storybook/angular --interactionTests=true

โฏ

nx g @nx/storybook:configuration storybook-host-shared --uiFramework=@storybook/angular --interactionTests=true

Import the stories

Now that our Storybook configuration is ready for our new libraries, we can go ahead and import the stories!

Thanks to our folder structure, we can easily configure Storybook to import all the stories under a specific folder, for example, which are associated with a specific scope.

For example, libs/storybook-host-admin/.storybook/main.ts:

libs/storybook-host-admin/.storybook/main.ts
1import type { StorybookConfig } from '@storybook/angular'; 2 3const config: StorybookConfig = { 4 stories: ['../../admin/ui/**/src/lib/**/*.stories.ts'], 5 addons: ['@storybook/addon-essentials', '@storybook/addon-interactions'], 6 framework: { 7 name: '@storybook/angular', 8 options: {}, 9 }, 10}; 11 12export default config; 13

And don't forget the libs/storybook-host-admin/.storybook/tsconfig.json:

libs/storybook-host-admin/.storybook/tsconfig.json
1{ 2 "extends": "../tsconfig.json", 3 "compilerOptions": { 4 "emitDecoratorMetadata": true 5 }, 6 "exclude": ["../**/*.spec.ts"], 7 "include": ["../../admin/ui/**/src/lib/**/*.stories.ts", "*.ts"] 8} 9

Use cases that apply to this solution

  • Workspaces with multiple apps and libraries, all using a single framework

  • Workspaces that use scopes and follow the suggested folder structure

  • Workspaces that have multiple apps and libs divided by theme and by framework, that do not mind having more than one Storybook

Extras - Dependencies

In this example, you can still use the implicit dependencies to manually tell Nx which projects your new libraries depend on.