Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve help output for possible values #4316

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1645,9 +1645,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}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/api/__snapshots__/CLI.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
],
[
"",
Expand Down
24 changes: 19 additions & 5 deletions test/help/__snapshots__/help.test.js.snap.devServer5.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -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 <value>
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'.

Expand All @@ -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 <value>
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'.

Expand Down Expand Up @@ -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 <value>
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'.

Expand All @@ -1289,7 +1303,7 @@ exports[`help should show help information using the "help --target" option: std
"Usage: webpack --target <value...>
Short: webpack -t <value...>
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'.

Expand Down Expand Up @@ -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 <value>
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'.

Expand Down
8 changes: 8 additions & 0 deletions test/help/help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);

Expand Down
Loading