Skip to content

Commit 59efd2d

Browse files
refactor: code
1 parent 7c60a48 commit 59efd2d

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type MultiStatsOptions,
77
type StatsOptions,
88
type WebpackError,
9-
cli,
109
default as webpack,
1110
} from "webpack";
1211
import type webpackMerge from "webpack-merge";
@@ -104,9 +103,12 @@ class WebpackCLI implements IWebpackCLI {
104103
this.program = program;
105104
this.program.name("webpack");
106105
this.program.configureOutput({
107-
writeErr: this.logger.error,
108-
outputError: (str, write) =>
109-
write(`Error: ${this.capitalizeFirstLetter(str.replace(/^error:/, "").trim())}`),
106+
writeErr: (str) => {
107+
this.logger.error(str);
108+
},
109+
outputError: (str, write) => {
110+
write(`Error: ${this.capitalizeFirstLetter(str.replace(/^error:/, "").trim())}`);
111+
},
110112
});
111113
}
112114

@@ -135,11 +137,17 @@ class WebpackCLI implements IWebpackCLI {
135137
}
136138

137139
createColors(useColor?: boolean): WebpackCLIColors {
138-
if (typeof cli.createColors === "function") {
139-
const { createColors, isColorSupported } = cli;
140-
const shouldUseColor = useColor || isColorSupported();
140+
try {
141+
const { cli } = require("webpack");
141142

142-
return { ...createColors({ useColor: shouldUseColor }), isColorSupported: shouldUseColor };
143+
if (typeof cli.createColors === "function") {
144+
const { createColors, isColorSupported } = cli;
145+
const shouldUseColor = useColor || isColorSupported();
146+
147+
return { ...createColors({ useColor: shouldUseColor }), isColorSupported: shouldUseColor };
148+
}
149+
} catch {
150+
// Nothing
143151
}
144152

145153
// TODO remove `colorette` and set [email protected] as the minimum supported version in the next major release
@@ -1332,16 +1340,14 @@ class WebpackCLI implements IWebpackCLI {
13321340
});
13331341

13341342
this.program.option("--color", "Enable colors on console.");
1335-
this.program.on("option:color", function color() {
1336-
// @ts-expect-error shadowing 'this' is intended
1343+
this.program.on("option:color", function color(this: WebpackCLICommand) {
13371344
const { color } = this.opts();
13381345

13391346
cli.isColorSupportChanged = color;
13401347
cli.colors = cli.createColors(color);
13411348
});
13421349
this.program.option("--no-color", "Disable colors on console.");
1343-
this.program.on("option:no-color", function noColor() {
1344-
// @ts-expect-error shadowing 'this' is intended
1350+
this.program.on("option:no-color", function noColor(this: WebpackCLICommand) {
13451351
const { color } = this.opts();
13461352

13471353
cli.isColorSupportChanged = color;
@@ -1839,8 +1845,7 @@ class WebpackCLI implements IWebpackCLI {
18391845
false,
18401846
moduleType,
18411847
);
1842-
// @ts-expect-error error type assertion
1843-
} catch (error: Error) {
1848+
} catch (error) {
18441849
this.logger.error(`Failed to load '${configPath}' config`);
18451850

18461851
if (this.isValidationError(error)) {
@@ -2337,8 +2342,10 @@ class WebpackCLI implements IWebpackCLI {
23372342
return config;
23382343
}
23392344

2340-
isValidationError(error: Error): error is WebpackError {
2341-
return error instanceof this.webpack.ValidationError || error.name === "ValidationError";
2345+
isValidationError(error: unknown): error is WebpackError {
2346+
return (
2347+
error instanceof this.webpack.ValidationError || (error as Error).name === "ValidationError"
2348+
);
23422349
}
23432350

23442351
async createCompiler(
@@ -2370,8 +2377,7 @@ class WebpackCLI implements IWebpackCLI {
23702377
}
23712378
: callback,
23722379
)!;
2373-
// @ts-expect-error error type assertion
2374-
} catch (error: Error) {
2380+
} catch (error) {
23752381
if (this.isValidationError(error)) {
23762382
this.logger.error(error.message);
23772383
} else {

0 commit comments

Comments
 (0)