@@ -3,6 +3,7 @@ import { type Help, type ParseOptions } from "commander";
33import {
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 ( / ^ e r r o r : / , "" ) . trim ( ) ) } ` ) ,
106+ writeErr : ( str ) => {
107+ this . logger . error ( str ) ;
108+ } ,
109+ outputError : ( str , write ) => {
110+ write ( `Error: ${ this . capitalizeFirstLetter ( str . replace ( / ^ e r r o r : / , "" ) . 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 {
0 commit comments