Skip to content

refactor: types #4503

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

Merged
merged 2 commits into from
Jul 22, 2025
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
19 changes: 14 additions & 5 deletions packages/webpack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<StatsOptions, "children"> & {
children?: ChildrenStatsOptions | ChildrenStatsOptions[];
};

type WebpackCallback = (err: Error | undefined, stats: Stats | MultiStats | undefined) => void;

interface IWebpackCLI {
colors: WebpackCLIColors;
logger: WebpackCLILogger;
Expand Down Expand Up @@ -67,7 +77,7 @@ interface IWebpackCLI {
isValidationError(error: Error): error is WebpackError;
createCompiler(
options: Partial<WebpackDevServerOptions>,
callback?: Callback<[Error | undefined, Stats | MultiStats | undefined]>,
callback?: WebpackCallback,
): Promise<WebpackCompiler>;
needWatchStdin(compiler: Compiler | MultiCompiler): boolean;
runWebpack(options: WebpackRunOptions, isWatchCommand: boolean): Promise<void>;
Expand All @@ -91,7 +101,7 @@ interface WebpackCLICommandOption extends CommanderOption {
}

interface WebpackCLIConfig {
options: WebpackConfiguration | WebpackConfiguration[];
options: WebpackConfiguration | (WebpackConfiguration[] & MultiCompilerOptions);
path: WeakMap<object, string[]>;
}

Expand Down Expand Up @@ -176,8 +186,6 @@ type WebpackDevServerOptions = DevServerConfig &
argv: Argv;
};

type Callback<T extends unknown[]> = (...args: T) => void;

/**
* Webpack
*/
Expand Down Expand Up @@ -308,7 +316,6 @@ export {
type BasicPrimitive,
type CLIPluginOptions,
type CallableWebpackConfiguration,
type Callback,
type CommandAction,
type CommanderOption,
type DynamicImport,
Expand All @@ -320,6 +327,7 @@ export {
type JsonExt,
type LoadableWebpackConfiguration,
type ModuleName,
type MultiStatsOptions,
type PackageInstallOptions,
type PackageManager,
type Path,
Expand All @@ -340,6 +348,7 @@ export {
type WebpackCLILogger,
type WebpackCLIMainOption,
type WebpackCLIOptions,
type WebpackCallback,
type WebpackCompiler,
type WebpackConfiguration,
type WebpackDevServerOptions,
Expand Down
22 changes: 9 additions & 13 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,7 +16,6 @@ import {
type BasicPrimitive,
type CLIPluginOptions,
type CallableWebpackConfiguration,
type Callback,
type CommandAction,
type DynamicImport,
type EnumValue,
Expand All @@ -29,6 +26,7 @@ import {
type JsonExt,
type LoadableWebpackConfiguration,
type ModuleName,
type MultiStatsOptions,
type PackageInstallOptions,
type PackageManager,
type Path,
Expand All @@ -50,6 +48,7 @@ import {
type WebpackCLILogger,
type WebpackCLIMainOption,
type WebpackCLIOptions,
type WebpackCallback,
type WebpackCompiler,
type WebpackConfiguration,
type WebpackDevServerOptions,
Expand Down Expand Up @@ -547,10 +546,7 @@ class WebpackCLI implements IWebpackCLI {
}) as WebpackCLICommand;

if (commandOptions.description) {
command.description(
commandOptions.description,
commandOptions.argsDescription as Record<string, string>,
);
command.description(commandOptions.description, commandOptions.argsDescription!);
}

if (commandOptions.usage) {
Expand Down Expand Up @@ -2338,7 +2334,7 @@ class WebpackCLI implements IWebpackCLI {

async createCompiler(
options: Partial<WebpackDevServerOptions>,
callback?: Callback<[Error | undefined, Stats | MultiStats | undefined]>,
callback?: WebpackCallback,
): Promise<WebpackCompiler> {
if (typeof options.configNodeEnv === "string") {
process.env.NODE_ENV = options.configNodeEnv;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Loading