1- import webpack from 'webpack' ;
1+ import { validate , version , ValidationError , WebpackOptionsValidationError } from 'webpack' ;
2+
3+ const isWebpack5 : boolean = version . startsWith ( '5' ) ;
24
35class ConfigTestCommand {
46 async apply ( cli ) : Promise < void > {
@@ -13,22 +15,29 @@ class ConfigTestCommand {
1315 pkg : '@webpack-cli/configtest' ,
1416 } ,
1517 [ ] ,
16- async ( configPath : string ) => {
18+ async ( configPath : string ) : Promise < void > => {
1719 const { options } = await cli . resolveConfig ( { config : [ configPath ] } ) ;
1820
19- const isValidationError = ( error ) => {
21+ const isValidationError = ( error ) : boolean => {
2022 // https://github.com/webpack/webpack/blob/master/lib/index.js#L267
2123 // https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
22- const ValidationError : any = webpack . ValidationError || webpack . WebpackOptionsValidationError ;
24+ //eslint-disable-next-line @typescript-eslint/no-explicit-any
25+ const webpackValidationError : any = ValidationError || WebpackOptionsValidationError ;
2326
24- return error instanceof ValidationError ;
27+ return error instanceof webpackValidationError ;
2528 } ;
2629
2730 //eslint-disable-next-line @typescript-eslint/no-explicit-any
28- const error : any = webpack . validate ( options ) ;
29-
30- if ( error ) {
31- logger . error ( isValidationError ( error ) ? error . message : error ) ;
31+ const error : any = validate ( options ) ;
32+
33+ if ( error && error . length ) {
34+ if ( isWebpack5 ) {
35+ logger . error ( isValidationError ( error ) ? error . message : error ) ;
36+ } else {
37+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
38+ // @ts -ignore
39+ logger . error ( new WebpackOptionsValidationError ( error ) ) ;
40+ }
3241 process . exit ( 2 ) ;
3342 }
3443
0 commit comments