Skip to content

Commit f515ecc

Browse files
refactor: types (#4503)
1 parent bc18a6e commit f515ecc

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

packages/webpack-cli/src/types.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99
type EntryOptions,
1010
type FileCacheOptions,
1111
type MultiCompiler,
12+
type MultiCompilerOptions,
1213
type MultiStats,
1314
type Stats,
15+
type StatsOptions,
1416
type WebpackError,
1517
type WebpackOptionsNormalized,
1618
default as webpack,
@@ -25,6 +27,14 @@ import {
2527
* Webpack CLI
2628
*/
2729

30+
// TODO remove me and get it from webpack types
31+
type ChildrenStatsOptions = undefined | string | boolean | StatsOptions;
32+
type MultiStatsOptions = Omit<StatsOptions, "children"> & {
33+
children?: ChildrenStatsOptions | ChildrenStatsOptions[];
34+
};
35+
36+
type WebpackCallback = (err: Error | undefined, stats: Stats | MultiStats | undefined) => void;
37+
2838
interface IWebpackCLI {
2939
colors: WebpackCLIColors;
3040
logger: WebpackCLILogger;
@@ -67,7 +77,7 @@ interface IWebpackCLI {
6777
isValidationError(error: Error): error is WebpackError;
6878
createCompiler(
6979
options: Partial<WebpackDevServerOptions>,
70-
callback?: Callback<[Error | undefined, Stats | MultiStats | undefined]>,
80+
callback?: WebpackCallback,
7181
): Promise<WebpackCompiler>;
7282
needWatchStdin(compiler: Compiler | MultiCompiler): boolean;
7383
runWebpack(options: WebpackRunOptions, isWatchCommand: boolean): Promise<void>;
@@ -91,7 +101,7 @@ interface WebpackCLICommandOption extends CommanderOption {
91101
}
92102

93103
interface WebpackCLIConfig {
94-
options: WebpackConfiguration | WebpackConfiguration[];
104+
options: WebpackConfiguration | (WebpackConfiguration[] & MultiCompilerOptions);
95105
path: WeakMap<object, string[]>;
96106
}
97107

@@ -176,8 +186,6 @@ type WebpackDevServerOptions = DevServerConfig &
176186
argv: Argv;
177187
};
178188

179-
type Callback<T extends unknown[]> = (...args: T) => void;
180-
181189
/**
182190
* Webpack
183191
*/
@@ -308,7 +316,6 @@ export {
308316
type BasicPrimitive,
309317
type CLIPluginOptions,
310318
type CallableWebpackConfiguration,
311-
type Callback,
312319
type CommandAction,
313320
type CommanderOption,
314321
type DynamicImport,
@@ -320,6 +327,7 @@ export {
320327
type JsonExt,
321328
type LoadableWebpackConfiguration,
322329
type ModuleName,
330+
type MultiStatsOptions,
323331
type PackageInstallOptions,
324332
type PackageManager,
325333
type Path,
@@ -340,6 +348,7 @@ export {
340348
type WebpackCLILogger,
341349
type WebpackCLIMainOption,
342350
type WebpackCLIOptions,
351+
type WebpackCallback,
343352
type WebpackCompiler,
344353
type WebpackConfiguration,
345354
type WebpackDevServerOptions,

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { type Help, type ParseOptions } from "commander";
33
import {
44
type Compiler,
55
type MultiCompiler,
6-
type MultiStats,
7-
type Stats,
86
type StatsOptions,
97
type WebpackError,
108
default as webpack,
@@ -18,7 +16,6 @@ import {
1816
type BasicPrimitive,
1917
type CLIPluginOptions,
2018
type CallableWebpackConfiguration,
21-
type Callback,
2219
type CommandAction,
2320
type DynamicImport,
2421
type EnumValue,
@@ -29,6 +26,7 @@ import {
2926
type JsonExt,
3027
type LoadableWebpackConfiguration,
3128
type ModuleName,
29+
type MultiStatsOptions,
3230
type PackageInstallOptions,
3331
type PackageManager,
3432
type Path,
@@ -50,6 +48,7 @@ import {
5048
type WebpackCLILogger,
5149
type WebpackCLIMainOption,
5250
type WebpackCLIOptions,
51+
type WebpackCallback,
5352
type WebpackCompiler,
5453
type WebpackConfiguration,
5554
type WebpackDevServerOptions,
@@ -547,10 +546,7 @@ class WebpackCLI implements IWebpackCLI {
547546
}) as WebpackCLICommand;
548547

549548
if (commandOptions.description) {
550-
command.description(
551-
commandOptions.description,
552-
commandOptions.argsDescription as Record<string, string>,
553-
);
549+
command.description(commandOptions.description, commandOptions.argsDescription!);
554550
}
555551

556552
if (commandOptions.usage) {
@@ -2338,7 +2334,7 @@ class WebpackCLI implements IWebpackCLI {
23382334

23392335
async createCompiler(
23402336
options: Partial<WebpackDevServerOptions>,
2341-
callback?: Callback<[Error | undefined, Stats | MultiStats | undefined]>,
2337+
callback?: WebpackCallback,
23422338
): Promise<WebpackCompiler> {
23432339
if (typeof options.configNodeEnv === "string") {
23442340
process.env.NODE_ENV = options.configNodeEnv;
@@ -2353,7 +2349,7 @@ class WebpackCLI implements IWebpackCLI {
23532349

23542350
try {
23552351
compiler = this.webpack(
2356-
config.options as WebpackConfiguration,
2352+
config.options,
23572353
callback
23582354
? (error, stats) => {
23592355
if (error && this.isValidationError(error)) {
@@ -2399,7 +2395,7 @@ class WebpackCLI implements IWebpackCLI {
23992395
createStringifyChunked = jsonExt.stringifyChunked;
24002396
}
24012397

2402-
const callback = (error: Error | undefined, stats: Stats | MultiStats | undefined): void => {
2398+
const callback: WebpackCallback = (error, stats): void => {
24032399
if (error) {
24042400
this.logger.error(error);
24052401
process.exit(2);
@@ -2414,13 +2410,13 @@ class WebpackCLI implements IWebpackCLI {
24142410
}
24152411

24162412
const statsOptions = this.isMultipleCompiler(compiler)
2417-
? {
2413+
? ({
24182414
children: compiler.compilers.map((compiler) =>
24192415
compiler.options ? compiler.options.stats : undefined,
24202416
),
2421-
}
2417+
} as MultiStatsOptions)
24222418
: compiler.options
2423-
? compiler.options.stats
2419+
? (compiler.options.stats as StatsOptions)
24242420
: undefined;
24252421

24262422
if (options.json && createStringifyChunked) {

0 commit comments

Comments
 (0)