Skip to content

Commit

Permalink
Merge pull request #26128 from storybookjs/tom/add-stats-json-flag
Browse files Browse the repository at this point in the history
CLI: Rename `--webpack-stats-json` to `--stats-json` as it works in Vite
  • Loading branch information
shilman authored Feb 23, 2024
2 parents e5cb586 + acafea6 commit 995abe0
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 18 deletions.
6 changes: 6 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [For Solid:](#for-solid)
- [For Qwik:](#for-qwik)
- [TurboSnap Vite plugin is no longer needed](#turbosnap-vite-plugin-is-no-longer-needed)
- [`--webpack-stats-json` option renamed `--stats-json`](#--webpack-stats-json-option-renamed---stats-json)
- [Implicit actions can not be used during rendering (for example in the play function)](#implicit-actions-can-not-be-used-during-rendering-for-example-in-the-play-function)
- [MDX related changes](#mdx-related-changes)
- [MDX is upgraded to v3](#mdx-is-upgraded-to-v3)
Expand Down Expand Up @@ -551,6 +552,11 @@ At least in build mode, `builder-vite` now supports the `--webpack-stats-json` f

This means https://github.com/IanVS/vite-plugin-turbosnap is no longer necessary, and duplicative, and the plugin will automatically be removed if found.

### `--webpack-stats-json` option renamed `--stats-json`

Now that both Vite and Webpack support the `preview-stats.json` file, the flag has been renamed. The old flag will continue to work.


### Implicit actions can not be used during rendering (for example in the play function)

In Storybook 7, we inferred if the component accepts any action props,
Expand Down
1 change: 1 addition & 0 deletions code/builders/builder-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@storybook/channels": "workspace:*",
"@storybook/client-logger": "workspace:*",
"@storybook/core-common": "workspace:*",
"@storybook/core-events": "workspace:*",
"@storybook/csf-plugin": "workspace:*",
"@storybook/node-logger": "workspace:*",
"@storybook/preview": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion code/builders/builder-vite/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function build(options: Options) {
finalConfig.plugins && (await hasVitePlugins(finalConfig.plugins, [turbosnapPluginName]));
if (hasTurbosnapPlugin) {
logger.warn(dedent`Found '${turbosnapPluginName}' which is now included by default in Storybook 8.
Removing from your plugins list. Ensure you pass \`--webpack-stats-json\` to generate stats.
Removing from your plugins list. Ensure you pass \`--stats-json\` to generate stats.
For more information, see https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#turbosnap-vite-plugin-is-no-longer-needed`);

Expand Down
7 changes: 6 additions & 1 deletion code/builders/builder-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { RequestHandler } from 'express';
import type { ViteDevServer } from 'vite';
import express from 'express';
import { dirname, join, parse } from 'path';
import { NoStatsForViteDevError } from '@storybook/core-events/server-errors';
import type { Options } from '@storybook/types';
import { transformIframeHtml } from './transform-iframe-html';
import { createViteServer } from './vite-server';
Expand Down Expand Up @@ -68,7 +69,11 @@ export const start: ViteBuilder['start'] = async ({

return {
bail,
stats: { toJson: () => null },
stats: {
toJson: () => {
throw new NoStatsForViteDevError();
},
},
totalTime: process.hrtime(startTime),
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe.skip('Build Storybook Builder', () => {
packageJson: expect.any(Object),
mode: 'static',
tsConfig: './storybook/tsconfig.ts',
webpackStatsJson: false,
statsJson: false,
})
);
});
Expand Down Expand Up @@ -133,7 +133,7 @@ describe.skip('Build Storybook Builder', () => {
packageJson: expect.any(Object),
mode: 'static',
tsConfig: 'path/to/tsConfig.json',
webpackStatsJson: false,
statsJson: false,
})
);
});
Expand All @@ -142,7 +142,7 @@ describe.skip('Build Storybook Builder', () => {
const run = await architect.scheduleBuilder('@storybook/angular:build-storybook', {
tsConfig: 'path/to/tsConfig.json',
compodoc: false,
webpackStatsJson: true,
statsJson: true,
});

const output = await run.result;
Expand All @@ -162,7 +162,7 @@ describe.skip('Build Storybook Builder', () => {
packageJson: expect.any(Object),
mode: 'static',
tsConfig: 'path/to/tsConfig.json',
webpackStatsJson: true,
statsJson: true,
})
);
});
Expand Down Expand Up @@ -212,7 +212,7 @@ describe.skip('Build Storybook Builder', () => {
packageJson: expect.any(Object),
mode: 'static',
tsConfig: './storybook/tsconfig.ts',
webpackStatsJson: false,
statsJson: false,
})
);
});
Expand Down Expand Up @@ -242,7 +242,7 @@ describe.skip('Build Storybook Builder', () => {
packageJson: expect.any(Object),
mode: 'static',
tsConfig: 'path/to/tsConfig.json',
webpackStatsJson: false,
statsJson: false,
})
);
});
Expand Down
3 changes: 3 additions & 0 deletions code/frameworks/angular/src/builders/build-storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type StorybookBuilderOptions = JsonObject & {
| 'quiet'
| 'test'
| 'webpackStatsJson'
| 'statsJson'
| 'disableTelemetry'
| 'debugWebpack'
| 'previewUrl'
Expand Down Expand Up @@ -93,6 +94,7 @@ const commandBuilder: BuilderHandlerFn<StorybookBuilderOptions> = (
quiet,
enableProdMode = true,
webpackStatsJson,
statsJson,
debugWebpack,
disableTelemetry,
assets,
Expand Down Expand Up @@ -120,6 +122,7 @@ const commandBuilder: BuilderHandlerFn<StorybookBuilderOptions> = (
},
tsConfig,
webpackStatsJson,
statsJson,
debugWebpack,
previewUrl,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type StorybookBuilderOptions = JsonObject & {
| 'docs'
| 'debugWebpack'
| 'webpackStatsJson'
| 'statsJson'
| 'loglevel'
| 'previewUrl'
>;
Expand Down Expand Up @@ -112,6 +113,7 @@ const commandBuilder: BuilderHandlerFn<StorybookBuilderOptions> = (options, cont
debugWebpack,
loglevel,
webpackStatsJson,
statsJson,
previewUrl,
sourceMap = false,
} = options;
Expand Down Expand Up @@ -143,8 +145,9 @@ const commandBuilder: BuilderHandlerFn<StorybookBuilderOptions> = (options, cont
initialPath,
open,
debugWebpack,
loglevel,
webpackStatsJson,
statsJson,
loglevel,
previewUrl,
};

Expand Down
12 changes: 10 additions & 2 deletions code/lib/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ command('dev')
.option('--quiet', 'Suppress verbose build output')
.option('--no-version-updates', 'Suppress update check', true)
.option('--debug-webpack', 'Display final webpack configurations for debugging purposes')
.option('--webpack-stats-json [directory]', 'Write Webpack Stats JSON to disk')
.option(
'--webpack-stats-json [directory]',
'Write Webpack stats JSON to disk (synonym for `--stats-json`)'
)
.option('--stats-json [directory]', 'Write stats JSON to disk')
.option(
'--preview-url <string>',
'Disables the default storybook preview and lets your use your own'
Expand Down Expand Up @@ -263,7 +267,11 @@ command('build')
.option('--quiet', 'Suppress verbose build output')
.option('--loglevel <level>', 'Control level of logging during build')
.option('--debug-webpack', 'Display final webpack configurations for debugging purposes')
.option('--webpack-stats-json [directory]', 'Write Webpack Stats JSON to disk')
.option(
'--webpack-stats-json [directory]',
'Write Webpack stats JSON to disk (synonym for `--stats-json`)'
)
.option('--stats-json [directory]', 'Write stats JSON to disk')
.option(
'--preview-url <string>',
'Disables the default storybook preview and lets your use your own'
Expand Down
14 changes: 14 additions & 0 deletions code/lib/core-events/src/errors/server-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,17 @@ export class UpgradeStorybookUnknownCurrentVersionError extends StorybookError {
`;
}
}

export class NoStatsForViteDevError extends StorybookError {
readonly category = Category.BUILDER_VITE;

readonly code = 1;

template() {
return dedent`
Unable to write preview stats as the Vite builder does not support stats in dev mode.
Please remove the \`--stats-json\` flag when running in dev mode.
`;
}
}
6 changes: 4 additions & 2 deletions code/lib/core-server/src/build-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ export async function buildDevStandalone(
const previewStats = previewResult?.stats;
const managerStats = managerResult?.stats;

if (options.webpackStatsJson) {
const target = options.webpackStatsJson === true ? options.outputDir : options.webpackStatsJson;
const statsOption = options.webpackStatsJson || options.statsJson;
if (statsOption) {
const target = statsOption === true ? options.outputDir : statsOption;

await outputStats(target, previewStats);
}

Expand Down
6 changes: 3 additions & 3 deletions code/lib/core-server/src/build-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption
.then(async (previewStats) => {
logger.trace({ message: '=> Preview built', time: process.hrtime(startTime) });

if (options.webpackStatsJson) {
const target =
options.webpackStatsJson === true ? options.outputDir : options.webpackStatsJson;
const statsOption = options.webpackStatsJson || options.statsJson;
if (statsOption) {
const target = statsOption === true ? options.outputDir : statsOption;
await outputStats(target, previewStats);
}
})
Expand Down
1 change: 1 addition & 0 deletions code/lib/types/src/modules/core-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export interface CLIOptions {
test?: boolean;
debugWebpack?: boolean;
webpackStatsJson?: string | boolean;
statsJson?: string | boolean;
outputDir?: string;
}

Expand Down
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5334,6 +5334,7 @@ __metadata:
"@storybook/channels": "workspace:*"
"@storybook/client-logger": "workspace:*"
"@storybook/core-common": "workspace:*"
"@storybook/core-events": "workspace:*"
"@storybook/csf-plugin": "workspace:*"
"@storybook/node-logger": "workspace:*"
"@storybook/preview": "workspace:*"
Expand Down
4 changes: 2 additions & 2 deletions docs/api/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Options include:
| `--no-open` | Do not open Storybook automatically in the browser<br/>`storybook dev --no-open` |
| `--quiet` | Suppress verbose build output<br/>`storybook dev --quiet` |
| `--debug-webpack` | Display final webpack configurations for debugging purposes<br/>`storybook dev --debug-webpack` |
| `--webpack-stats-json` | Write Webpack Stats JSON to disk<br/>`storybook dev --webpack-stats-json /tmp/webpack-stats` |
| `--stats-json` | Write stats JSON to disk<br/>`storybook dev ---stats-json /tmp/stats`<br/>NOTE: only works for webpack. |
| `--docs` | Starts Storybook in documentation mode. Learn more about it in [here](../writing-docs/build-documentation.md#preview-storybooks-documentation)<br/>`storybook dev --docs` |
| `--disable-telemetry` | Disables Storybook's telemetry. Learn more about it [here](../configure/telemetry.md)<br/>`storybook dev --disable-telemetry` |

Expand Down Expand Up @@ -75,7 +75,7 @@ Options include:
| `--loglevel [level]` | Controls level of logging during build.<br/> Available options: `silly`, `verbose`, `info` (default), `warn`, `error`, `silent`<br/>`storybook build --loglevel warn` |
| `--quiet` | Suppress verbose build output<br/>`storybook build --quiet` |
| `--debug-webpack` | Display final webpack configurations for debugging purposes<br/>`storybook build --debug-webpack` |
| `--webpack-stats-json` | Write Webpack Stats JSON to disk<br/>`storybook build --webpack-stats-json /my-storybook/webpack-stats` |
| `---stats-json` | Write stats JSON to disk<br/>`storybook build -stats-json /my-storybook/-stats` |
| `--docs` | Builds Storybook in documentation mode. Learn more about it in [here](../writing-docs/build-documentation.md#publish-storybooks-documentation)<br/>`storybook build --docs` |
| `--disable-telemetry` | Disables Storybook's telemetry. Learn more about it [here](../configure/telemetry.md).<br/>`storybook build --disable-telemetry` |
| `--test` | Optimize Storybook's production build for performance and tests by removing unnecessary features with the `test` option. Learn more [here](../api/main-config-build.md).<br/>`storybook build --test` |
Expand Down

0 comments on commit 995abe0

Please sign in to comment.