Skip to content

Commit 71e89b4

Browse files
authored
fix: exit process in case of schema errors
1 parent 02b6d21 commit 71e89b4

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

packages/webpack-cli/lib/utils/Compiler.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,17 @@ class Compiler {
129129
this.compiler = await webpack(options);
130130
this.compilerOptions = options;
131131
} catch (err) {
132-
process.stdout.write('\n');
133-
logger.error(`${err.name}: ${err.message}`);
134-
process.stdout.write('\n');
135-
return;
132+
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267
133+
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
134+
const ValidationError = webpack.ValidationError ? webpack.ValidationError : webpack.WebpackOptionsValidationError;
135+
// In case of schema errors print and exit process
136+
// For webpack@4 and webpack@5
137+
if (err instanceof ValidationError) {
138+
logger.error(`\n${err.message}`);
139+
} else {
140+
logger.error(`\n${err}`);
141+
}
142+
process.exit(1);
136143
}
137144
}
138145

@@ -153,7 +160,6 @@ class Compiler {
153160
const interactive = require('./interactive');
154161
return interactive(options, outputOptions);
155162
}
156-
157163
if (this.compiler.compilers) {
158164
this.compiler.compilers.forEach((comp, idx) => {
159165
bailAndWatchWarning(comp); //warn the user if bail and watch both are used together

test/core-flags/devtool-flag.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ describe('--devtool flag', () => {
1313
it('should throw error for invalid config', () => {
1414
const { stderr } = run(__dirname, ['--devtool', 'invalid']);
1515

16-
expect(stderr).toContain('ValidationError: Invalid configuration object');
16+
expect(stderr).toContain('Invalid configuration object');
1717
});
1818
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const { run, isWindows } = require('../utils/test-utils');
3+
4+
describe('invalid schema', () => {
5+
it('should log webpack error and exit process on invalid config', () => {
6+
const { stderr, stdout, exitCode } = run(__dirname, ['--config', './webpack.config.mock.js']);
7+
console.log({ stderr, stdout, exitCode });
8+
expect(stderr).toContain('Invalid configuration object');
9+
// TODO - Fix exitcode check on windows
10+
if (!isWindows) {
11+
expect(exitCode).toEqual(1);
12+
}
13+
});
14+
15+
it('should log webpack error and exit process on invalid flag', () => {
16+
const { stderr, exitCode } = run(__dirname, ['--mode', 'Yukihira']);
17+
expect(stderr).toContain('Invalid configuration object');
18+
// TODO - Fix exitcode check on windows
19+
if (!isWindows) {
20+
expect(exitCode).toEqual(1);
21+
}
22+
});
23+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
mode: 'Nishinoya Yuu',
3+
};

test/stats/cli-flags/stats.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable node/no-extraneous-require */
22
/* eslint-disable node/no-unpublished-require */
33
'use strict';
4-
const { run, isWebpack5 } = require('../../utils/test-utils');
4+
const { run, isWebpack5, isWindows } = require('../../utils/test-utils');
55

66
const presets = ['normal', 'detailed', 'errors-only', 'errors-warnings', 'minimal', 'verbose', 'none'];
77

@@ -33,7 +33,7 @@ describe('stats flag', () => {
3333
});
3434

3535
it('should warn when an unknown flag stats value is passed', () => {
36-
const { stderr, stdout } = run(__dirname, ['--stats', 'foo']);
36+
const { stderr, exitCode } = run(__dirname, ['--stats', 'foo']);
3737
expect(stderr).toBeTruthy();
3838
expect(stderr).toContain('* configuration.stats should be one of these:');
3939
if (isWebpack5) {
@@ -43,6 +43,9 @@ describe('stats flag', () => {
4343
} else {
4444
expect(stderr).toContain('"none" | "errors-only" | "minimal" | "normal" | "detailed" | "verbose" | "errors-warnings"');
4545
}
46-
expect(stdout).toBeTruthy();
46+
// TODO - Fix exitcode check on windows
47+
if (!isWindows) {
48+
expect(exitCode).toEqual(1);
49+
}
4750
});
4851
});

test/target/flag-test/target-flag.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('--target flag', () => {
4343
it(`should throw error with invalid value for --target`, () => {
4444
const { stderr } = run(__dirname, ['--target', 'invalid']);
4545
if (isWebpack5) {
46-
expect(stderr).toContain(`Error: Unknown target 'invalid'`);
46+
expect(stderr).toContain(`Unknown target 'invalid'`);
4747
} else {
4848
expect(stderr).toContain('Invalid configuration object');
4949
}

0 commit comments

Comments
 (0)