@@ -2,7 +2,9 @@ import { type stringifyChunked } from "@discoveryjs/json-ext";
22import { type Help , type ParseOptions } from "commander" ;
33import {
44 type Compiler ,
5+ type Configuration ,
56 type MultiCompiler ,
7+ type MultiConfiguration ,
68 type MultiStatsOptions ,
79 type StatsOptions ,
810 type WebpackError ,
@@ -50,7 +52,6 @@ import {
5052 type WebpackCLIOptions ,
5153 type WebpackCallback ,
5254 type WebpackCompiler ,
53- type WebpackConfiguration ,
5455 type WebpackDevServerOptions ,
5556 type WebpackRunOptions ,
5657} from "./types.js" ;
@@ -112,6 +113,12 @@ class WebpackCLI implements IWebpackCLI {
112113 } ) ;
113114 }
114115
116+ isMultipleConfiguration (
117+ config : Configuration | MultiConfiguration ,
118+ ) : config is MultiConfiguration {
119+ return Array . isArray ( config ) ;
120+ }
121+
115122 isMultipleCompiler ( compiler : WebpackCompiler ) : compiler is MultiCompiler {
116123 return ( compiler as MultiCompiler ) . compilers as unknown as boolean ;
117124 }
@@ -1791,7 +1798,7 @@ class WebpackCLI implements IWebpackCLI {
17911798 const loadConfigByPath = async (
17921799 configPath : string ,
17931800 argv : Argv = { } ,
1794- ) : Promise < { options : WebpackConfiguration | WebpackConfiguration [ ] ; path : string } > => {
1801+ ) : Promise < { options : Configuration | Configuration [ ] ; path : string } > => {
17951802 const ext = path . extname ( configPath ) . toLowerCase ( ) ;
17961803 let interpreted = Object . keys ( interpret . jsVariants ) . find ( ( variant ) => variant === ext ) ;
17971804 // Fallback `.cts` to `.ts`
@@ -1824,7 +1831,7 @@ class WebpackCLI implements IWebpackCLI {
18241831
18251832 let options : LoadableWebpackConfiguration | LoadableWebpackConfiguration [ ] ;
18261833
1827- type LoadConfigOption = PotentialPromise < WebpackConfiguration > ;
1834+ type LoadConfigOption = PotentialPromise < Configuration > ;
18281835
18291836 let moduleType : "unknown" | "commonjs" | "esm" = "unknown" ;
18301837
@@ -1868,8 +1875,8 @@ class WebpackCLI implements IWebpackCLI {
18681875 await Promise . all (
18691876 optionsArray . map ( async ( _ , i ) => {
18701877 if (
1871- this . isPromise < WebpackConfiguration | CallableWebpackConfiguration > (
1872- optionsArray [ i ] as Promise < WebpackConfiguration | CallableWebpackConfiguration > ,
1878+ this . isPromise < Configuration | CallableWebpackConfiguration > (
1879+ optionsArray [ i ] as Promise < Configuration | CallableWebpackConfiguration > ,
18731880 )
18741881 ) {
18751882 optionsArray [ i ] = await optionsArray [ i ] ;
@@ -1908,7 +1915,7 @@ class WebpackCLI implements IWebpackCLI {
19081915 }
19091916
19101917 return {
1911- options : options as WebpackConfiguration | WebpackConfiguration [ ] ,
1918+ options : options as Configuration | Configuration [ ] ,
19121919 path : configPath ,
19131920 } ;
19141921 } ;
@@ -1933,11 +1940,11 @@ class WebpackCLI implements IWebpackCLI {
19331940 for ( const loadedConfig of loadedConfigs ) {
19341941 if ( Array . isArray ( loadedConfig . options ) ) {
19351942 for ( const item of loadedConfig . options ) {
1936- ( config . options as WebpackConfiguration [ ] ) . push ( item ) ;
1943+ ( config . options as Configuration [ ] ) . push ( item ) ;
19371944 config . path . set ( options , [ loadedConfig . path ] ) ;
19381945 }
19391946 } else {
1940- ( config . options as WebpackConfiguration [ ] ) . push ( loadedConfig . options ) ;
1947+ ( config . options as Configuration [ ] ) . push ( loadedConfig . options ) ;
19411948 config . path . set ( loadedConfig . options , [ loadedConfig . path ] ) ;
19421949 }
19431950 }
@@ -1976,7 +1983,7 @@ class WebpackCLI implements IWebpackCLI {
19761983
19771984 config . options = loadedConfig . options ;
19781985
1979- if ( Array . isArray ( config . options ) ) {
1986+ if ( this . isMultipleConfiguration ( config . options ) ) {
19801987 for ( const item of config . options ) {
19811988 config . path . set ( item , [ loadedConfig . path ] ) ;
19821989 }
@@ -1992,7 +1999,7 @@ class WebpackCLI implements IWebpackCLI {
19921999 config . options = options . configName . map ( ( configName ) => {
19932000 let found ;
19942001
1995- if ( Array . isArray ( config . options ) ) {
2002+ if ( this . isMultipleConfiguration ( config . options ) ) {
19962003 found = config . options . find ( ( options ) => options . name === configName ) ;
19972004 } else {
19982005 found = config . options . name === configName ? config . options : undefined ;
@@ -2003,7 +2010,7 @@ class WebpackCLI implements IWebpackCLI {
20032010 }
20042011
20052012 return found ;
2006- } ) as WebpackConfiguration [ ] ;
2013+ } ) as Configuration [ ] ;
20072014
20082015 if ( notFoundConfigNames . length > 0 ) {
20092016 this . logger . error (
@@ -2016,10 +2023,10 @@ class WebpackCLI implements IWebpackCLI {
20162023 }
20172024
20182025 const resolveExtends = async (
2019- config : WebpackConfiguration ,
2026+ config : Configuration ,
20202027 configPaths : WebpackCLIConfig [ "path" ] ,
20212028 extendsPaths : string [ ] ,
2022- ) : Promise < WebpackConfiguration > => {
2029+ ) : Promise < Configuration > => {
20232030 delete config . extends ;
20242031
20252032 const loadedConfigs = await Promise . all (
@@ -2044,10 +2051,7 @@ class WebpackCLI implements IWebpackCLI {
20442051 }
20452052 }
20462053
2047- config = merge (
2048- ...( loadedOptions as [ WebpackConfiguration , ...WebpackConfiguration [ ] ] ) ,
2049- config ,
2050- ) ;
2054+ config = merge ( ...( loadedOptions as [ Configuration , ...Configuration [ ] ] ) , config ) ;
20512055
20522056 if ( prevPaths ) {
20532057 configPaths . set ( config , [ ...prevPaths , ...loadedPaths ] ) ;
@@ -2067,7 +2071,7 @@ class WebpackCLI implements IWebpackCLI {
20672071 if ( options . extends && options . extends . length > 0 ) {
20682072 const extendsPaths = options . extends ;
20692073
2070- if ( Array . isArray ( config . options ) ) {
2074+ if ( this . isMultipleConfiguration ( config . options ) ) {
20712075 config . options = await Promise . all (
20722076 config . options . map ( ( options ) => resolveExtends ( options , config . path , extendsPaths ) ) ,
20732077 ) ;
@@ -2077,7 +2081,10 @@ class WebpackCLI implements IWebpackCLI {
20772081 }
20782082 }
20792083 // if no extends option is passed, check if the config file has extends
2080- else if ( Array . isArray ( config . options ) && config . options . some ( ( options ) => options . extends ) ) {
2084+ else if (
2085+ this . isMultipleConfiguration ( config . options ) &&
2086+ config . options . some ( ( options ) => options . extends )
2087+ ) {
20812088 config . options = await Promise . all (
20822089 config . options . map ( ( options ) => {
20832090 if ( options . extends ) {
@@ -2090,7 +2097,7 @@ class WebpackCLI implements IWebpackCLI {
20902097 return options ;
20912098 } ) ,
20922099 ) ;
2093- } else if ( ! Array . isArray ( config . options ) && config . options . extends ) {
2100+ } else if ( ! this . isMultipleConfiguration ( config . options ) && config . options . extends ) {
20942101 config . options = await resolveExtends (
20952102 config . options ,
20962103 config . path ,
@@ -2106,7 +2113,7 @@ class WebpackCLI implements IWebpackCLI {
21062113 // we can only merge when there are multiple configurations
21072114 // either by passing multiple configs by flags or passing a
21082115 // single config exporting an array
2109- if ( ! Array . isArray ( config . options ) || config . options . length <= 1 ) {
2116+ if ( ! this . isMultipleConfiguration ( config . options ) || config . options . length <= 1 ) {
21102117 this . logger . error ( "At least two configurations are required for merge." ) ;
21112118 process . exit ( 2 ) ;
21122119 }
@@ -2159,7 +2166,7 @@ class WebpackCLI implements IWebpackCLI {
21592166 "./plugins/cli-plugin" ,
21602167 ) ;
21612168
2162- const internalBuildConfig = ( item : WebpackConfiguration ) => {
2169+ const internalBuildConfig = ( item : Configuration ) => {
21632170 const originalWatchValue = item . watch ;
21642171
21652172 // Apply options
@@ -2246,9 +2253,7 @@ class WebpackCLI implements IWebpackCLI {
22462253 }
22472254 }
22482255
2249- const isFileSystemCacheOptions = (
2250- config : WebpackConfiguration ,
2251- ) : config is FileSystemCacheOptions =>
2256+ const isFileSystemCacheOptions = ( config : Configuration ) : config is FileSystemCacheOptions =>
22522257 Boolean ( config . cache ) && ( config as FileSystemCacheOptions ) . cache . type === "filesystem" ;
22532258
22542259 // Setup default cache options
@@ -2325,13 +2330,13 @@ class WebpackCLI implements IWebpackCLI {
23252330 helpfulOutput : ! options . json ,
23262331 progress : options . progress ,
23272332 analyze : options . analyze ,
2328- isMultiCompiler : Array . isArray ( config . options ) ,
2333+ isMultiCompiler : this . isMultipleConfiguration ( config . options ) ,
23292334 } ) ,
23302335 ) ;
23312336 }
23322337 } ;
23332338
2334- if ( Array . isArray ( config . options ) ) {
2339+ if ( this . isMultipleConfiguration ( config . options ) ) {
23352340 for ( const item of config . options ) {
23362341 internalBuildConfig ( item ) ;
23372342 }
@@ -2364,19 +2369,16 @@ class WebpackCLI implements IWebpackCLI {
23642369 let compiler : WebpackCompiler ;
23652370
23662371 try {
2367- compiler = this . webpack (
2368- config . options ,
2369- callback
2370- ? ( error , stats ) => {
2371- if ( error && this . isValidationError ( error ) ) {
2372- this . logger . error ( error . message ) ;
2373- process . exit ( 2 ) ;
2374- }
2375-
2376- callback ( error as Error | undefined , stats ) ;
2372+ compiler = callback
2373+ ? this . webpack ( config . options , ( error , stats ) => {
2374+ if ( error && this . isValidationError ( error ) ) {
2375+ this . logger . error ( error . message ) ;
2376+ process . exit ( 2 ) ;
23772377 }
2378- : callback ,
2379- ) ! ;
2378+
2379+ callback ( error as Error | null , stats ) ;
2380+ } ) !
2381+ : this . webpack ( config . options ) ;
23802382 } catch ( error ) {
23812383 if ( this . isValidationError ( error ) ) {
23822384 this . logger . error ( error . message ) ;
0 commit comments