Skip to content

Commit

Permalink
feat(storybook): change angular generator for storybook configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Mar 16, 2022
1 parent 615bd73 commit d6c2ffc
Show file tree
Hide file tree
Showing 36 changed files with 2,038 additions and 675 deletions.
28 changes: 28 additions & 0 deletions docs/generated/api-angular/generators/change-storybook-targets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: '@nrwl/angular:change-storybook-targets generator'
description: 'Change Storybook targets to use native Storybook builders'
---

# @nrwl/angular:change-storybook-targets

Change Storybook targets to use native Storybook builders

## Usage

```bash
nx generate change-storybook-targets ...
```

By default, Nx will search for `change-storybook-targets` in the default collection provisioned in `workspace.json`.

You can specify the collection explicitly as follows:

```bash
nx g @nrwl/angular:change-storybook-targets ...
```

Show what will be generated without writing to disk:

```bash
nx g change-storybook-targets ... --dry-run
```
14 changes: 1 addition & 13 deletions docs/generated/api-storybook/executors/storybook.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Default: `@storybook/angular`

Type: `string`

Possible values: `@storybook/angular`, `@storybook/react`, `@storybook/html`, `@storybook/web-components`, `@storybook/vue`, `@storybook/vue3`, `@storybook/svelte`
Possible values: `@storybook/react`, `@storybook/html`, `@storybook/web-components`, `@storybook/vue`, `@storybook/vue3`, `@storybook/svelte`

Storybook framework npm package

Expand Down Expand Up @@ -87,18 +87,6 @@ Type: `array`

Directory where to load static files from, array of strings

### stylePreprocessorOptions.includePaths

Type: `array`

Options to pass to style preprocessors. The paths to include. Paths will be resolved to workspace root. This is for Angular projects only, as an option for the Storybook/Angular builder. It will be ignored in non-Angular projects.

### styles

Type: `array`

Global styles to be included in the build. This is for Angular projects only, as an option for the Storybook/Angular builder. It will be ignored in non-Angular projects.

### watch

Default: `true`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: '@nrwl/storybook:change-storybook-targets generator'
description: 'Change Storybook targets to use native Storybook builders'
---

# @nrwl/storybook:change-storybook-targets

Change Storybook targets to use native Storybook builders

## Usage

```bash
nx generate change-storybook-targets ...
```

By default, Nx will search for `change-storybook-targets` in the default collection provisioned in `workspace.json`.

You can specify the collection explicitly as follows:

```bash
nx g @nrwl/storybook:change-storybook-targets ...
```

Show what will be generated without writing to disk:

```bash
nx g change-storybook-targets ... --dry-run
```
21 changes: 20 additions & 1 deletion docs/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,16 @@
"id": "storybook-migrate-stories-to-6-2",
"file": "generated/api-angular/generators/storybook-migrate-stories-to-6-2"
},
{
"name": "change-storybook-targets generator",
"id": "change-storybook-targets",
"file": "generated/api-angular/generators/change-storybook-targets"
},
{
"name": "change-storybook-targets generator",
"id": "change-storybook-targets",
"file": "generated/api-angular/generators/change-storybook-targets"
},
{
"name": "upgrade-module generator",
"id": "upgrade-module",
Expand Down Expand Up @@ -943,7 +953,16 @@
"id": "migrate-stories-to-6-2",
"file": "generated/api-storybook/generators/migrate-stories-to-6-2"
},

{
"name": "change-storybook-targets generator",
"id": "change-storybook-targets",
"file": "generated/api-storybook/generators/change-storybook-targets"
},
{
"name": "change-storybook-targets generator",
"id": "change-storybook-targets",
"file": "generated/api-storybook/generators/change-storybook-targets"
},
{
"name": "Executors: Build",
"id": "executors-build",
Expand Down
57 changes: 10 additions & 47 deletions docs/shared/guides/storybook/plugin-angular.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,77 +188,40 @@ describe('shared-ui', () => {
});
```

### Setting up `projectBuildConfig`
### Storybook uses `browserTarget` for Angular

Storybook for Angular needs a default project specified in order to run. The reason is that it uses that default project to read the build configuration from (paths to files to include in the build, and other configurations/settings). In Nx workspaces, that project is specified with the `projectBuildConfig` property.

If you're using Nx version `>=13.4.6` either in a new Nx workspace, or you migrated your older Nx workspace to Nx version `>=13.4.6`, Nx will automatically add the `projectBuildConfig` property in your projects `project.json` files, for projects that are using Storybook. It will look like this:
Nx is using the original Storybook executor for Angular to serve and build Storybook. If you're using Storybook in
your Angular project, you will notice that `browserTarget` is specified for the `storybook` and `build-storybook` targets, much like it is done for `serve` or other targets. Angular needs the `browserTarget` for Storybook in order to know which configuration to use for the build. If your project is buildable (it has a `build` target) the `browserTarget` for Storybook will use the `build` target, if it's not buildable it will use the `build-storybook` target.

```json
"storybook": {
"executor": "@nrwl/storybook:storybook",
"executor": "@storybook/angular:start-storybook",
"options": {
...
"projectBuildConfig": "my-project:build-storybook"
"browserTarget": "my-project:build"
},
...
},
"build-storybook": {
"executor": "@nrwl/storybook:build",
"executor": "@storybook/angular:build-storybook",
...
"options": {
...
"projectBuildConfig": "my-project:build-storybook"
"browserTarget": "my-project:build"
},
...
}
```

This setup instructs Nx to use the configuration under the `build-storybook` target of `my-project` when using the `storybook` and `build-storybook` executors.

If the `projectBuildConfig` is not set in your `project.json`, you can manually set it up in one of the following ways:

#### Adding the `projectBuildConfig` option directly in the project's `project.json`

In your project's `project.json` file find the `storybook` and `build-storybook` targets. Add the `projectBuildConfig` property under the `options` as shown above.

After you add this property, you can run your `storybook` and `build-storybook` executors as normal:

```bash
nx storybook my-project
```

and

```bash
nx build-storybook my-project
```

#### Using the `projectBuildConfig` flag on the executors

The way you would run your `storybook` and your `build-storybook` executors would be:

```bash
nx storybook my-project --projectBuildConfig=my-project:build-storybook
```

and

```bash
nx build-storybook my-project --projectBuildConfig=my-project:build-storybook
```

**Note:** If your project is buildable (eg. any project that has a `build` target set up in its `project.json`) you can also do `nx storybook my-project --projectBuildConfig=my-project`.

> In a pure Angular/Storybook setup (**not** an Nx workspace), the Angular application/project would have an `angular.json` file. That file would have a property called `defaultProject`. In an Nx workspace the `defaultProject` property would be specified in the `nx.json` file. Previously, Nx would try to resolve the `defaultProject` of the workspace, and use the build configuration of that project. In most cases, the `defaultProject`'s build configuration would not work for some other project set up with Storybook, since there would most probably be mismatches in paths or other project-specific options.
This setup instructs Nx to use the configuration under the `build` target of `my-project` when using the `storybook` and `build-storybook` executors.

### Configuring styles and preprocessor options

Angular supports including extra entry-point files for styles. Also, in case you use Sass, you can add extra base paths that will be checked for imports. In your project's `project.json` file you can use the `styles` and `stylePreprocessorOptions` properties in your `storybook` and `build-storybook` target `options`, as you would in your Storybook or your Angular configurations. Check out the [Angular Workspace Configuration](https://angular.io/guide/workspace-config#styles-and-scripts-configuration) documentation for more information.

```json
"storybook": {
"executor": "@nrwl/storybook:storybook",
"executor": "@storybook/angular:start-storybook",
"options": {
...
"styles": ["some-styles.css"],
Expand All @@ -269,7 +232,7 @@ Angular supports including extra entry-point files for styles. Also, in case you
...
},
"build-storybook": {
"executor": "@nrwl/storybook:build",
"executor": "@storybook/angular:build-storybook",
...
"options": {
...
Expand Down
11 changes: 11 additions & 0 deletions packages/angular/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@
"factory": "./src/generators/web-worker/compat",
"schema": "./src/generators/web-worker/schema.json",
"description": "Creates a Web Worker."
},
"change-storybook-targets": {
"factory": "./src/generators/change-storybook-targets/compat",
"schema": "./src/generators/change-storybook-targets/schema.json",
"description": "Change Storybook targets to use native Storybook builders",
"hidden": false
}
},
"generators": {
Expand Down Expand Up @@ -300,6 +306,11 @@
"factory": "./src/generators/web-worker/web-worker",
"schema": "./src/generators/web-worker/schema.json",
"description": "Creates a Web Worker."
},
"change-storybook-targets": {
"factory": "./src/generators/change-storybook-targets/change-storybook-targets",
"schema": "./src/generators/change-storybook-targets/schema.json",
"description": "Change Storybook targets to use native Storybook builders"
}
}
}
1 change: 1 addition & 0 deletions packages/angular/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from './src/generators/component-cypress-spec/component-cypress-spec';
export * from './src/generators/component-story/component-story';
export * from './src/generators/web-worker/web-worker';
export * from './src/generators/mfe-remote/mfe-remote';
export * from './src/generators/change-storybook-targets/change-storybook-targets';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Tree } from '@nrwl/devkit';
import { changeStorybookTargetsGenerator } from '@nrwl/storybook';

export async function angularChangeStorybookTargestGenerator(tree: Tree) {
await changeStorybookTargetsGenerator(tree);
}

export default angularChangeStorybookTargestGenerator;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { convertNxGenerator } from '@nrwl/devkit';
import angularChangeStorybookTargestGenerator from './change-storybook-targets';

export default convertNxGenerator(angularChangeStorybookTargestGenerator);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "NxAngularChangeStorybookTargetsGenerator",
"type": "object",
"cli": "nx",
"properties": {},
"additionalProperties": false,
"required": []
}
1 change: 1 addition & 0 deletions packages/devkit/src/executors/parse-target-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { Target } from 'nx/src/commands/run';
*/
export function parseTargetString(targetString: string): Target {
const [project, target, configuration] = targetString.split(':');
console.log('KATERINA', project, target, configuration);
if (!project || !target) {
throw new Error(`Invalid Target String: ${targetString}`);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/storybook/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
"schema": "./src/generators/migrate-stories-to-6-2/schema.json",
"description": "Migrate stories syntax to 6.2",
"hidden": false
},
"change-storybook-targets": {
"factory": "./src/generators/change-storybook-targets/change-storybook-targets#changeStorybookTargetsSchematic",
"schema": "./src/generators/change-storybook-targets/schema.json",
"description": "Change Storybook targets to use native Storybook builders",
"hidden": false
}
},
"generators": {
Expand Down Expand Up @@ -65,6 +71,12 @@
"schema": "./src/generators/migrate-stories-to-6-2/schema.json",
"description": "Generate default Storybook configuration files using Storybook version >=6.x specs, for projects that already have Storybook instances and configurations of versions <6.x.",
"hidden": false
},
"change-storybook-targets": {
"factory": "./src/generators/change-storybook-targets/change-storybook-targets",
"schema": "./src/generators/change-storybook-targets/schema.json",
"description": "Change Storybook targets to use native Storybook builders",
"hidden": false
}
}
}
1 change: 1 addition & 0 deletions packages/storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export { configurationGenerator } from './src/generators/configuration/configura
export { cypressProjectGenerator } from './src/generators/cypress-project/cypress-project';
export { migrateDefaultsGenerator } from './src/generators/migrate-defaults-5-to-6/migrate-defaults-5-to-6';
export { migrateStoriesTo62Generator } from './src/generators/migrate-stories-to-6-2/migrate-stories-to-6-2';
export { changeStorybookTargetsGenerator } from './src/generators/change-storybook-targets/change-storybook-targets';
export { storybookVersion } from './src/utils/versions';
6 changes: 6 additions & 0 deletions packages/storybook/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
"cli": "nx",
"description": "Add projectBuildConfig option to project's Storybook config.",
"factory": "./src/migrations/update-13-4-6/set-project-build-config"
},
"update-14.0.0": {
"version": "14.0.0",
"cli": "nx",
"description": "Change Storybook targets to use native Storybook builders",
"factory": "./src/migrations/update-14-0-0/change-storybook-targets"
}
},
"packageJsonUpdates": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ describe('Build storybook', () => {
jest.spyOn(fileUtils, 'readPackageJson').mockReturnValue({
devDependencies: {
'@storybook/addon-essentials': '~6.2.9',
'@storybook/angular': '~6.2.9',
'@storybook/react': '~6.2.9',
},
});
uiFramework = '@storybook/angular';
uiFramework = '@storybook/react';
outputPath = '/root/dist/storybook';
config = {
pluginPath: join(
Expand All @@ -43,11 +43,6 @@ describe('Build storybook', () => {
options = {
uiFramework,
outputPath,
projectBuildConfig: 'proj',
stylePreprocessorOptions: {
includePaths: ['my-path/my-style-options'],
},
styles: ['my-other-path/my-other-styles.scss'],
config,
};

Expand All @@ -64,12 +59,22 @@ describe('Build storybook', () => {
sourceRoot: 'src',
targets: {
build: {
executor: '@angular-devkit/build-angular:browser',
executor: '@nrwl/web:webpack',
options: {
main: 'apps/proj/src/main.ts',
outputPath: 'dist/apps/proj',
tsConfig: 'apps/proj/tsconfig.app.json',
index: 'apps/proj/src/index.html',
compiler: 'babel',
outputPath: 'dist/apps/webre',
index: 'apps/webre/src/index.html',
baseHref: '/',
main: 'apps/webre/src/main.tsx',
polyfills: 'apps/webre/src/polyfills.ts',
tsConfig: 'apps/webre/tsconfig.app.json',
assets: [
'apps/webre/src/favicon.ico',
'apps/webre/src/assets',
],
styles: ['apps/webre/src/styles.css'],
scripts: [],
webpackConfig: '@nrwl/react/plugins/webpack',
},
},
storybook: {
Expand All @@ -79,7 +84,6 @@ describe('Build storybook', () => {
},
},
},
defaultProject: 'proj',
npmScope: 'test',
},
isVerbose: false,
Expand Down

0 comments on commit d6c2ffc

Please sign in to comment.