From 8a4492d5abe46d9f2e208a971c2b155cabed9772 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Thu, 16 Jan 2025 14:18:57 -0800 Subject: [PATCH] Add support for the --docs parameter to the Heft storybook plugin. (#5080) --- ...dd-docs-to-storybook_2025-01-16-21-48.json | 10 ++ .../heft-storybook-plugin/heft-plugin.json | 5 + .../src/StorybookPlugin.ts | 131 +++++++++++------- 3 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 common/changes/@rushstack/heft-storybook-plugin/add-docs-to-storybook_2025-01-16-21-48.json diff --git a/common/changes/@rushstack/heft-storybook-plugin/add-docs-to-storybook_2025-01-16-21-48.json b/common/changes/@rushstack/heft-storybook-plugin/add-docs-to-storybook_2025-01-16-21-48.json new file mode 100644 index 00000000000..6da1ab942b1 --- /dev/null +++ b/common/changes/@rushstack/heft-storybook-plugin/add-docs-to-storybook_2025-01-16-21-48.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/heft-storybook-plugin", + "comment": "Add support for the `--docs` parameter.", + "type": "minor" + } + ], + "packageName": "@rushstack/heft-storybook-plugin" +} \ No newline at end of file diff --git a/heft-plugins/heft-storybook-plugin/heft-plugin.json b/heft-plugins/heft-storybook-plugin/heft-plugin.json index 2b552d232db..c961ec466f9 100644 --- a/heft-plugins/heft-storybook-plugin/heft-plugin.json +++ b/heft-plugins/heft-storybook-plugin/heft-plugin.json @@ -18,6 +18,11 @@ "longName": "--storybook-test", "description": "Executes a stripped down build-storybook for testing purposes.", "parameterKind": "flag" + }, + { + "longName": "--docs", + "description": "Execute storybook in docs mode.", + "parameterKind": "flag" } ] } diff --git a/heft-plugins/heft-storybook-plugin/src/StorybookPlugin.ts b/heft-plugins/heft-storybook-plugin/src/StorybookPlugin.ts index 9d396767630..40b16a949e3 100644 --- a/heft-plugins/heft-storybook-plugin/src/StorybookPlugin.ts +++ b/heft-plugins/heft-storybook-plugin/src/StorybookPlugin.ts @@ -157,7 +157,9 @@ export interface IStorybookPluginOptions { captureWebpackStats?: boolean; } -interface IRunStorybookOptions { +interface IRunStorybookOptions extends IPrepareStorybookOptions { + logger: IScopedLogger; + isServeMode: boolean; workingDirectory: string; resolvedModulePath: string; outputFolder: string | undefined; @@ -165,6 +167,15 @@ interface IRunStorybookOptions { verbose: boolean; } +interface IPrepareStorybookOptions extends IStorybookPluginOptions { + logger: IScopedLogger; + taskSession: IHeftTaskSession; + heftConfiguration: HeftConfiguration; + isServeMode: boolean; + isTestMode: boolean; + isDocsMode: boolean; +} + const DEFAULT_STORYBOOK_VERSION: StorybookCliVersion = StorybookCliVersion.STORYBOOK7; const DEFAULT_STORYBOOK_CLI_CONFIG: Record = { [StorybookCliVersion.STORYBOOK6]: { @@ -190,14 +201,12 @@ const DEFAULT_STORYBOOK_CLI_CONFIG: Record { - private _logger!: IScopedLogger; - private _isServeMode: boolean = false; - private _isTestMode: boolean = false; - /** * Generate typings for Sass files before TypeScript compilation. */ @@ -206,11 +215,12 @@ export default class StorybookPlugin implements IHeftTaskPlugin Promise = async () => { // Discard Webpack's configuration to prevent Webpack from running - this._logger.terminal.writeLine( + logger.terminal.writeLine( 'The command line includes "--storybook", redirecting Webpack to Storybook' ); return false; }; + let isServeMode: boolean = false; taskSession.requestAccessToPluginByName( '@rushstack/heft-webpack4-plugin', WEBPACK4_PLUGIN_NAME, (accessor: IWebpack4PluginAccessor) => { - if (accessor.parameters.isServeMode) { - this._isServeMode = true; - } + isServeMode = accessor.parameters.isServeMode; + // Discard Webpack's configuration to prevent Webpack from running only when performing Storybook build accessor.hooks.onLoadConfiguration.tapPromise(PLUGIN_NAME, configureWebpackTap); } @@ -251,31 +257,37 @@ export default class StorybookPlugin implements IHeftTaskPlugin { - if (accessor.parameters.isServeMode) { - this._isServeMode = true; - } + isServeMode = accessor.parameters.isServeMode; + // Discard Webpack's configuration to prevent Webpack from running only when performing Storybook build accessor.hooks.onLoadConfiguration.tapPromise(PLUGIN_NAME, configureWebpackTap); } ); taskSession.hooks.run.tapPromise(PLUGIN_NAME, async (runOptions: IHeftTaskRunHookOptions) => { - const runStorybookOptions: IRunStorybookOptions = await this._prepareStorybookAsync( + const runStorybookOptions: IRunStorybookOptions = await this._prepareStorybookAsync({ + logger, taskSession, heftConfiguration, - options - ); + isServeMode, + isTestMode: storybookTestParameter.value, + isDocsMode: docsParameter.value, + ...options + }); await this._runStorybookAsync(runStorybookOptions, options); }); } } - private async _prepareStorybookAsync( - taskSession: IHeftTaskSession, - heftConfiguration: HeftConfiguration, - options: IStorybookPluginOptions - ): Promise { - const { storykitPackageName, staticBuildOutputFolder } = options; + private async _prepareStorybookAsync(options: IPrepareStorybookOptions): Promise { + const { + logger, + taskSession, + heftConfiguration, + storykitPackageName, + staticBuildOutputFolder, + isTestMode + } = options; const storybookCliVersion: `${StorybookCliVersion}` = this._getStorybookVersion(options); const storyBookCliConfig: IStorybookCliCallingConfig = DEFAULT_STORYBOOK_CLI_CONFIG[storybookCliVersion]; const cliPackageName: string = options.cliPackageName ?? storyBookCliConfig.packageName; @@ -283,11 +295,11 @@ export default class StorybookPlugin implements IHeftTaskPlugin { - const { resolvedModulePath, verbose } = runStorybookOptions; + const { logger, resolvedModulePath, verbose, isServeMode, isTestMode, isDocsMode } = runStorybookOptions; let { workingDirectory, outputFolder } = runStorybookOptions; - this._logger.terminal.writeLine('Running Storybook compilation'); - this._logger.terminal.writeVerboseLine(`Loading Storybook module "${resolvedModulePath}"`); + logger.terminal.writeLine('Running Storybook compilation'); + logger.terminal.writeVerboseLine(`Loading Storybook module "${resolvedModulePath}"`); const storybookCliVersion: `${StorybookCliVersion}` = this._getStorybookVersion(options); /** @@ -424,7 +437,7 @@ export default class StorybookPlugin implements IHeftTaskPlugin { + private async _invokeAsSubprocessAsync( + logger: IScopedLogger, + command: string, + args: string[], + cwd: string + ): Promise { return await new Promise((resolve, reject) => { const storybookEnv: NodeJS.ProcessEnv = { ...process.env }; const forkedProcess: child_process.ChildProcess = child_process.fork(command, args, { @@ -479,12 +505,12 @@ export default class StorybookPlugin implements IHeftTaskPlugin