diff --git a/packages/webpack-cli/src/types.ts b/packages/webpack-cli/src/types.ts index 882362e84f6..b09f64d43cc 100644 --- a/packages/webpack-cli/src/types.ts +++ b/packages/webpack-cli/src/types.ts @@ -9,8 +9,10 @@ import { type EntryOptions, type FileCacheOptions, type MultiCompiler, + type MultiCompilerOptions, type MultiStats, type Stats, + type StatsOptions, type WebpackError, type WebpackOptionsNormalized, default as webpack, @@ -25,6 +27,14 @@ import { * Webpack CLI */ +// TODO remove me and get it from webpack types +type ChildrenStatsOptions = undefined | string | boolean | StatsOptions; +type MultiStatsOptions = Omit & { + children?: ChildrenStatsOptions | ChildrenStatsOptions[]; +}; + +type WebpackCallback = (err: Error | undefined, stats: Stats | MultiStats | undefined) => void; + interface IWebpackCLI { colors: WebpackCLIColors; logger: WebpackCLILogger; @@ -67,7 +77,7 @@ interface IWebpackCLI { isValidationError(error: Error): error is WebpackError; createCompiler( options: Partial, - callback?: Callback<[Error | undefined, Stats | MultiStats | undefined]>, + callback?: WebpackCallback, ): Promise; needWatchStdin(compiler: Compiler | MultiCompiler): boolean; runWebpack(options: WebpackRunOptions, isWatchCommand: boolean): Promise; @@ -91,7 +101,7 @@ interface WebpackCLICommandOption extends CommanderOption { } interface WebpackCLIConfig { - options: WebpackConfiguration | WebpackConfiguration[]; + options: WebpackConfiguration | (WebpackConfiguration[] & MultiCompilerOptions); path: WeakMap; } @@ -176,8 +186,6 @@ type WebpackDevServerOptions = DevServerConfig & argv: Argv; }; -type Callback = (...args: T) => void; - /** * Webpack */ @@ -308,7 +316,6 @@ export { type BasicPrimitive, type CLIPluginOptions, type CallableWebpackConfiguration, - type Callback, type CommandAction, type CommanderOption, type DynamicImport, @@ -320,6 +327,7 @@ export { type JsonExt, type LoadableWebpackConfiguration, type ModuleName, + type MultiStatsOptions, type PackageInstallOptions, type PackageManager, type Path, @@ -340,6 +348,7 @@ export { type WebpackCLILogger, type WebpackCLIMainOption, type WebpackCLIOptions, + type WebpackCallback, type WebpackCompiler, type WebpackConfiguration, type WebpackDevServerOptions, diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 4a5823bb907..43c33c25dbb 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -3,8 +3,6 @@ import { type Help, type ParseOptions } from "commander"; import { type Compiler, type MultiCompiler, - type MultiStats, - type Stats, type StatsOptions, type WebpackError, default as webpack, @@ -18,7 +16,6 @@ import { type BasicPrimitive, type CLIPluginOptions, type CallableWebpackConfiguration, - type Callback, type CommandAction, type DynamicImport, type EnumValue, @@ -29,6 +26,7 @@ import { type JsonExt, type LoadableWebpackConfiguration, type ModuleName, + type MultiStatsOptions, type PackageInstallOptions, type PackageManager, type Path, @@ -50,6 +48,7 @@ import { type WebpackCLILogger, type WebpackCLIMainOption, type WebpackCLIOptions, + type WebpackCallback, type WebpackCompiler, type WebpackConfiguration, type WebpackDevServerOptions, @@ -547,10 +546,7 @@ class WebpackCLI implements IWebpackCLI { }) as WebpackCLICommand; if (commandOptions.description) { - command.description( - commandOptions.description, - commandOptions.argsDescription as Record, - ); + command.description(commandOptions.description, commandOptions.argsDescription!); } if (commandOptions.usage) { @@ -2338,7 +2334,7 @@ class WebpackCLI implements IWebpackCLI { async createCompiler( options: Partial, - callback?: Callback<[Error | undefined, Stats | MultiStats | undefined]>, + callback?: WebpackCallback, ): Promise { if (typeof options.configNodeEnv === "string") { process.env.NODE_ENV = options.configNodeEnv; @@ -2353,7 +2349,7 @@ class WebpackCLI implements IWebpackCLI { try { compiler = this.webpack( - config.options as WebpackConfiguration, + config.options, callback ? (error, stats) => { if (error && this.isValidationError(error)) { @@ -2399,7 +2395,7 @@ class WebpackCLI implements IWebpackCLI { createStringifyChunked = jsonExt.stringifyChunked; } - const callback = (error: Error | undefined, stats: Stats | MultiStats | undefined): void => { + const callback: WebpackCallback = (error, stats): void => { if (error) { this.logger.error(error); process.exit(2); @@ -2414,13 +2410,13 @@ class WebpackCLI implements IWebpackCLI { } const statsOptions = this.isMultipleCompiler(compiler) - ? { + ? ({ children: compiler.compilers.map((compiler) => compiler.options ? compiler.options.stats : undefined, ), - } + } as MultiStatsOptions) : compiler.options - ? compiler.options.stats + ? (compiler.options.stats as StatsOptions) : undefined; if (options.json && createStringifyChunked) {