Skip to content

Commit 86fa10b

Browse files
Phillip9587xiongemi
authored andcommitted
feat(storybook): use createNodesV2 for init and convert-to-inferred generators
1 parent 8563a46 commit 86fa10b

File tree

4 files changed

+165
-111
lines changed

4 files changed

+165
-111
lines changed

packages/storybook/src/generators/convert-to-inferred/convert-to-inferred.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {
77
} from '@nx/devkit';
88
import { AggregatedLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';
99
import {
10-
migrateProjectExecutorsToPluginV1,
10+
migrateProjectExecutorsToPlugin,
1111
NoTargetsToMigrateError,
1212
} from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator';
1313
import { buildPostTargetTransformer } from './lib/build-post-target-transformer';
1414
import { servePostTargetTransformer } from './lib/serve-post-target-transformer';
15-
import { createNodes } from '../../plugins/plugin';
15+
import { createNodesV2 } from '../../plugins/plugin';
1616
import { storybookVersion } from '../../utils/versions';
1717

1818
interface Schema {
@@ -23,11 +23,11 @@ interface Schema {
2323
export async function convertToInferred(tree: Tree, options: Schema) {
2424
const projectGraph = await createProjectGraphAsync();
2525
const migrationLogs = new AggregatedLog();
26-
const migratedProjects = await migrateProjectExecutorsToPluginV1(
26+
const migratedProjects = await migrateProjectExecutorsToPlugin(
2727
tree,
2828
projectGraph,
2929
'@nx/storybook/plugin',
30-
createNodes,
30+
createNodesV2,
3131
{
3232
buildStorybookTargetName: 'build-storybook',
3333
serveStorybookTargetName: 'storybook',

packages/storybook/src/generators/init/init.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
updateJson,
1111
updateNxJson,
1212
} from '@nx/devkit';
13-
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
13+
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
1414
import { gte } from 'semver';
15-
import { createNodes } from '../../plugins/plugin';
15+
import { createNodesV2 } from '../../plugins/plugin';
1616
import {
1717
getInstalledStorybookVersion,
1818
storybookMajorVersion,
@@ -99,11 +99,11 @@ export async function initGeneratorInternal(tree: Tree, schema: Schema) {
9999
schema.addPlugin ??= addPluginDefault;
100100

101101
if (schema.addPlugin) {
102-
await addPluginV1(
102+
await addPlugin(
103103
tree,
104104
await createProjectGraphAsync(),
105105
'@nx/storybook/plugin',
106-
createNodes,
106+
createNodesV2,
107107
{
108108
serveStorybookTargetName: [
109109
'storybook',

packages/storybook/src/plugins/plugin.spec.ts

+157-101
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,57 @@ describe('@nx/storybook/plugin', () => {
6464
context
6565
);
6666

67-
expect(nodes.at(0)?.[0]).toStrictEqual('my-app/.storybook/main.ts');
68-
expect(nodes.at(0)?.[1]?.['projects']?.['my-app']?.targets).toBeDefined();
69-
expect(
70-
nodes.at(0)?.[1]?.['projects']?.['my-app']?.targets['build-storybook']
71-
).toMatchObject({
72-
command: 'storybook build',
73-
options: {
74-
cwd: 'my-app',
75-
},
76-
cache: true,
77-
outputs: [
78-
'{projectRoot}/storybook-static',
79-
'{options.output-dir}',
80-
'{options.outputDir}',
81-
'{options.o}',
82-
],
83-
inputs: [
84-
'production',
85-
'^production',
86-
{ externalDependencies: ['storybook'] },
87-
],
88-
});
89-
expect(
90-
nodes.at(0)?.[1]?.['projects']?.['my-app']?.targets['serve-storybook']
91-
).toMatchObject({
92-
command: 'storybook dev',
93-
});
67+
expect(nodes).toMatchInlineSnapshot(`
68+
[
69+
[
70+
"my-app/.storybook/main.ts",
71+
{
72+
"projects": {
73+
"my-app": {
74+
"root": "my-app",
75+
"targets": {
76+
"build-storybook": {
77+
"cache": true,
78+
"command": "storybook build",
79+
"inputs": [
80+
"production",
81+
"^production",
82+
{
83+
"externalDependencies": [
84+
"storybook",
85+
],
86+
},
87+
],
88+
"options": {
89+
"cwd": "my-app",
90+
},
91+
"outputs": [
92+
"{projectRoot}/storybook-static",
93+
"{options.output-dir}",
94+
"{options.outputDir}",
95+
"{options.o}",
96+
],
97+
},
98+
"serve-storybook": {
99+
"command": "storybook dev",
100+
"options": {
101+
"cwd": "my-app",
102+
},
103+
},
104+
"static-storybook": {
105+
"executor": "@nx/web:file-server",
106+
"options": {
107+
"buildTarget": "build-storybook",
108+
"staticFilePath": "my-app/storybook-static",
109+
},
110+
},
111+
},
112+
},
113+
},
114+
},
115+
],
116+
]
117+
`);
94118
});
95119

96120
it('should create angular nodes', async () => {
@@ -115,49 +139,63 @@ describe('@nx/storybook/plugin', () => {
115139
context
116140
);
117141

118-
expect(nodes.at(0)?.[0]).toStrictEqual('my-ng-app/.storybook/main.ts');
119-
expect(
120-
nodes.at(0)?.[1]?.['projects']?.['my-ng-app']?.targets
121-
).toBeDefined();
122-
expect(
123-
nodes.at(0)?.[1]?.['projects']?.['my-ng-app']?.targets?.[
124-
'build-storybook'
125-
]
126-
).toMatchObject({
127-
executor: '@storybook/angular:build-storybook',
128-
options: {
129-
outputDir: 'my-ng-app/storybook-static',
130-
configDir: 'my-ng-app/.storybook',
131-
browserTarget: 'my-ng-app:build-storybook',
132-
compodoc: false,
133-
},
134-
cache: true,
135-
outputs: [
136-
'{projectRoot}/storybook-static',
137-
'{options.output-dir}',
138-
'{options.outputDir}',
139-
'{options.o}',
140-
],
141-
inputs: [
142-
'production',
143-
'^production',
144-
{
145-
externalDependencies: ['storybook', '@storybook/angular'],
146-
},
147-
],
148-
});
149-
expect(
150-
nodes.at(0)?.[1]?.['projects']?.['my-ng-app']?.targets?.[
151-
'serve-storybook'
142+
expect(nodes).toMatchInlineSnapshot(`
143+
[
144+
[
145+
"my-ng-app/.storybook/main.ts",
146+
{
147+
"projects": {
148+
"my-ng-app": {
149+
"root": "my-ng-app",
150+
"targets": {
151+
"build-storybook": {
152+
"cache": true,
153+
"executor": "@storybook/angular:build-storybook",
154+
"inputs": [
155+
"production",
156+
"^production",
157+
{
158+
"externalDependencies": [
159+
"storybook",
160+
"@storybook/angular",
161+
],
162+
},
163+
],
164+
"options": {
165+
"browserTarget": "my-ng-app:build-storybook",
166+
"compodoc": false,
167+
"configDir": "my-ng-app/.storybook",
168+
"outputDir": "my-ng-app/storybook-static",
169+
},
170+
"outputs": [
171+
"{projectRoot}/storybook-static",
172+
"{options.output-dir}",
173+
"{options.outputDir}",
174+
"{options.o}",
175+
],
176+
},
177+
"serve-storybook": {
178+
"executor": "@storybook/angular:start-storybook",
179+
"options": {
180+
"browserTarget": "my-ng-app:build-storybook",
181+
"compodoc": false,
182+
"configDir": "my-ng-app/.storybook",
183+
},
184+
},
185+
"static-storybook": {
186+
"executor": "@nx/web:file-server",
187+
"options": {
188+
"buildTarget": "build-storybook",
189+
"staticFilePath": "my-ng-app/storybook-static",
190+
},
191+
},
192+
},
193+
},
194+
},
195+
},
196+
],
152197
]
153-
).toMatchObject({
154-
executor: '@storybook/angular:start-storybook',
155-
options: {
156-
browserTarget: 'my-ng-app:build-storybook',
157-
configDir: 'my-ng-app/.storybook',
158-
compodoc: false,
159-
},
160-
});
198+
`);
161199
});
162200

163201
it('should support main.js', async () => {
@@ -186,39 +224,57 @@ describe('@nx/storybook/plugin', () => {
186224
context
187225
);
188226

189-
expect(nodes.at(0)?.[0]).toStrictEqual('my-react-lib/.storybook/main.js');
190-
expect(
191-
nodes.at(0)?.[1]?.['projects']?.['my-react-lib']?.targets
192-
).toBeDefined();
193-
expect(
194-
nodes.at(0)?.[1]?.['projects']?.['my-react-lib']?.targets?.[
195-
'build-storybook'
196-
]
197-
).toMatchObject({
198-
command: 'storybook build',
199-
options: {
200-
cwd: 'my-react-lib',
201-
},
202-
cache: true,
203-
outputs: [
204-
'{projectRoot}/storybook-static',
205-
'{options.output-dir}',
206-
'{options.outputDir}',
207-
'{options.o}',
208-
],
209-
inputs: [
210-
'production',
211-
'^production',
212-
{ externalDependencies: ['storybook'] },
213-
],
214-
});
215-
expect(
216-
nodes.at(0)?.[1]?.['projects']?.['my-react-lib']?.targets?.[
217-
'serve-storybook'
227+
expect(nodes).toMatchInlineSnapshot(`
228+
[
229+
[
230+
"my-react-lib/.storybook/main.js",
231+
{
232+
"projects": {
233+
"my-react-lib": {
234+
"root": "my-react-lib",
235+
"targets": {
236+
"build-storybook": {
237+
"cache": true,
238+
"command": "storybook build",
239+
"inputs": [
240+
"production",
241+
"^production",
242+
{
243+
"externalDependencies": [
244+
"storybook",
245+
],
246+
},
247+
],
248+
"options": {
249+
"cwd": "my-react-lib",
250+
},
251+
"outputs": [
252+
"{projectRoot}/storybook-static",
253+
"{options.output-dir}",
254+
"{options.outputDir}",
255+
"{options.o}",
256+
],
257+
},
258+
"serve-storybook": {
259+
"command": "storybook dev",
260+
"options": {
261+
"cwd": "my-react-lib",
262+
},
263+
},
264+
"static-storybook": {
265+
"executor": "@nx/web:file-server",
266+
"options": {
267+
"buildTarget": "build-storybook",
268+
"staticFilePath": "my-react-lib/storybook-static",
269+
},
270+
},
271+
},
272+
},
273+
},
274+
},
275+
],
218276
]
219-
).toMatchObject({
220-
command: 'storybook dev',
221-
});
277+
`);
222278
});
223279

224280
function mockStorybookMainConfig(

packages/storybook/src/plugins/plugin.ts

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import { loadConfigFile } from '@nx/devkit/src/utils/config-utils';
2323
import type { StorybookConfig } from '@storybook/types';
2424
import { hashObject } from 'nx/src/hasher/file-hasher';
2525

26-
const pmc = getPackageManagerCommand();
27-
2826
export interface StorybookPluginOptions {
2927
buildStorybookTargetName?: string;
3028
serveStorybookTargetName?: string;

0 commit comments

Comments
 (0)