@nx/storybook 24.0.5 → 24.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -320,15 +320,18 @@ function addStorybookToNamedInputs(tree, schema) {
320
320
  if (nxJson.namedInputs) {
321
321
  const hasProductionFileset = !!nxJson.namedInputs?.production;
322
322
  if (hasProductionFileset) {
323
- if (!nxJson.namedInputs.production.includes('!{projectRoot}/**/*.stories.@(js|jsx|ts|tsx|mdx)')) {
324
- nxJson.namedInputs.production.push('!{projectRoot}/**/*.stories.@(js|jsx|ts|tsx|mdx)');
323
+ const storiesExclusion = `!{projectRoot}/${utilities_1.storybookFilePatterns.stories}`;
324
+ if (!nxJson.namedInputs.production.includes(storiesExclusion)) {
325
+ nxJson.namedInputs.production.push(storiesExclusion);
325
326
  }
326
- if (!nxJson.namedInputs.production.includes('!{projectRoot}/.storybook/**/*')) {
327
- nxJson.namedInputs.production.push('!{projectRoot}/.storybook/**/*');
327
+ const configExclusion = `!{projectRoot}/${utilities_1.storybookFilePatterns.config}`;
328
+ if (!nxJson.namedInputs.production.includes(configExclusion)) {
329
+ nxJson.namedInputs.production.push(configExclusion);
328
330
  }
331
+ const tsconfigExclusion = `!{projectRoot}/${utilities_1.storybookFilePatterns.tsconfig}`;
329
332
  if (schema.uiFramework !== '@storybook/angular' &&
330
- !nxJson.namedInputs.production.includes('!{projectRoot}/tsconfig.storybook.json')) {
331
- nxJson.namedInputs.production.push('!{projectRoot}/tsconfig.storybook.json');
333
+ !nxJson.namedInputs.production.includes(tsconfigExclusion)) {
334
+ nxJson.namedInputs.production.push(tsconfigExclusion);
332
335
  }
333
336
  }
334
337
  (0, devkit_1.updateNxJson)(tree, nxJson);
@@ -78,12 +78,12 @@ function handlePropertiesFromTargetOptions(tree, options, defaultConfigDir, proj
78
78
  }
79
79
  if ('docsMode' in options) {
80
80
  configValues.docsMode = options.docsMode;
81
- moveDocsModeToConfigFile(tree, configDir, projectName, migrationLogs, configDir === defaultConfigDir);
81
+ moveDocsModeToConfigFile(tree, configDir, projectName, migrationLogs);
82
82
  delete options.docsMode;
83
83
  }
84
84
  if ('staticDir' in options) {
85
85
  configValues.staticDir = options.staticDir;
86
- moveStaticDirToConfigFile(tree, configDir, projectName, migrationLogs, configDir === defaultConfigDir);
86
+ moveStaticDirToConfigFile(tree, configDir, projectName, migrationLogs);
87
87
  delete options.staticDir;
88
88
  }
89
89
  const storybookPropMappings = (0, utils_1.getStorybookPropMappings)(tree);
@@ -95,7 +95,7 @@ function handlePropertiesFromTargetOptions(tree, options, defaultConfigDir, proj
95
95
  }
96
96
  }
97
97
  }
98
- function moveDocsModeToConfigFile(tree, configDir, projectName, migrationLogs, useConfigValues = true) {
98
+ function moveDocsModeToConfigFile(tree, configDir, projectName, migrationLogs) {
99
99
  const configFilePath = (0, utils_1.getConfigFilePath)(tree, configDir);
100
100
  const configFileContents = tree.read(configFilePath, 'utf-8');
101
101
  const sourceFile = (0, tsquery_1.ast)(configFileContents);
@@ -130,7 +130,7 @@ function moveDocsModeToConfigFile(tree, configDir, projectName, migrationLogs, u
130
130
  : `docsMode: ${docsModeInsert},`;
131
131
  tree.write(configFilePath, `${configFileContents.slice(0, startPosition)}${nodeToInsert}${configFileContents.slice(startPosition)}`);
132
132
  }
133
- function moveStaticDirToConfigFile(tree, configDir, projectName, migrationLogs, useConfigValues = true) {
133
+ function moveStaticDirToConfigFile(tree, configDir, projectName, migrationLogs) {
134
134
  const configFilePath = (0, utils_1.getConfigFilePath)(tree, configDir);
135
135
  const configFileContents = tree.read(configFilePath, 'utf-8');
136
136
  const sourceFile = (0, tsquery_1.ast)(configFileContents);
@@ -1,3 +1,6 @@
1
1
  import { Schema } from './schema';
2
- export declare function callUpgrade(schema: Schema): 1 | Buffer;
2
+ export declare function callUpgrade(schema: Schema): {
3
+ success: boolean;
4
+ nxVersionsRestored: boolean;
5
+ };
3
6
  export declare function checkStorybookInstalled(packageJson: Record<string, any>): boolean;
@@ -4,9 +4,11 @@ exports.callUpgrade = callUpgrade;
4
4
  exports.checkStorybookInstalled = checkStorybookInstalled;
5
5
  const devkit_1 = require("@nx/devkit");
6
6
  const child_process_1 = require("child_process");
7
+ const path_1 = require("path");
7
8
  function callUpgrade(schema) {
8
9
  const packageManager = (0, devkit_1.detectPackageManager)();
9
10
  const pm = (0, devkit_1.getPackageManagerCommand)(packageManager);
11
+ let nxVersionsRestored = false;
10
12
  try {
11
13
  devkit_1.output.log({
12
14
  title: `Calling sb upgrade`,
@@ -16,10 +18,16 @@ function callUpgrade(schema) {
16
18
  ],
17
19
  color: 'blue',
18
20
  });
19
- (0, child_process_1.execSync)(`${pm.dlx} ${packageManager === 'yarn' ? 'storybook' : 'storybook@latest'} upgrade ${schema.autoAcceptAllPrompts ? '--yes' : ''}`, {
20
- stdio: [0, 1, 2],
21
- windowsHide: true,
22
- });
21
+ const nxVersions = readNxDependencyVersions();
22
+ try {
23
+ (0, child_process_1.execSync)(`${pm.dlx} ${packageManager === 'yarn' ? 'storybook' : 'storybook@latest'} upgrade ${schema.autoAcceptAllPrompts ? '--yes' : ''}`, {
24
+ stdio: [0, 1, 2],
25
+ windowsHide: true,
26
+ });
27
+ }
28
+ finally {
29
+ nxVersionsRestored = restoreNxDependencyVersions(nxVersions);
30
+ }
23
31
  devkit_1.output.log({
24
32
  title: `Storybook packages upgraded.`,
25
33
  bodyLines: [
@@ -40,8 +48,38 @@ function callUpgrade(schema) {
40
48
  color: 'red',
41
49
  });
42
50
  console.log(e);
43
- return 1;
51
+ return { success: false, nxVersionsRestored };
52
+ }
53
+ return { success: true, nxVersionsRestored };
54
+ }
55
+ const dependencySections = ['dependencies', 'devDependencies'];
56
+ function isNxPackage(name) {
57
+ return name === 'nx' || name.startsWith('@nx/');
58
+ }
59
+ function readNxDependencyVersions() {
60
+ const packageJson = (0, devkit_1.readJsonFile)((0, path_1.join)(devkit_1.workspaceRoot, 'package.json'));
61
+ const versions = {};
62
+ for (const section of dependencySections) {
63
+ versions[section] = Object.fromEntries(Object.entries(packageJson[section] ?? {}).filter(([name]) => isNxPackage(name)));
64
+ }
65
+ return versions;
66
+ }
67
+ function restoreNxDependencyVersions(versions) {
68
+ const packageJsonPath = (0, path_1.join)(devkit_1.workspaceRoot, 'package.json');
69
+ const packageJson = (0, devkit_1.readJsonFile)(packageJsonPath);
70
+ let changed = false;
71
+ for (const section of dependencySections) {
72
+ for (const [name, version] of Object.entries(versions[section])) {
73
+ if (packageJson[section]?.[name] !== version) {
74
+ packageJson[section][name] = version;
75
+ changed = true;
76
+ }
77
+ }
78
+ }
79
+ if (changed) {
80
+ (0, devkit_1.writeJsonFile)(packageJsonPath, packageJson);
44
81
  }
82
+ return changed;
45
83
  }
46
84
  function checkStorybookInstalled(packageJson) {
47
85
  return ((packageJson.dependencies['storybook'] ||
@@ -1,4 +1,4 @@
1
- import { Tree } from '@nx/devkit';
1
+ import { Tree, type GeneratorCallback } from '@nx/devkit';
2
2
  import { Schema } from './schema';
3
- export declare function migrate10Generator(tree: Tree, schema: Schema): Promise<void>;
3
+ export declare function migrate10Generator(tree: Tree, schema: Schema): Promise<GeneratorCallback | void>;
4
4
  export default migrate10Generator;
@@ -19,18 +19,22 @@ async function migrate10Generator(tree, schema) {
19
19
  });
20
20
  return;
21
21
  }
22
- (0, calling_storybook_cli_1.callUpgrade)(schema);
22
+ const { nxVersionsRestored } = (0, calling_storybook_cli_1.callUpgrade)(schema);
23
+ const installTask = nxVersionsRestored
24
+ ? () => (0, devkit_1.installPackagesTask)(tree, true)
25
+ : undefined;
23
26
  if (schema.skipAiInstructions) {
24
- return;
27
+ return installTask;
25
28
  }
26
29
  const pathToAiInstructions = (0, path_1.join)(__dirname, 'files', 'ai-instructions-for-cjs-esm.md');
27
30
  if (!(0, fs_1.existsSync)(pathToAiInstructions)) {
28
- return;
31
+ return installTask;
29
32
  }
30
33
  const contents = (0, fs_1.readFileSync)(pathToAiInstructions);
31
34
  tree.write('tools/ai-migrations/MIGRATE_STORYBOOK_10.md', contents);
32
35
  devkit_1.logger.log(`Storybook 10 requires Storybook Configs to use ESM.
33
36
  We created 'tools/ai-migrations/MIGRATE_STORYBOOK_10.md' with instructions for an AI Agent to convert CJS Storybook Configs to ESM in your workspace.`);
34
37
  await (0, devkit_1.formatFiles)(tree);
38
+ return installTask;
35
39
  }
36
40
  exports.default = migrate10Generator;
@@ -11,6 +11,12 @@ const internal_2 = require("@nx/js/internal");
11
11
  const utilities_1 = require("../utils/utilities");
12
12
  const dev_server_ready_check_1 = require("../utils/dev-server-ready-check");
13
13
  const storybookConfigGlob = '**/.storybook/main.{js,ts,mjs,mts,cjs,cts}';
14
+ // `production` usually excludes these files. Its `!{projectRoot}/` exclusions
15
+ // don't filter `{workspaceRoot}/` filesets, so these keep the files hashed.
16
+ const storybookFileInputs = Object.values(utilities_1.storybookFilePatterns).map((pattern) => `{workspaceRoot}/{projectRoot}/${pattern}`);
17
+ // `^production` drops dependencies' stories too, which a host Storybook loads
18
+ // from the projects it depends on.
19
+ const dependencyStoriesInput = `^{projectRoot}/${utilities_1.storybookFilePatterns.stories}`;
14
20
  // Vite-builder frameworks we infer the addon target for. The addon itself only
15
21
  // checks that the resolved builder is Vite, so this list is conservative rather
16
22
  // than exhaustive. `@storybook/sveltekit` uses the Vite builder despite the name.
@@ -40,7 +46,10 @@ exports.createNodes = [
40
46
  const lockFileName = (0, js_1.getLockFileName)(packageManager);
41
47
  try {
42
48
  const { entries, preErrors } = await filterStorybookConfigs(configFilePaths, context);
43
- const projectHashes = await (0, internal_1.calculateHashesForCreateNodes)(entries.map((e) => e.projectRoot), normalizedOptions, context, entries.map(() => [lockFileName]));
49
+ const projectHashes = await (0, internal_1.calculateHashesForCreateNodes)(entries.map((e) => e.projectRoot), {
50
+ ...normalizedOptions,
51
+ namedInputs: context.nxJsonConfiguration.namedInputs ?? {},
52
+ }, context, entries.map(() => [lockFileName]));
44
53
  let results = [];
45
54
  let nodeErrors = [];
46
55
  try {
@@ -136,7 +145,12 @@ async function buildStorybookTargets(configFilePath, projectRoot, options, conte
136
145
  function storybookInputs(namedInputs, frameworkIsAngular, hasTestRunner) {
137
146
  return [
138
147
  ...('production' in namedInputs
139
- ? ['production', '^production']
148
+ ? [
149
+ 'production',
150
+ ...storybookFileInputs,
151
+ '^production',
152
+ dependencyStoriesInput,
153
+ ]
140
154
  : ['default', '^default']),
141
155
  {
142
156
  externalDependencies: [
@@ -11,6 +11,11 @@ export declare const Constants: {
11
11
  jsonIndentLevel: number;
12
12
  coreAddonPrefix: string;
13
13
  };
14
+ export declare const storybookFilePatterns: {
15
+ stories: string;
16
+ config: string;
17
+ tsconfig: string;
18
+ };
14
19
  export declare function getStorybookVersionToInstall(tree: Tree): string;
15
20
  export declare function safeFileDelete(tree: Tree, path: string): boolean;
16
21
  export type TsConfig = {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Constants = exports.getInstalledStorybookVersion = exports.storybookMajorVersion = void 0;
3
+ exports.storybookFilePatterns = exports.Constants = exports.getInstalledStorybookVersion = exports.storybookMajorVersion = void 0;
4
4
  exports.getStorybookVersionToInstall = getStorybookVersionToInstall;
5
5
  exports.safeFileDelete = safeFileDelete;
6
6
  exports.dedupe = dedupe;
@@ -22,6 +22,11 @@ exports.Constants = {
22
22
  jsonIndentLevel: 2,
23
23
  coreAddonPrefix: '@storybook/addon-',
24
24
  };
25
+ exports.storybookFilePatterns = {
26
+ stories: '**/*.stories.@(js|jsx|ts|tsx|mdx)',
27
+ config: '.storybook/**/*',
28
+ tsconfig: 'tsconfig.storybook.json',
29
+ };
25
30
  function getStorybookVersionToInstall(tree) {
26
31
  return (0, versions_1.versions)(tree).storybookVersion;
27
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/storybook",
3
- "version": "24.0.5",
3
+ "version": "24.1.0-beta.0",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "files": [
@@ -97,20 +97,20 @@
97
97
  }
98
98
  },
99
99
  "dependencies": {
100
- "@nx/devkit": "24.0.5",
100
+ "@nx/devkit": "24.1.0-beta.0",
101
101
  "@phenomnomnominal/tsquery": "~6.2.0",
102
102
  "semver": "^7.6.3",
103
103
  "tslib": "^2.3.0",
104
- "@nx/cypress": "24.0.5",
105
- "@nx/js": "24.0.5",
106
- "@nx/eslint": "24.0.5"
104
+ "@nx/cypress": "24.1.0-beta.0",
105
+ "@nx/js": "24.1.0-beta.0",
106
+ "@nx/eslint": "24.1.0-beta.0"
107
107
  },
108
108
  "devDependencies": {
109
- "nx": "24.0.5",
110
- "storybook": "9.0.6"
109
+ "nx": "24.1.0-beta.0",
110
+ "storybook": "9.1.21"
111
111
  },
112
112
  "peerDependencies": {
113
- "@nx/web": "24.0.5",
113
+ "@nx/web": "24.1.0-beta.0",
114
114
  "storybook": ">=8.0.0 <11.0.0"
115
115
  },
116
116
  "peerDependenciesMeta": {