@@ -4,7 +4,6 @@ import type {
4
4
WebpackCLIBuiltInOption ,
5
5
WebpackCLIBuiltInFlag ,
6
6
WebpackCLIColors ,
7
- WebpackCLIStats ,
8
7
WebpackCLIConfig ,
9
8
WebpackCLIExternalCommandInfo ,
10
9
WebpackCLIOptions ,
@@ -18,11 +17,11 @@ import type {
18
17
WebpackConfiguration ,
19
18
Argv ,
20
19
BasicPrimitive ,
21
- CallableOption ,
20
+ CallableWebpackConfiguration ,
22
21
Callback ,
23
22
CLIPluginOptions ,
24
23
CommandAction ,
25
- ConfigOptions ,
24
+ LoadableWebpackConfiguration ,
26
25
DynamicImport ,
27
26
FileSystemCacheOptions ,
28
27
FlagConfig ,
@@ -49,12 +48,14 @@ import {
49
48
type MultiCompiler ,
50
49
type WebpackError ,
51
50
type StatsOptions ,
52
- type WebpackOptionsNormalized ,
51
+ type Stats ,
52
+ type MultiStats ,
53
53
} from "webpack" ;
54
54
import { type stringifyChunked } from "@discoveryjs/json-ext" ;
55
55
import { type Help , type ParseOptions } from "commander" ;
56
56
57
57
import { type CLIPlugin as CLIPluginClass } from "./plugins/cli-plugin" ;
58
+ import * as console from "node:console" ;
58
59
const fs = require ( "fs" ) ;
59
60
const { Readable } = require ( "stream" ) ;
60
61
const path = require ( "path" ) ;
@@ -106,9 +107,11 @@ class WebpackCLI implements IWebpackCLI {
106
107
isMultipleCompiler ( compiler : WebpackCompiler ) : compiler is MultiCompiler {
107
108
return ( compiler as MultiCompiler ) . compilers as unknown as boolean ;
108
109
}
110
+
109
111
isPromise < T > ( value : Promise < T > ) : value is Promise < T > {
110
112
return typeof ( value as unknown as Promise < T > ) . then === "function" ;
111
113
}
114
+
112
115
isFunction ( value : unknown ) : value is CallableFunction {
113
116
return typeof value === "function" ;
114
117
}
@@ -553,7 +556,7 @@ class WebpackCLI implements IWebpackCLI {
553
556
if ( Array . isArray ( commandOptions . alias ) ) {
554
557
command . aliases ( commandOptions . alias ) ;
555
558
} else {
556
- command . alias ( commandOptions . alias as string ) ;
559
+ command . alias ( commandOptions . alias ) ;
557
560
}
558
561
559
562
if ( commandOptions . pkg ) {
@@ -1069,7 +1072,7 @@ class WebpackCLI implements IWebpackCLI {
1069
1072
return {
1070
1073
...meta ,
1071
1074
name,
1072
- description : meta . description as string ,
1075
+ description : meta . description ,
1073
1076
group : "core" ,
1074
1077
helpLevel : minimumHelpFlags . includes ( name ) ? "minimum" : "verbose" ,
1075
1078
} ;
@@ -1792,7 +1795,10 @@ class WebpackCLI implements IWebpackCLI {
1792
1795
typeof options . disableInterpret !== "undefined" && options . disableInterpret ;
1793
1796
1794
1797
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 } > => {
1796
1802
const ext = path . extname ( configPath ) . toLowerCase ( ) ;
1797
1803
let interpreted = Object . keys ( interpret . jsVariants ) . find ( ( variant ) => variant === ext ) ;
1798
1804
// Fallback `.cts` to `.ts`
@@ -1823,7 +1829,7 @@ class WebpackCLI implements IWebpackCLI {
1823
1829
}
1824
1830
}
1825
1831
1826
- let options : ConfigOptions | ConfigOptions [ ] ;
1832
+ let options : LoadableWebpackConfiguration | LoadableWebpackConfiguration [ ] ;
1827
1833
1828
1834
type LoadConfigOption = PotentialPromise < WebpackConfiguration > ;
1829
1835
@@ -1866,26 +1872,30 @@ class WebpackCLI implements IWebpackCLI {
1866
1872
1867
1873
if ( Array . isArray ( options ) ) {
1868
1874
// reassign the value to assert type
1869
- const optionsArray : ConfigOptions [ ] = options ;
1875
+ const optionsArray : LoadableWebpackConfiguration [ ] = options ;
1870
1876
await Promise . all (
1871
1877
optionsArray . map ( async ( _ , i ) => {
1872
1878
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 > ,
1875
1881
)
1876
1882
) {
1877
1883
optionsArray [ i ] = await optionsArray [ i ] ;
1878
1884
}
1879
1885
// `Promise` may return `Function`
1880
1886
if ( this . isFunction ( optionsArray [ i ] ) ) {
1881
1887
// 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 ) ;
1883
1889
}
1884
1890
} ) ,
1885
1891
) ;
1886
1892
options = optionsArray ;
1887
1893
} else {
1888
- if ( this . isPromise < ConfigOptions > ( options as Promise < ConfigOptions > ) ) {
1894
+ if (
1895
+ this . isPromise < LoadableWebpackConfiguration > (
1896
+ options as Promise < LoadableWebpackConfiguration > ,
1897
+ )
1898
+ ) {
1889
1899
options = await options ;
1890
1900
}
1891
1901
@@ -1905,11 +1915,14 @@ class WebpackCLI implements IWebpackCLI {
1905
1915
process . exit ( 2 ) ;
1906
1916
}
1907
1917
1908
- return { options, path : configPath } ;
1918
+ return {
1919
+ options : options as WebpackConfiguration | WebpackConfiguration [ ] ,
1920
+ path : configPath ,
1921
+ } ;
1909
1922
} ;
1910
1923
1911
1924
const config : WebpackCLIConfig = {
1912
- options : { } as WebpackConfiguration ,
1925
+ options : { } ,
1913
1926
path : new WeakMap ( ) ,
1914
1927
} ;
1915
1928
@@ -1923,27 +1936,25 @@ class WebpackCLI implements IWebpackCLI {
1923
1936
config . options = [ ] ;
1924
1937
1925
1938
loadedConfigs . forEach ( ( loadedConfig ) => {
1926
- const isArray = Array . isArray ( loadedConfig . options ) ;
1927
-
1928
1939
// 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 ) {
1930
1941
config . options = loadedConfig . options as WebpackConfiguration ;
1931
1942
} else {
1932
1943
if ( ! Array . isArray ( config . options ) ) {
1933
1944
config . options = [ config . options ] ;
1934
1945
}
1935
1946
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 ) ;
1939
1950
}
1940
1951
} else {
1941
1952
config . options . push ( loadedConfig . options as WebpackConfiguration ) ;
1942
1953
}
1943
1954
}
1944
1955
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 ) {
1947
1958
config . path . set ( options , [ loadedConfig . path ] ) ;
1948
1959
}
1949
1960
} else {
@@ -1985,7 +1996,7 @@ class WebpackCLI implements IWebpackCLI {
1985
1996
if ( foundDefaultConfigFile ) {
1986
1997
const loadedConfig = await loadConfigByPath ( foundDefaultConfigFile , options . argv ) ;
1987
1998
1988
- config . options = loadedConfig . options as WebpackConfiguration [ ] ;
1999
+ config . options = loadedConfig . options ;
1989
2000
1990
2001
if ( Array . isArray ( config . options ) ) {
1991
2002
for ( const item of config . options ) {
@@ -2000,7 +2011,7 @@ class WebpackCLI implements IWebpackCLI {
2000
2011
if ( options . configName ) {
2001
2012
const notFoundConfigNames : string [ ] = [ ] ;
2002
2013
2003
- config . options = options . configName . map ( ( configName : string ) => {
2014
+ config . options = options . configName . map ( ( configName ) => {
2004
2015
let found ;
2005
2016
2006
2017
if ( Array . isArray ( config . options ) ) {
@@ -2280,11 +2291,7 @@ class WebpackCLI implements IWebpackCLI {
2280
2291
2281
2292
if ( Array . isArray ( configPath ) ) {
2282
2293
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 ) ;
2288
2295
}
2289
2296
} else {
2290
2297
item . cache . buildDependencies . defaultConfig . push ( configPath ) ;
@@ -2320,8 +2327,8 @@ class WebpackCLI implements IWebpackCLI {
2320
2327
colors = Boolean ( this . isColorSupportChanged ) ;
2321
2328
}
2322
2329
// 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 ;
2325
2332
}
2326
2333
// Default
2327
2334
else {
@@ -2363,7 +2370,7 @@ class WebpackCLI implements IWebpackCLI {
2363
2370
2364
2371
async createCompiler (
2365
2372
options : Partial < WebpackDevServerOptions > ,
2366
- callback ?: Callback < [ Error | undefined , WebpackCLIStats | undefined ] > ,
2373
+ callback ?: Callback < [ Error | undefined , Stats | MultiStats | undefined ] > ,
2367
2374
) : Promise < WebpackCompiler > {
2368
2375
if ( typeof options . configNodeEnv === "string" ) {
2369
2376
process . env . NODE_ENV = options . configNodeEnv ;
@@ -2407,7 +2414,7 @@ class WebpackCLI implements IWebpackCLI {
2407
2414
needWatchStdin ( compiler : Compiler | MultiCompiler ) : boolean {
2408
2415
if ( this . isMultipleCompiler ( compiler ) ) {
2409
2416
return Boolean (
2410
- ( compiler as MultiCompiler ) . compilers . some (
2417
+ compiler . compilers . some (
2411
2418
( compiler : Compiler ) =>
2412
2419
compiler . options . watchOptions && compiler . options . watchOptions . stdin ,
2413
2420
) ,
@@ -2428,7 +2435,7 @@ class WebpackCLI implements IWebpackCLI {
2428
2435
createStringifyChunked = jsonExt . stringifyChunked ;
2429
2436
}
2430
2437
2431
- const callback = ( error : Error | undefined , stats : WebpackCLIStats | undefined ) : void => {
2438
+ const callback = ( error : Error | undefined , stats : Stats | MultiStats | undefined ) : void => {
2432
2439
if ( error ) {
2433
2440
this . logger . error ( error ) ;
2434
2441
process . exit ( 2 ) ;
@@ -2479,10 +2486,7 @@ class WebpackCLI implements IWebpackCLI {
2479
2486
} ) ;
2480
2487
}
2481
2488
} 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 ) ;
2486
2490
2487
2491
// Avoid extra empty line when `stats: 'none'`
2488
2492
if ( printedStats ) {
0 commit comments