From 4cd5aef3b93e3d73b5175c36cf9e8f9ae4455cb2 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 5 Nov 2024 02:10:13 +0530 Subject: [PATCH] fix: improve help output for possible values (#4316) * fix: improve help output for possible values * test: udpate snaps --- packages/webpack-cli/src/webpack-cli.ts | 11 ++++++--- .../__snapshots__/CLI.test.js.snap.webpack5 | 2 +- .../help.test.js.snap.devServer5.webpack5 | 24 +++++++++++++++---- test/help/help.test.js | 8 +++++++ 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index fc9eb770b4a..8c5bc9746b7 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -1646,9 +1646,14 @@ class WebpackCLI implements IWebpackCLI { ); if (possibleValues.length > 0) { - this.logger.raw( - `${bold("Possible values:")} ${JSON.stringify(possibleValues.join(" | "))}`, - ); + // Convert the possible values to a union type string + // ['mode', 'development', 'production'] => "'mode' | 'development' | 'production'" + // [false, 'eval'] => "false | 'eval'" + const possibleValuesUnionTypeString = possibleValues + .map((value) => (typeof value === "string" ? `'${value}'` : value)) + .join(" | "); + + this.logger.raw(`${bold("Possible values:")} ${possibleValuesUnionTypeString}`); } } diff --git a/test/api/__snapshots__/CLI.test.js.snap.webpack5 b/test/api/__snapshots__/CLI.test.js.snap.webpack5 index 40b8c08886a..64b26c6595b 100644 --- a/test/api/__snapshots__/CLI.test.js.snap.webpack5 +++ b/test/api/__snapshots__/CLI.test.js.snap.webpack5 @@ -9,7 +9,7 @@ exports[`CLI API custom help output should display help information 1`] = ` "Description: Enable production optimizations or development hints.", ], [ - "Possible values: "development | production | none"", + "Possible values: 'development' | 'production' | 'none'", ], [ "", diff --git a/test/help/__snapshots__/help.test.js.snap.devServer5.webpack5 b/test/help/__snapshots__/help.test.js.snap.devServer5.webpack5 index 473a5be356b..c1ae9165b41 100644 --- a/test/help/__snapshots__/help.test.js.snap.devServer5.webpack5 +++ b/test/help/__snapshots__/help.test.js.snap.devServer5.webpack5 @@ -1207,7 +1207,7 @@ exports[`help should show help information using the "help --cache-type" option: exports[`help should show help information using the "help --cache-type" option: stdout 1`] = ` "Usage: webpack --cache-type Description: In memory caching. Filesystem caching. -Possible values: "memory | filesystem" +Possible values: 'memory' | 'filesystem' To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1234,7 +1234,7 @@ exports[`help should show help information using the "help --mode" option: stder exports[`help should show help information using the "help --mode" option: stdout 1`] = ` "Usage: webpack --mode Description: Enable production optimizations or development hints. -Possible values: "development | production | none" +Possible values: 'development' | 'production' | 'none' To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1269,12 +1269,26 @@ CLI documentation: https://webpack.js.org/api/cli/. Made with ♥ by the webpack team." `; +exports[`help should show help information using the "help --output-chunk-format" option: stderr 1`] = `""`; + +exports[`help should show help information using the "help --output-chunk-format" option: stdout 1`] = ` +"Usage: webpack --output-chunk-format +Description: The format of chunks (formats included by default are 'array-push' (web/WebWorker), 'commonjs' (node.js), 'module' (ESM), but others might be added by plugins). +Possible values: 'array-push' | 'commonjs' | 'module' | false + +To see list of all supported commands and options run 'webpack --help=verbose'. + +Webpack documentation: https://webpack.js.org/. +CLI documentation: https://webpack.js.org/api/cli/. +Made with ♥ by the webpack team." +`; + exports[`help should show help information using the "help --stats" option: stderr 1`] = `""`; exports[`help should show help information using the "help --stats" option: stdout 1`] = ` "Usage: webpack --stats [value] Description: Stats options object or preset name. -Possible values: "none | summary | errors-only | errors-warnings | minimal | normal | detailed | verbose" +Possible values: 'none' | 'summary' | 'errors-only' | 'errors-warnings' | 'minimal' | 'normal' | 'detailed' | 'verbose' To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1289,7 +1303,7 @@ exports[`help should show help information using the "help --target" option: std "Usage: webpack --target Short: webpack -t Description: Environment to build for. Environment to build for. An array of environments to build for all of them when possible. -Possible values: "false" +Possible values: false To see list of all supported commands and options run 'webpack --help=verbose'. @@ -1344,7 +1358,7 @@ exports[`help should show help information using the "help serve --mode" option: exports[`help should show help information using the "help serve --mode" option: stdout 1`] = ` "Usage: webpack serve --mode Description: Enable production optimizations or development hints. -Possible values: "development | production | none" +Possible values: 'development' | 'production' | 'none' To see list of all supported commands and options run 'webpack --help=verbose'. diff --git a/test/help/help.test.js b/test/help/help.test.js index 36096a2af0b..344e9537f8b 100644 --- a/test/help/help.test.js +++ b/test/help/help.test.js @@ -243,6 +243,14 @@ describe("help", () => { expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); + it('should show help information using the "help --output-chunk-format" option', async () => { + const { exitCode, stderr, stdout } = await run(__dirname, ["help", "--output-chunk-format"]); + + expect(exitCode).toBe(0); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + }); + it('should show help information using the "help --no-stats" option', async () => { const { exitCode, stderr, stdout } = await run(__dirname, ["help", "--no-stats"]);