Skip to content

Commit 9b88e10

Browse files
refactor: code
1 parent a5044f8 commit 9b88e10

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"ts-node": "^10.9.1",
9898
"typescript": "^5.0.4",
9999
"typescript-eslint": "^8.35.0",
100-
"webpack": "^5.99.1",
100+
"webpack": "^5.101.0",
101101
"webpack-bundle-analyzer": "^4.5.0",
102102
"webpack-dev-server": "^5.1.0"
103103
},

packages/webpack-cli/src/types.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { type stringifyChunked } from "@discoveryjs/json-ext";
2-
import { type Colorette } from "colorette";
32
import { type Command, type CommandOptions, type Option, type ParseOptions } from "commander";
43
import { type prepare } from "rechoir";
54
import {
65
type AssetEmittedInfo,
6+
type Colors,
77
type Compiler,
88
type Configuration,
99
type EntryOptions,
@@ -12,7 +12,6 @@ import {
1212
type MultiCompilerOptions,
1313
type MultiStats,
1414
type Stats,
15-
type StatsOptions,
1615
type WebpackError,
1716
type WebpackOptionsNormalized,
1817
default as webpack,
@@ -27,14 +26,9 @@ import {
2726
* Webpack CLI
2827
*/
2928

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-
3629
type WebpackCallback = (err: Error | undefined, stats: Stats | MultiStats | undefined) => void;
3730

31+
// TODO remove me in the next major release, we don't need extra interface
3832
interface IWebpackCLI {
3933
colors: WebpackCLIColors;
4034
logger: WebpackCLILogger;
@@ -83,7 +77,7 @@ interface IWebpackCLI {
8377
runWebpack(options: WebpackRunOptions, isWatchCommand: boolean): Promise<void>;
8478
}
8579

86-
interface WebpackCLIColors extends Colorette {
80+
interface WebpackCLIColors extends Colors {
8781
isColorSupported: boolean;
8882
}
8983

@@ -327,7 +321,6 @@ export {
327321
type JsonExt,
328322
type LoadableWebpackConfiguration,
329323
type ModuleName,
330-
type MultiStatsOptions,
331324
type PackageInstallOptions,
332325
type PackageManager,
333326
type Path,

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { type Help, type ParseOptions } from "commander";
33
import {
44
type Compiler,
55
type MultiCompiler,
6+
type MultiStatsOptions,
67
type StatsOptions,
78
type WebpackError,
89
default as webpack,
@@ -26,7 +27,6 @@ import {
2627
type JsonExt,
2728
type LoadableWebpackConfiguration,
2829
type ModuleName,
29-
type MultiStatsOptions,
3030
type PackageInstallOptions,
3131
type PackageManager,
3232
type Path,
@@ -103,9 +103,12 @@ class WebpackCLI implements IWebpackCLI {
103103
this.program = program;
104104
this.program.name("webpack");
105105
this.program.configureOutput({
106-
writeErr: this.logger.error,
107-
outputError: (str, write) =>
108-
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+
},
109112
});
110113
}
111114

@@ -134,6 +137,20 @@ class WebpackCLI implements IWebpackCLI {
134137
}
135138

136139
createColors(useColor?: boolean): WebpackCLIColors {
140+
try {
141+
const { cli } = require("webpack");
142+
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
151+
}
152+
153+
// TODO remove `colorette` and set [email protected] as the minimum supported version in the next major release
137154
const { createColors, isColorSupported } = require("colorette");
138155

139156
const shouldUseColor = useColor || isColorSupported;
@@ -1323,16 +1340,14 @@ class WebpackCLI implements IWebpackCLI {
13231340
});
13241341

13251342
this.program.option("--color", "Enable colors on console.");
1326-
this.program.on("option:color", function color() {
1327-
// @ts-expect-error shadowing 'this' is intended
1343+
this.program.on("option:color", function color(this: WebpackCLICommand) {
13281344
const { color } = this.opts();
13291345

13301346
cli.isColorSupportChanged = color;
13311347
cli.colors = cli.createColors(color);
13321348
});
13331349
this.program.option("--no-color", "Disable colors on console.");
1334-
this.program.on("option:no-color", function noColor() {
1335-
// @ts-expect-error shadowing 'this' is intended
1350+
this.program.on("option:no-color", function noColor(this: WebpackCLICommand) {
13361351
const { color } = this.opts();
13371352

13381353
cli.isColorSupportChanged = color;
@@ -1830,8 +1845,7 @@ class WebpackCLI implements IWebpackCLI {
18301845
false,
18311846
moduleType,
18321847
);
1833-
// @ts-expect-error error type assertion
1834-
} catch (error: Error) {
1848+
} catch (error) {
18351849
this.logger.error(`Failed to load '${configPath}' config`);
18361850

18371851
if (this.isValidationError(error)) {
@@ -2328,8 +2342,10 @@ class WebpackCLI implements IWebpackCLI {
23282342
return config;
23292343
}
23302344

2331-
isValidationError(error: Error): error is WebpackError {
2332-
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+
);
23332349
}
23342350

23352351
async createCompiler(
@@ -2360,9 +2376,8 @@ class WebpackCLI implements IWebpackCLI {
23602376
callback(error as Error | undefined, stats);
23612377
}
23622378
: callback,
2363-
);
2364-
// @ts-expect-error error type assertion
2365-
} catch (error: Error) {
2379+
)!;
2380+
} catch (error) {
23662381
if (this.isValidationError(error)) {
23672382
this.logger.error(error.message);
23682383
} else {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11911,10 +11911,10 @@ webpack-sources@^3.3.3:
1191111911
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.3.3.tgz#d4bf7f9909675d7a070ff14d0ef2a4f3c982c723"
1191211912
integrity sha512-yd1RBzSGanHkitROoPFd6qsrxt+oFhg/129YzheDGqeustzX0vTZJZsSsQjVQC4yzBQ56K55XU8gaNCtIzOnTg==
1191311913

11914-
webpack@^5.99.1:
11915-
version "5.100.2"
11916-
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.100.2.tgz#e2341facf9f7de1d702147c91bcb65b693adf9e8"
11917-
integrity sha512-QaNKAvGCDRh3wW1dsDjeMdDXwZm2vqq3zn6Pvq4rHOEOGSaUMgOOjG2Y9ZbIGzpfkJk9ZYTHpDqgDfeBDcnLaw==
11914+
webpack@^5.101.0:
11915+
version "5.101.0"
11916+
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.101.0.tgz#4b81407ffad9857f81ff03f872e3369b9198cc9d"
11917+
integrity sha512-B4t+nJqytPeuZlHuIKTbalhljIFXeNRqrUGAQgTGlfOl2lXXKXw+yZu6bicycP+PUlM44CxBjCFD6aciKFT3LQ==
1191811918
dependencies:
1191911919
"@types/eslint-scope" "^3.7.7"
1192011920
"@types/estree" "^1.0.8"

0 commit comments

Comments
 (0)