Skip to content

Commit c2ec3cd

Browse files
committed
fix: configtest for v4
1 parent b94c067 commit c2ec3cd

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

packages/configtest/src/index.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import webpack from 'webpack';
1+
import { validate, version, ValidationError, WebpackOptionsValidationError } from 'webpack';
2+
3+
const isWebpack5: boolean = version.startsWith('5');
24

35
class 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

test/configtest/configtest.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('basic info usage', () => {
1515
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './error.config.js'], false);
1616

1717
expect(exitCode).toBe(2);
18-
expect(stderr).toContain('ValidationError: Invalid configuration object.');
18+
expect(stderr).toContain('Invalid configuration object.');
1919
expect(stderr).toContain('configuration.mode should be one of these:');
2020
expect(stdout).toBeFalsy();
2121
});
@@ -32,7 +32,7 @@ describe('basic info usage', () => {
3232
const { exitCode, stderr, stdout } = run(__dirname, ['t', './error.config.js'], false);
3333

3434
expect(exitCode).toBe(2);
35-
expect(stderr).toContain('ValidationError: Invalid configuration object.');
35+
expect(stderr).toContain('Invalid configuration object.');
3636
expect(stderr).toContain('configuration.mode should be one of these:');
3737
expect(stdout).toBeFalsy();
3838
});

0 commit comments

Comments
 (0)