Skip to content

Commit

Permalink
Update storybook-composition-setup.md (#10510)
Browse files Browse the repository at this point in the history
  • Loading branch information
romhayh committed Jun 9, 2022
1 parent 56dbb0d commit 9fcf6f8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/generated/packages/storybook.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"id": "storybook-composition-setup",
"name": "Setting up Storybook Composition with Nx",
"file": "shared/guides/storybook/storybook-composition-setup",
"content": "# Setting up Storybook Composition with Nx\n\n## What is Storybook Compotision\n\nAs explained in the [Storybook official docs](https://storybook.js.org/docs/angular/workflows/storybook-composition), 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:\n\n- [Storybook Composition - Chromatic blog](https://www.chromatic.com/blog/storybook-composition/)\n- [Storybook Composition - Storybook docs](https://storybook.js.org/docs/angular/workflows/storybook-composition)\n\n## How it works\n\nIn 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.\n\n## How to use it\n\nAll 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).\n\nIn 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](https://next--storybookjs.netlify.app/official-storybook) which is live on the address that you see.\n\n```javascript\n// .storybook/main.js of our Host Storybook - assuming it's running on port 4400\nmodule.exports = {\n ...,\n refs: {\n 'one-composed': {\n title: 'One composed',\n url: 'http://localhost:4401',\n },\n 'two-composed': {\n title: 'Two composed',\n url: 'http://localhost:4402',\n },\n 'storybook-website-storybook': {\n title: 'The Storybook of the Storybook website',\n url: 'https://next--storybookjs.netlify.app/official-storybook/',\n },\n },\n};\n```\n\nYou can always read more in the [official Storybook docs](https://storybook.js.org/docs/angular/workflows/storybook-composition#compose-published-storybooks).\n\n## How to use it in Nx\n\nIt'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.\n\nNx provides the [`run-many`](https://nx.dev/l/a/cli/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=4`), 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\nflag, 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).\n\nYou can just do:\n\n```bash\nnx run-many --target=storybook --projects=main-host,one-composed,two-composed,three-composed --parallel=4\n```\n\nBefore running the above command to actually compose our Storybook instances under the **`main-host`** project, we would need to do the following adjustments to our workspace:\n\n### Adjust the Storybook ports in `project.json`\n\nTake a look in your `project.json` file of each one of your projects (eg. for the `main-host` project, you can find it in the path `apps/main-host/project.json`).\nIn 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`:\n\n```json\n{\n ...\n \"targets\": {\n ...\n \"storybook\": {\n ...\n \"options\": {\n ...\n \"port\": 4400,\n ...\n },\n ...\n },\n ...\n` },\n}\n```\n\nWe 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:\n\n- When the `nx run-many --target=storybook --parallel=4` command executes, it will go and look into your `project.json` file to see the port you have assigned for that project's Storybook.\n- 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).\n\nSince 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.\n\nIf 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\nof 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.\n\n### Add the refs in our host project's `.storybook/main.js` file\n\nNow, 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:\n\n```javascript\nmodule.exports = {\n ...,\n refs: {\n one-composed: {\n title: 'One composed',\n url: 'http://localhost:4401',\n },\n two-composed: {\n title: 'Two composed',\n url: 'http://localhost:4402',\n },\n three-composed: {\n title: 'Three composed',\n url: 'http://localhost:4403',\n },\n },\n};\n```\n"
"content": "# Setting up Storybook Composition with Nx\n\n## What is Storybook Composition\n\nAs explained in the [Storybook official docs](https://storybook.js.org/docs/angular/workflows/storybook-composition), 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:\n\n- [Storybook Composition - Chromatic blog](https://www.chromatic.com/blog/storybook-composition/)\n- [Storybook Composition - Storybook docs](https://storybook.js.org/docs/angular/workflows/storybook-composition)\n\n## How it works\n\nIn 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.\n\n## How to use it\n\nAll 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).\n\nIn 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](https://next--storybookjs.netlify.app/official-storybook) which is live on the address that you see.\n\n```javascript\n// .storybook/main.js of our Host Storybook - assuming it's running on port 4400\nmodule.exports = {\n ...,\n refs: {\n 'one-composed': {\n title: 'One composed',\n url: 'http://localhost:4401',\n },\n 'two-composed': {\n title: 'Two composed',\n url: 'http://localhost:4402',\n },\n 'storybook-website-storybook': {\n title: 'The Storybook of the Storybook website',\n url: 'https://next--storybookjs.netlify.app/official-storybook/',\n },\n },\n};\n```\n\nYou can always read more in the [official Storybook docs](https://storybook.js.org/docs/angular/workflows/storybook-composition#compose-published-storybooks).\n\n## How to use it in Nx\n\nIt'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.\n\nNx provides the [`run-many`](https://nx.dev/l/a/cli/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=4`), 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\nflag, 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).\n\nYou can just do:\n\n```bash\nnx run-many --target=storybook --projects=main-host,one-composed,two-composed,three-composed --parallel=4\n```\n\nBefore running the above command to actually compose our Storybook instances under the **`main-host`** project, we would need to do the following adjustments to our workspace:\n\n### Adjust the Storybook ports in `project.json`\n\nTake a look in your `project.json` file of each one of your projects (eg. for the `main-host` project, you can find it in the path `apps/main-host/project.json`).\nIn 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`:\n\n```json\n{\n ...\n \"targets\": {\n ...\n \"storybook\": {\n ...\n \"options\": {\n ...\n \"port\": 4400,\n ...\n },\n ...\n },\n ...\n` },\n}\n```\n\nWe 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:\n\n- When the `nx run-many --target=storybook --parallel=4` command executes, it will go and look into your `project.json` file to see the port you have assigned for that project's Storybook.\n- 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).\n\nSince 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.\n\nIf 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\nof 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.\n\n### Add the refs in our host project's `.storybook/main.js` file\n\nNow, 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:\n\n```javascript\nmodule.exports = {\n ...,\n refs: {\n one-composed: {\n title: 'One composed',\n url: 'http://localhost:4401',\n },\n two-composed: {\n title: 'Two composed',\n url: 'http://localhost:4402',\n },\n three-composed: {\n title: 'Three composed',\n url: 'http://localhost:4403',\n },\n },\n};\n```\n"
}
],
"generators": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Setting up Storybook Composition with Nx

## What is Storybook Compotision
## What is Storybook Composition

As explained in the [Storybook official docs](https://storybook.js.org/docs/angular/workflows/storybook-composition), 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:

Expand Down

1 comment on commit 9fcf6f8

@vercel
Copy link

@vercel vercel bot commented on 9fcf6f8 Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.