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

refactor: reduce loops #3744

Merged
merged 3 commits into from
Apr 20, 2023
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
4 changes: 4 additions & 0 deletions packages/generators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GeneratorsCommand {
configs: [{ type: "string" }],
description: "Type of template",
defaultValue: "default",
helpLevel: "minimum",
},
{
name: "force",
Expand All @@ -37,6 +38,7 @@ class GeneratorsCommand {
},
],
description: "Generate without questions (ideally) using default answers",
helpLevel: "minimum",
},
],
async (generationPath: string, options: InitOptions) => {
Expand Down Expand Up @@ -76,6 +78,7 @@ class GeneratorsCommand {
configs: [{ type: "string" }],
description: "Type of template",
defaultValue: "default",
helpLevel: "minimum",
},
],
async (outputPath: string, options: LoaderOptions) => {
Expand Down Expand Up @@ -115,6 +118,7 @@ class GeneratorsCommand {
configs: [{ type: "string" }],
description: "Type of template",
defaultValue: "default",
helpLevel: "minimum",
},
],
async (outputPath: string, options: PluginOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ interface WebpackCLIBuiltInFlag {
describe?: string;
negatedDescription?: string;
defaultValue?: string;
helpLevel: "minimum" | "verbose";
}

interface WebpackCLIBuiltInOption extends WebpackCLIBuiltInFlag {
hidden?: boolean;
group?: "core";
helpLevel?: "minimum" | "verbose";
}

type WebpackCLIExternalCommandInfo = Pick<WebpackCLIOptions, "name" | "alias" | "description"> & {
Expand Down
57 changes: 25 additions & 32 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,15 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "To get the output in a specified format ( accept json or markdown )",
helpLevel: "minimum",
},
{
name: "additional-package",
alias: "a",
configs: [{ type: "string" }],
multiple: true,
description: "Adds additional packages to the output",
helpLevel: "minimum",
},
];
}
Expand Down Expand Up @@ -810,6 +812,7 @@ class WebpackCLI implements IWebpackCLI {
],
multiple: true,
description: "Provide path to a webpack configuration file e.g. ./webpack.config.js.",
helpLevel: "minimum",
},
{
name: "config-name",
Expand All @@ -820,6 +823,7 @@ class WebpackCLI implements IWebpackCLI {
],
multiple: true,
description: "Name of the configuration to use.",
helpLevel: "minimum",
},
{
name: "merge",
Expand All @@ -831,6 +835,7 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "Merge two or more configurations using 'webpack-merge'.",
helpLevel: "minimum",
},
{
name: "disable-interpret",
Expand All @@ -841,6 +846,7 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "Disable interpret for loading the config file.",
helpLevel: "minimum",
},
// Complex configs
{
Expand Down Expand Up @@ -888,6 +894,7 @@ class WebpackCLI implements IWebpackCLI {
},
multiple: true,
description: "Environment passed to the configuration when it is a function.",
helpLevel: "minimum",
},
{
name: "node-env",
Expand All @@ -898,6 +905,7 @@ class WebpackCLI implements IWebpackCLI {
],
multiple: false,
description: "Sets process.env.NODE_ENV to the specified value.",
helpLevel: "minimum",
},
{
name: "define-process-env-node-env",
Expand All @@ -909,6 +917,7 @@ class WebpackCLI implements IWebpackCLI {
multiple: false,
description:
"Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)",
helpLevel: "minimum",
},

// Adding more plugins
Expand All @@ -922,6 +931,7 @@ class WebpackCLI implements IWebpackCLI {
],
multiple: false,
description: "It invokes webpack-bundle-analyzer plugin to get bundle information.",
helpLevel: "minimum",
},
{
name: "progress",
Expand All @@ -935,6 +945,7 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "Print compilation progress during build.",
helpLevel: "minimum",
},

// Output options
Expand All @@ -951,6 +962,7 @@ class WebpackCLI implements IWebpackCLI {
],
alias: "j",
description: "Prints result as JSON or store it in a file.",
helpLevel: "minimum",
},
{
name: "fail-on-warnings",
Expand All @@ -961,11 +973,11 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "Stop webpack-cli process with non-zero exit code on warnings from webpack",
helpLevel: "minimum",
},
];

const minimumHelpFlags = [
...builtInFlags.map((flag) => flag.name),
"mode",
"watch",
"watch-options-stdin",
Expand All @@ -979,37 +991,18 @@ class WebpackCLI implements IWebpackCLI {

// Extract all the flags being exported from core.
// A list of cli flags generated by core can be found here https://github.com/webpack/webpack/blob/main/test/__snapshots__/Cli.basictest.js.snap
const coreArguments = Object.entries(this.webpack.cli.getArguments()).map(([flag, meta]) => {
const inBuiltIn = builtInFlags.find((builtInFlag) => builtInFlag.name === flag);

if (inBuiltIn) {
return {
...meta,
// @ts-expect-error this might be overwritten
name: flag,
group: "core",
...inBuiltIn,
configs: meta.configs || [],
};
}

return { ...meta, name: flag, group: "core" };
});

const options: WebpackCLIBuiltInOption[] = ([] as WebpackCLIBuiltInFlag[])
.concat(
builtInFlags.filter(
(builtInFlag) =>
!coreArguments.find((coreArgument) => builtInFlag.name === coreArgument.name),
),
)
.concat(coreArguments)
.map((option): WebpackCLIBuiltInOption => {
(option as WebpackCLIBuiltInOption).helpLevel = minimumHelpFlags.includes(option.name)
? "minimum"
: "verbose";
return option as WebpackCLIBuiltInOption;
});
const options = builtInFlags.concat(
Object.entries(this.webpack.cli.getArguments()).map<WebpackCLIBuiltInOption>(
([name, meta]) => {
return {
...meta,
name,
group: "core",
helpLevel: minimumHelpFlags.includes(name) ? "minimum" : "verbose",
};
},
),
);

this.builtInOptionsCache = options;

Expand Down
1 change: 1 addition & 0 deletions test/build/json/logging.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
mode: "development",
infrastructureLogging: {
level: "log",
},
Expand Down