@@ -3,6 +3,7 @@ import { type Help, type ParseOptions } from "commander";
3
3
import {
4
4
type Compiler ,
5
5
type MultiCompiler ,
6
+ type MultiStatsOptions ,
6
7
type StatsOptions ,
7
8
type WebpackError ,
8
9
default as webpack ,
@@ -26,7 +27,6 @@ import {
26
27
type JsonExt ,
27
28
type LoadableWebpackConfiguration ,
28
29
type ModuleName ,
29
- type MultiStatsOptions ,
30
30
type PackageInstallOptions ,
31
31
type PackageManager ,
32
32
type Path ,
@@ -103,9 +103,12 @@ class WebpackCLI implements IWebpackCLI {
103
103
this . program = program ;
104
104
this . program . name ( "webpack" ) ;
105
105
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
+ } ,
109
112
} ) ;
110
113
}
111
114
@@ -134,6 +137,20 @@ class WebpackCLI implements IWebpackCLI {
134
137
}
135
138
136
139
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
137
154
const { createColors, isColorSupported } = require ( "colorette" ) ;
138
155
139
156
const shouldUseColor = useColor || isColorSupported ;
@@ -1323,16 +1340,14 @@ class WebpackCLI implements IWebpackCLI {
1323
1340
} ) ;
1324
1341
1325
1342
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 ) {
1328
1344
const { color } = this . opts ( ) ;
1329
1345
1330
1346
cli . isColorSupportChanged = color ;
1331
1347
cli . colors = cli . createColors ( color ) ;
1332
1348
} ) ;
1333
1349
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 ) {
1336
1351
const { color } = this . opts ( ) ;
1337
1352
1338
1353
cli . isColorSupportChanged = color ;
@@ -1830,8 +1845,7 @@ class WebpackCLI implements IWebpackCLI {
1830
1845
false ,
1831
1846
moduleType ,
1832
1847
) ;
1833
- // @ts -expect-error error type assertion
1834
- } catch ( error : Error ) {
1848
+ } catch ( error ) {
1835
1849
this . logger . error ( `Failed to load '${ configPath } ' config` ) ;
1836
1850
1837
1851
if ( this . isValidationError ( error ) ) {
@@ -2328,8 +2342,10 @@ class WebpackCLI implements IWebpackCLI {
2328
2342
return config ;
2329
2343
}
2330
2344
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
+ ) ;
2333
2349
}
2334
2350
2335
2351
async createCompiler (
@@ -2360,9 +2376,8 @@ class WebpackCLI implements IWebpackCLI {
2360
2376
callback ( error as Error | undefined , stats ) ;
2361
2377
}
2362
2378
: callback ,
2363
- ) ;
2364
- // @ts -expect-error error type assertion
2365
- } catch ( error : Error ) {
2379
+ ) ! ;
2380
+ } catch ( error ) {
2366
2381
if ( this . isValidationError ( error ) ) {
2367
2382
this . logger . error ( error . message ) ;
2368
2383
} else {
0 commit comments