Setting up Storybook Composition with Nx

What is Storybook Composition

As explained in the Storybook official docs, Storybook Composition allows you to embed components from any Storybook inside your local Storybook. If you want to learn more about Storybook Composition, please take a look at the following articles, which explain it in detail:

How it works

In essence, you have a Storybook running, which will be the host of the embeded Storybooks as well. Then, you provide this "host" Storybook with a URL of a live/running Storybook. The composed Storybook is then displayed in a new Canvas iframe as part of the host Storybook, and is listed on the left-hand-side stories inventory, too. You can read more about this in the docs listed above.

How to use it

All you need is a URL of a live Storybook, and a "host" Storybook. In the .storybook/main.js file of the "host" Storybook, inside module.exports you add a new refs attribute, which will contain the link(s) for the composed Storybook(s).

In the example below, we have a host Storybook running on local port 4400 (http://localhost:4400) - not displayed here. In it, we want to compose three other Storybooks. The "one-composed" and "two-composed", running on local ports 4401 and 4402 accordingly, as well as the Storybook website's Storybook which is live on the address that you see.

1// .storybook/main.js of our Host Storybook - assuming it's running on port 4400 2module.exports = { 3 ..., 4 refs: { 5 'one-composed': { 6 title: 'One composed', 7 url: 'http://localhost:4401', 8 }, 9 'two-composed': { 10 title: 'Two composed', 11 url: 'http://localhost:4402', 12 }, 13 'storybook-website-storybook': { 14 title: 'The Storybook of the Storybook website', 15 url: '', 16 }, 17 }, 18}; 19

You can always read more in the official Storybook docs.

How to use it in Nx

It's quite easy to use this feature, in Nx and in general, since you do not need to make any code changes, you just need to have the "composed" Storybook instances (the ones you need to "compose") running, choose a "host" Storybook, and just add the composed Storybooks in it's .storybook/main.js file.

Nx provides the run-many command, which will allow you to easily run multiple Storybooks at the same time. You need to run the run-many command with the parallel flag (eg. --parallel=3), because you want to run all your Storybooks in parallel. You can change the value of the parallel flag to be of as many Storybooks you want to run in parallel as you need. However, be very carefull with putting large numbers in this flag, since it can cause big delays or get stuck. You can play around and adjust that number to one your machine runs comfortably with. Keep in mind that you can add in this feature however many live/public Storybooks as you need (Storybooks that you do not run locally).

In order to get it working for you, you need to two things:

  1. Make sure your "composed" Storybook instances are running. For that you can do:

nx run-many -t storybook -p one-composed two-composed three-composed --parallel=3

  1. Start your host Storybook in another tab of your terminal:

nx storybook main-host

Before doing the above steps to actually compose our Storybook instances under the main-host project, we would need to do the following adjustments to our workspace:

Adjust the Storybook ports in project.json

Take a look in your project.json file of each one of your projects (e.g. for the main-host project, you can find it in the path apps/main-host/project.json). In your project's targets, in the storybook target, you will notice that the default port that Nx assigns to your projects' Storybook is always 4400:

1{ 2 ... 3 "targets": { 4 ... 5 "storybook": { 6 ... 7 "options": { 8 ... 9 "port": 4400, 10 ... 11 }, 12 ... 13 }, 14 ... 15` }, 16} 17

We can keep this port for the project which will serve as the host of our configuration, but we must change the port numbers of the other projects, the projects which will be composed/composed. The reason for that is the following:

  • When the nx run-many -t storybook --parallel=3 command executes, it will go and look into your project.json file to see the port you have assigned for that project's Storybook.
  • When it finds a port that it is already used, it will change the port number randomly (usually adding 1 until it finds an empty port).

Since we are using the --parallel flag, and the commands are executed in parallel, we cannot know for sure the order that the storybook command will be executed. So, we cannot be sure which port will correspond to which of the projects.

If we don't change the port numbers, and there are projects that want to use the same port for their Storybooks, the run-many command will change that port, and the result will be that we will not know for sure which of our projects runs on which port. The problem that this creates is that we will not be able to create the proper configuration for Storybook Composition, since we will not be able to tell which URLs our composed Storybooks run on.

Add the refs in our host project's .storybook/main.js file

Now, we need to add to our host project's main.js file (the path of which would be apps/main-host/.storybook/main.js) a refs object, to configure our composition. An example of such a configuration looks like this:

1module.exports = { 2 ..., 3 refs: { 4 one-composed: { 5 title: 'One composed', 6 url: 'http://localhost:4401', 7 }, 8 two-composed: { 9 title: 'Two composed', 10 url: 'http://localhost:4402', 11 }, 12 three-composed: { 13 title: 'Three composed', 14 url: 'http://localhost:4403', 15 }, 16 }, 17}; 18

Optional: use run-commands and create a storybook-composition target

If you want to take advantage of the run-commands functionality of Nx, you can create a custom target that will invoke the run-parallel command for your "composed" Storybook instances.

The objective is to end up with a new target in your main-host's project.json file (apps/main-host/project.json) that looks like this:

1 "storybook-composition": { 2 "executor": "nx:run-commands", 3 "options": { 4 "commands": [ 5 "nx storybook one-composed", 6 "nx storybook two-composed", 7 "nx storybook three-composed" 8 ], 9 "parallel": true 10 } 11 } 12

which you can then invoke like this:

nx run main-host:storybook-composition

which will take care of starting all your "composed" Storybook instances, before you run nx storybook main-host.

Generating a new target in our main-host

Let's first generate a new target called storybook-composition for our main-host.

Run the following command:

nx generate nx:run-commands storybook-composition --command='nx storybook one-composed' --project=main-host

This will create a new target in your apps/main-host/project.json:

1 "storybook-composition": { 2 "executor": "nx:run-commands", 3 "outputs": [], 4 "options": { 5 "command": "nx storybook one-composed" 6 } 7 } 8

Now, change the command option to be commands, add the "parallel": true option, and add all the other "composed" Storybook commands:

1 "storybook-composition": { 2 "executor": "nx:run-commands", 3 "options": { 4 "commands": [ 5 "nx storybook one-composed", 6 "nx storybook two-composed", 7 "nx storybook three-composed" 8 ], 9 "parallel": true 10 } 11 } 12

Now, you can start all your "composed" Storybook instances by running:

nx run main-host:storybook-composition

After all of your "composed" Storybook instances have started, you can run in a new terminal:

nx storybook main-host

This approach takes the "burden" of writing the run-many command manually, and makes it easier to add/remove "composed" Storybook instances.