Skip to content

Commit e6b7cee

Browse files
refactor: types
1 parent 582e316 commit e6b7cee

File tree

2 files changed

+52
-58
lines changed

2 files changed

+52
-58
lines changed

packages/webpack-cli/src/types.ts

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type {
22
EntryOptions,
33
Stats,
4+
MultiStats,
45
Configuration,
56
WebpackError,
6-
StatsOptions,
77
WebpackOptionsNormalized,
88
Compiler,
99
MultiCompiler,
@@ -70,7 +70,7 @@ interface IWebpackCLI {
7070
isValidationError(error: Error): error is WebpackError;
7171
createCompiler(
7272
options: Partial<WebpackDevServerOptions>,
73-
callback?: Callback<[Error | undefined, WebpackCLIStats | undefined]>,
73+
callback?: Callback<[Error | undefined, Stats | MultiStats | undefined]>,
7474
): Promise<WebpackCompiler>;
7575
needWatchStdin(compiler: Compiler | MultiCompiler): boolean;
7676
runWebpack(options: WebpackRunOptions, isWatchCommand: boolean): Promise<void>;
@@ -104,10 +104,6 @@ interface WebpackCLICommand extends Command {
104104
_args: WebpackCLICommandOption[];
105105
}
106106

107-
interface WebpackCLIStats extends Stats {
108-
presetToOptions?: (item: string | boolean) => StatsOptions;
109-
}
110-
111107
type WebpackCLIMainOption = Pick<
112108
WebpackCLIBuiltInOption,
113109
"valueName" | "description" | "defaultValue" | "multiple"
@@ -141,7 +137,7 @@ interface WebpackCLIBuiltInFlag {
141137
negative?: boolean;
142138
multiple?: boolean;
143139
valueName?: string;
144-
description: string;
140+
description?: string;
145141
describe?: string;
146142
negatedDescription?: string;
147143
defaultValue?: string;
@@ -189,8 +185,10 @@ type Callback<T extends unknown[]> = (...args: T) => void;
189185
* Webpack
190186
*/
191187
type WebpackConfiguration = Configuration;
192-
type ConfigOptions = PotentialPromise<WebpackConfiguration | CallableOption>;
193-
type CallableOption = (env: Env | undefined, argv: Argv) => WebpackConfiguration;
188+
type LoadableWebpackConfiguration = PotentialPromise<
189+
WebpackConfiguration | CallableWebpackConfiguration
190+
>;
191+
type CallableWebpackConfiguration = (env: Env | undefined, argv: Argv) => WebpackConfiguration;
194192
type WebpackCompiler = Compiler | MultiCompiler;
195193

196194
type FlagType = boolean | "enum" | "string" | "path" | "number" | "boolean" | "RegExp" | "reset";
@@ -226,12 +224,6 @@ type PackageManager = "pnpm" | "yarn" | "npm";
226224
interface PackageInstallOptions {
227225
preMessage?: () => void;
228226
}
229-
interface BasicPackageJsonContent {
230-
name: string;
231-
version: string;
232-
description: string;
233-
license: string;
234-
}
235227

236228
/**
237229
* Plugins and util types
@@ -309,7 +301,6 @@ export {
309301
WebpackCLIBuiltInOption,
310302
WebpackCLIBuiltInFlag,
311303
WebpackCLIColors,
312-
WebpackCLIStats,
313304
WebpackCLIConfig,
314305
WebpackCLIExternalCommandInfo,
315306
WebpackCLIOptions,
@@ -324,14 +315,13 @@ export {
324315
Argv,
325316
Argument,
326317
BasicPrimitive,
327-
BasicPackageJsonContent,
328-
CallableOption,
318+
CallableWebpackConfiguration,
329319
Callback,
330320
CLIPluginOptions,
331321
CommandAction,
332322
CommanderOption,
333323
CommandOptions,
334-
ConfigOptions,
324+
LoadableWebpackConfiguration,
335325
DynamicImport,
336326
FileSystemCacheOptions,
337327
FlagConfig,

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

+43-39
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type {
44
WebpackCLIBuiltInOption,
55
WebpackCLIBuiltInFlag,
66
WebpackCLIColors,
7-
WebpackCLIStats,
87
WebpackCLIConfig,
98
WebpackCLIExternalCommandInfo,
109
WebpackCLIOptions,
@@ -18,11 +17,11 @@ import type {
1817
WebpackConfiguration,
1918
Argv,
2019
BasicPrimitive,
21-
CallableOption,
20+
CallableWebpackConfiguration,
2221
Callback,
2322
CLIPluginOptions,
2423
CommandAction,
25-
ConfigOptions,
24+
LoadableWebpackConfiguration,
2625
DynamicImport,
2726
FileSystemCacheOptions,
2827
FlagConfig,
@@ -49,12 +48,14 @@ import {
4948
type MultiCompiler,
5049
type WebpackError,
5150
type StatsOptions,
52-
type WebpackOptionsNormalized,
51+
type Stats,
52+
type MultiStats,
5353
} from "webpack";
5454
import { type stringifyChunked } from "@discoveryjs/json-ext";
5555
import { type Help, type ParseOptions } from "commander";
5656

5757
import { type CLIPlugin as CLIPluginClass } from "./plugins/cli-plugin";
58+
import * as console from "node:console";
5859
const fs = require("fs");
5960
const { Readable } = require("stream");
6061
const path = require("path");
@@ -106,9 +107,11 @@ class WebpackCLI implements IWebpackCLI {
106107
isMultipleCompiler(compiler: WebpackCompiler): compiler is MultiCompiler {
107108
return (compiler as MultiCompiler).compilers as unknown as boolean;
108109
}
110+
109111
isPromise<T>(value: Promise<T>): value is Promise<T> {
110112
return typeof (value as unknown as Promise<T>).then === "function";
111113
}
114+
112115
isFunction(value: unknown): value is CallableFunction {
113116
return typeof value === "function";
114117
}
@@ -553,7 +556,7 @@ class WebpackCLI implements IWebpackCLI {
553556
if (Array.isArray(commandOptions.alias)) {
554557
command.aliases(commandOptions.alias);
555558
} else {
556-
command.alias(commandOptions.alias as string);
559+
command.alias(commandOptions.alias);
557560
}
558561

559562
if (commandOptions.pkg) {
@@ -1069,7 +1072,7 @@ class WebpackCLI implements IWebpackCLI {
10691072
return {
10701073
...meta,
10711074
name,
1072-
description: meta.description as string,
1075+
description: meta.description,
10731076
group: "core",
10741077
helpLevel: minimumHelpFlags.includes(name) ? "minimum" : "verbose",
10751078
};
@@ -1792,7 +1795,10 @@ class WebpackCLI implements IWebpackCLI {
17921795
typeof options.disableInterpret !== "undefined" && options.disableInterpret;
17931796

17941797
const interpret = require("interpret");
1795-
const loadConfigByPath = async (configPath: string, argv: Argv = {}) => {
1798+
const loadConfigByPath = async (
1799+
configPath: string,
1800+
argv: Argv = {},
1801+
): Promise<{ options: WebpackConfiguration | WebpackConfiguration[]; path: string }> => {
17961802
const ext = path.extname(configPath).toLowerCase();
17971803
let interpreted = Object.keys(interpret.jsVariants).find((variant) => variant === ext);
17981804
// Fallback `.cts` to `.ts`
@@ -1823,7 +1829,7 @@ class WebpackCLI implements IWebpackCLI {
18231829
}
18241830
}
18251831

1826-
let options: ConfigOptions | ConfigOptions[];
1832+
let options: LoadableWebpackConfiguration | LoadableWebpackConfiguration[];
18271833

18281834
type LoadConfigOption = PotentialPromise<WebpackConfiguration>;
18291835

@@ -1866,26 +1872,30 @@ class WebpackCLI implements IWebpackCLI {
18661872

18671873
if (Array.isArray(options)) {
18681874
// reassign the value to assert type
1869-
const optionsArray: ConfigOptions[] = options;
1875+
const optionsArray: LoadableWebpackConfiguration[] = options;
18701876
await Promise.all(
18711877
optionsArray.map(async (_, i) => {
18721878
if (
1873-
this.isPromise<WebpackConfiguration | CallableOption>(
1874-
optionsArray[i] as Promise<WebpackConfiguration | CallableOption>,
1879+
this.isPromise<WebpackConfiguration | CallableWebpackConfiguration>(
1880+
optionsArray[i] as Promise<WebpackConfiguration | CallableWebpackConfiguration>,
18751881
)
18761882
) {
18771883
optionsArray[i] = await optionsArray[i];
18781884
}
18791885
// `Promise` may return `Function`
18801886
if (this.isFunction(optionsArray[i])) {
18811887
// when config is a function, pass the env from args to the config function
1882-
optionsArray[i] = await (optionsArray[i] as CallableOption)(argv.env, argv);
1888+
optionsArray[i] = await optionsArray[i](argv.env, argv);
18831889
}
18841890
}),
18851891
);
18861892
options = optionsArray;
18871893
} else {
1888-
if (this.isPromise<ConfigOptions>(options as Promise<ConfigOptions>)) {
1894+
if (
1895+
this.isPromise<LoadableWebpackConfiguration>(
1896+
options as Promise<LoadableWebpackConfiguration>,
1897+
)
1898+
) {
18891899
options = await options;
18901900
}
18911901

@@ -1905,11 +1915,14 @@ class WebpackCLI implements IWebpackCLI {
19051915
process.exit(2);
19061916
}
19071917

1908-
return { options, path: configPath };
1918+
return {
1919+
options: options as WebpackConfiguration | WebpackConfiguration[],
1920+
path: configPath,
1921+
};
19091922
};
19101923

19111924
const config: WebpackCLIConfig = {
1912-
options: {} as WebpackConfiguration,
1925+
options: {},
19131926
path: new WeakMap(),
19141927
};
19151928

@@ -1923,27 +1936,25 @@ class WebpackCLI implements IWebpackCLI {
19231936
config.options = [];
19241937

19251938
loadedConfigs.forEach((loadedConfig) => {
1926-
const isArray = Array.isArray(loadedConfig.options);
1927-
19281939
// TODO we should run webpack multiple times when the `--config` options have multiple values with `--merge`, need to solve for the next major release
1929-
if ((config.options as ConfigOptions[]).length === 0) {
1940+
if ((config.options as LoadableWebpackConfiguration[]).length === 0) {
19301941
config.options = loadedConfig.options as WebpackConfiguration;
19311942
} else {
19321943
if (!Array.isArray(config.options)) {
19331944
config.options = [config.options];
19341945
}
19351946

1936-
if (isArray) {
1937-
for (const item of loadedConfig.options as ConfigOptions[]) {
1938-
(config.options as ConfigOptions[]).push(item);
1947+
if (Array.isArray(loadedConfig.options)) {
1948+
for (const item of loadedConfig.options) {
1949+
config.options.push(item);
19391950
}
19401951
} else {
19411952
config.options.push(loadedConfig.options as WebpackConfiguration);
19421953
}
19431954
}
19441955

1945-
if (isArray) {
1946-
for (const options of loadedConfig.options as ConfigOptions[]) {
1956+
if (Array.isArray(loadedConfig.options)) {
1957+
for (const options of loadedConfig.options) {
19471958
config.path.set(options, [loadedConfig.path]);
19481959
}
19491960
} else {
@@ -1985,7 +1996,7 @@ class WebpackCLI implements IWebpackCLI {
19851996
if (foundDefaultConfigFile) {
19861997
const loadedConfig = await loadConfigByPath(foundDefaultConfigFile, options.argv);
19871998

1988-
config.options = loadedConfig.options as WebpackConfiguration[];
1999+
config.options = loadedConfig.options;
19892000

19902001
if (Array.isArray(config.options)) {
19912002
for (const item of config.options) {
@@ -2000,7 +2011,7 @@ class WebpackCLI implements IWebpackCLI {
20002011
if (options.configName) {
20012012
const notFoundConfigNames: string[] = [];
20022013

2003-
config.options = options.configName.map((configName: string) => {
2014+
config.options = options.configName.map((configName) => {
20042015
let found;
20052016

20062017
if (Array.isArray(config.options)) {
@@ -2280,11 +2291,7 @@ class WebpackCLI implements IWebpackCLI {
22802291

22812292
if (Array.isArray(configPath)) {
22822293
for (const oneOfConfigPath of configPath) {
2283-
(
2284-
item.cache.buildDependencies as NonNullable<
2285-
FileSystemCacheOptions["cache"]["buildDependencies"]
2286-
>
2287-
).defaultConfig.push(oneOfConfigPath);
2294+
item.cache.buildDependencies.defaultConfig.push(oneOfConfigPath);
22882295
}
22892296
} else {
22902297
item.cache.buildDependencies.defaultConfig.push(configPath);
@@ -2320,8 +2327,8 @@ class WebpackCLI implements IWebpackCLI {
23202327
colors = Boolean(this.isColorSupportChanged);
23212328
}
23222329
// From stats
2323-
else if (typeof (item.stats as StatsOptions).colors !== "undefined") {
2324-
colors = (item.stats as StatsOptions).colors;
2330+
else if (typeof item.stats.colors !== "undefined") {
2331+
colors = item.stats.colors;
23252332
}
23262333
// Default
23272334
else {
@@ -2363,7 +2370,7 @@ class WebpackCLI implements IWebpackCLI {
23632370

23642371
async createCompiler(
23652372
options: Partial<WebpackDevServerOptions>,
2366-
callback?: Callback<[Error | undefined, WebpackCLIStats | undefined]>,
2373+
callback?: Callback<[Error | undefined, Stats | MultiStats | undefined]>,
23672374
): Promise<WebpackCompiler> {
23682375
if (typeof options.configNodeEnv === "string") {
23692376
process.env.NODE_ENV = options.configNodeEnv;
@@ -2407,7 +2414,7 @@ class WebpackCLI implements IWebpackCLI {
24072414
needWatchStdin(compiler: Compiler | MultiCompiler): boolean {
24082415
if (this.isMultipleCompiler(compiler)) {
24092416
return Boolean(
2410-
(compiler as MultiCompiler).compilers.some(
2417+
compiler.compilers.some(
24112418
(compiler: Compiler) =>
24122419
compiler.options.watchOptions && compiler.options.watchOptions.stdin,
24132420
),
@@ -2428,7 +2435,7 @@ class WebpackCLI implements IWebpackCLI {
24282435
createStringifyChunked = jsonExt.stringifyChunked;
24292436
}
24302437

2431-
const callback = (error: Error | undefined, stats: WebpackCLIStats | undefined): void => {
2438+
const callback = (error: Error | undefined, stats: Stats | MultiStats | undefined): void => {
24322439
if (error) {
24332440
this.logger.error(error);
24342441
process.exit(2);
@@ -2479,10 +2486,7 @@ class WebpackCLI implements IWebpackCLI {
24792486
});
24802487
}
24812488
} else {
2482-
const printedStats = stats.toString(
2483-
// TODO fix me in webpack
2484-
statsOptions as Exclude<WebpackOptionsNormalized["stats"], boolean>,
2485-
);
2489+
const printedStats = stats.toString(statsOptions as StatsOptions);
24862490

24872491
// Avoid extra empty line when `stats: 'none'`
24882492
if (printedStats) {

0 commit comments

Comments
 (0)