@@ -4,7 +4,6 @@ import type {
44 WebpackCLIBuiltInOption ,
55 WebpackCLIBuiltInFlag ,
66 WebpackCLIColors ,
7- WebpackCLIStats ,
87 WebpackCLIConfig ,
98 WebpackCLIExternalCommandInfo ,
109 WebpackCLIOptions ,
@@ -18,11 +17,11 @@ import type {
1817 WebpackConfiguration ,
1918 Argv ,
2019 BasicPrimitive ,
21- CallableOption ,
20+ CallableWebpackConfiguration ,
2221 Callback ,
2322 CLIPluginOptions ,
2423 CommandAction ,
25- ConfigOptions ,
24+ LoadableWebpackConfiguration ,
2625 DynamicImport ,
2726 FileSystemCacheOptions ,
2827 FlagConfig ,
@@ -49,12 +48,14 @@ import {
4948 type MultiCompiler ,
5049 type WebpackError ,
5150 type StatsOptions ,
52- type WebpackOptionsNormalized ,
51+ type Stats ,
52+ type MultiStats ,
5353} from "webpack" ;
5454import { type stringifyChunked } from "@discoveryjs/json-ext" ;
5555import { type Help , type ParseOptions } from "commander" ;
5656
5757import { type CLIPlugin as CLIPluginClass } from "./plugins/cli-plugin" ;
58+ import * as console from "node:console" ;
5859const fs = require ( "fs" ) ;
5960const { Readable } = require ( "stream" ) ;
6061const path = require ( "path" ) ;
@@ -106,9 +107,11 @@ class WebpackCLI implements IWebpackCLI {
106107 isMultipleCompiler ( compiler : WebpackCompiler ) : compiler is MultiCompiler {
107108 return ( compiler as MultiCompiler ) . compilers as unknown as boolean ;
108109 }
110+
109111 isPromise < T > ( value : Promise < T > ) : value is Promise < T > {
110112 return typeof ( value as unknown as Promise < T > ) . then === "function" ;
111113 }
114+
112115 isFunction ( value : unknown ) : value is CallableFunction {
113116 return typeof value === "function" ;
114117 }
@@ -553,7 +556,7 @@ class WebpackCLI implements IWebpackCLI {
553556 if ( Array . isArray ( commandOptions . alias ) ) {
554557 command . aliases ( commandOptions . alias ) ;
555558 } else {
556- command . alias ( commandOptions . alias as string ) ;
559+ command . alias ( commandOptions . alias ) ;
557560 }
558561
559562 if ( commandOptions . pkg ) {
@@ -1069,7 +1072,7 @@ class WebpackCLI implements IWebpackCLI {
10691072 return {
10701073 ...meta ,
10711074 name,
1072- description : meta . description as string ,
1075+ description : meta . description ,
10731076 group : "core" ,
10741077 helpLevel : minimumHelpFlags . includes ( name ) ? "minimum" : "verbose" ,
10751078 } ;
@@ -1792,7 +1795,10 @@ class WebpackCLI implements IWebpackCLI {
17921795 typeof options . disableInterpret !== "undefined" && options . disableInterpret ;
17931796
17941797 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 } > => {
17961802 const ext = path . extname ( configPath ) . toLowerCase ( ) ;
17971803 let interpreted = Object . keys ( interpret . jsVariants ) . find ( ( variant ) => variant === ext ) ;
17981804 // Fallback `.cts` to `.ts`
@@ -1823,7 +1829,7 @@ class WebpackCLI implements IWebpackCLI {
18231829 }
18241830 }
18251831
1826- let options : ConfigOptions | ConfigOptions [ ] ;
1832+ let options : LoadableWebpackConfiguration | LoadableWebpackConfiguration [ ] ;
18271833
18281834 type LoadConfigOption = PotentialPromise < WebpackConfiguration > ;
18291835
@@ -1866,26 +1872,30 @@ class WebpackCLI implements IWebpackCLI {
18661872
18671873 if ( Array . isArray ( options ) ) {
18681874 // reassign the value to assert type
1869- const optionsArray : ConfigOptions [ ] = options ;
1875+ const optionsArray : LoadableWebpackConfiguration [ ] = options ;
18701876 await Promise . all (
18711877 optionsArray . map ( async ( _ , i ) => {
18721878 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 > ,
18751881 )
18761882 ) {
18771883 optionsArray [ i ] = await optionsArray [ i ] ;
18781884 }
18791885 // `Promise` may return `Function`
18801886 if ( this . isFunction ( optionsArray [ i ] ) ) {
18811887 // 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 ) ;
18831889 }
18841890 } ) ,
18851891 ) ;
18861892 options = optionsArray ;
18871893 } else {
1888- if ( this . isPromise < ConfigOptions > ( options as Promise < ConfigOptions > ) ) {
1894+ if (
1895+ this . isPromise < LoadableWebpackConfiguration > (
1896+ options as Promise < LoadableWebpackConfiguration > ,
1897+ )
1898+ ) {
18891899 options = await options ;
18901900 }
18911901
@@ -1905,11 +1915,14 @@ class WebpackCLI implements IWebpackCLI {
19051915 process . exit ( 2 ) ;
19061916 }
19071917
1908- return { options, path : configPath } ;
1918+ return {
1919+ options : options as WebpackConfiguration | WebpackConfiguration [ ] ,
1920+ path : configPath ,
1921+ } ;
19091922 } ;
19101923
19111924 const config : WebpackCLIConfig = {
1912- options : { } as WebpackConfiguration ,
1925+ options : { } ,
19131926 path : new WeakMap ( ) ,
19141927 } ;
19151928
@@ -1923,27 +1936,25 @@ class WebpackCLI implements IWebpackCLI {
19231936 config . options = [ ] ;
19241937
19251938 loadedConfigs . forEach ( ( loadedConfig ) => {
1926- const isArray = Array . isArray ( loadedConfig . options ) ;
1927-
19281939 // 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 ) {
19301941 config . options = loadedConfig . options as WebpackConfiguration ;
19311942 } else {
19321943 if ( ! Array . isArray ( config . options ) ) {
19331944 config . options = [ config . options ] ;
19341945 }
19351946
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 ) ;
19391950 }
19401951 } else {
19411952 config . options . push ( loadedConfig . options as WebpackConfiguration ) ;
19421953 }
19431954 }
19441955
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 ) {
19471958 config . path . set ( options , [ loadedConfig . path ] ) ;
19481959 }
19491960 } else {
@@ -1985,7 +1996,7 @@ class WebpackCLI implements IWebpackCLI {
19851996 if ( foundDefaultConfigFile ) {
19861997 const loadedConfig = await loadConfigByPath ( foundDefaultConfigFile , options . argv ) ;
19871998
1988- config . options = loadedConfig . options as WebpackConfiguration [ ] ;
1999+ config . options = loadedConfig . options ;
19892000
19902001 if ( Array . isArray ( config . options ) ) {
19912002 for ( const item of config . options ) {
@@ -2000,7 +2011,7 @@ class WebpackCLI implements IWebpackCLI {
20002011 if ( options . configName ) {
20012012 const notFoundConfigNames : string [ ] = [ ] ;
20022013
2003- config . options = options . configName . map ( ( configName : string ) => {
2014+ config . options = options . configName . map ( ( configName ) => {
20042015 let found ;
20052016
20062017 if ( Array . isArray ( config . options ) ) {
@@ -2280,11 +2291,7 @@ class WebpackCLI implements IWebpackCLI {
22802291
22812292 if ( Array . isArray ( configPath ) ) {
22822293 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 ) ;
22882295 }
22892296 } else {
22902297 item . cache . buildDependencies . defaultConfig . push ( configPath ) ;
@@ -2320,8 +2327,8 @@ class WebpackCLI implements IWebpackCLI {
23202327 colors = Boolean ( this . isColorSupportChanged ) ;
23212328 }
23222329 // 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 ;
23252332 }
23262333 // Default
23272334 else {
@@ -2363,7 +2370,7 @@ class WebpackCLI implements IWebpackCLI {
23632370
23642371 async createCompiler (
23652372 options : Partial < WebpackDevServerOptions > ,
2366- callback ?: Callback < [ Error | undefined , WebpackCLIStats | undefined ] > ,
2373+ callback ?: Callback < [ Error | undefined , Stats | MultiStats | undefined ] > ,
23672374 ) : Promise < WebpackCompiler > {
23682375 if ( typeof options . configNodeEnv === "string" ) {
23692376 process . env . NODE_ENV = options . configNodeEnv ;
@@ -2407,7 +2414,7 @@ class WebpackCLI implements IWebpackCLI {
24072414 needWatchStdin ( compiler : Compiler | MultiCompiler ) : boolean {
24082415 if ( this . isMultipleCompiler ( compiler ) ) {
24092416 return Boolean (
2410- ( compiler as MultiCompiler ) . compilers . some (
2417+ compiler . compilers . some (
24112418 ( compiler : Compiler ) =>
24122419 compiler . options . watchOptions && compiler . options . watchOptions . stdin ,
24132420 ) ,
@@ -2428,7 +2435,7 @@ class WebpackCLI implements IWebpackCLI {
24282435 createStringifyChunked = jsonExt . stringifyChunked ;
24292436 }
24302437
2431- const callback = ( error : Error | undefined , stats : WebpackCLIStats | undefined ) : void => {
2438+ const callback = ( error : Error | undefined , stats : Stats | MultiStats | undefined ) : void => {
24322439 if ( error ) {
24332440 this . logger . error ( error ) ;
24342441 process . exit ( 2 ) ;
@@ -2479,10 +2486,7 @@ class WebpackCLI implements IWebpackCLI {
24792486 } ) ;
24802487 }
24812488 } 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 ) ;
24862490
24872491 // Avoid extra empty line when `stats: 'none'`
24882492 if ( printedStats ) {
0 commit comments