Skip to content

Commit 6140b24

Browse files
fix: show warning if bail and watch are used together (#1804)
* fix: show warning if bail and watch are used together * fix: watch * tests: update * chore: add comments Co-authored-by: James George <[email protected]>
1 parent 4014808 commit 6140b24

File tree

9 files changed

+79
-0
lines changed

9 files changed

+79
-0
lines changed

packages/webpack-cli/lib/groups/BasicGroup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class BasicGroup extends GroupHelper {
3636
if (arg === 'name') {
3737
options.name = args[arg];
3838
}
39+
if (arg === 'watch') {
40+
options.watch = true;
41+
}
3942
if (arg === 'entry') {
4043
options[arg] = this.resolveFilePath(args[arg], 'index.js');
4144
if (options[arg].length === 0) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const webpack = require('webpack');
22
const logger = require('./logger');
3+
const bailAndWatchWarning = require('./warnings/bailAndWatchWarning');
34
const { CompilerOutput } = require('./CompilerOutput');
45

56
class Compiler {
@@ -119,9 +120,11 @@ class Compiler {
119120

120121
if (this.compiler.compilers) {
121122
this.compiler.compilers.forEach((comp, idx) => {
123+
bailAndWatchWarning(comp); //warn the user if bail and watch both are used together
122124
this.setUpHookForCompilation(comp, outputOptions, options[idx]);
123125
});
124126
} else {
127+
bailAndWatchWarning(this.compiler);
125128
this.setUpHookForCompilation(this.compiler, outputOptions, options);
126129
}
127130

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const logger = require('../logger');
2+
3+
/**
4+
* warn the user if bail and watch both are used together
5+
* @param {Object} webpack compiler
6+
* @returns {void}
7+
*/
8+
const bailAndWatchWarning = (compiler) => {
9+
if (compiler.options.bail && compiler.options.watch) {
10+
logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
11+
}
12+
};
13+
14+
module.exports = bailAndWatchWarning;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const { run } = require('../utils/test-utils');
4+
5+
describe('bail and watch warning', () => {
6+
it('should log warning in case of single compiler', () => {
7+
const { stderr, stdout } = run(__dirname, ['-c', 'single-webpack.config.js']);
8+
9+
expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
10+
expect(stdout).toBeTruthy();
11+
});
12+
13+
it('should log warning in case of multiple compilers', () => {
14+
const { stderr, stdout } = run(__dirname, ['-c', 'multi-webpack.config.js']);
15+
16+
expect(stderr).toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
17+
expect(stdout).toBeTruthy();
18+
});
19+
20+
it('should log not log warning if both are not true', () => {
21+
const { stderr, stdout } = run(__dirname, ['-c', 'third-webpack.config.js']);
22+
23+
expect(stderr).not.toContain(`You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`);
24+
expect(stdout).toBeTruthy();
25+
});
26+
});

test/bail/multi-webpack.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = [
2+
{
3+
output: {
4+
filename: './dist-first.js',
5+
},
6+
name: 'first',
7+
entry: './src/first.js',
8+
mode: 'development',
9+
},
10+
{
11+
output: {
12+
filename: './dist-second.js',
13+
},
14+
name: 'second',
15+
entry: './src/second.js',
16+
mode: 'production',
17+
bail: true,
18+
watch: true,
19+
},
20+
];

test/bail/single-webpack.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
entry: './src/first.js',
3+
mode: 'development',
4+
bail: true,
5+
watch: true,
6+
};

test/bail/src/first.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('bail and watch warning test first');

test/bail/src/second.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('bail and watch warning test second');

test/bail/third-webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
entry: './src/first.js',
3+
mode: 'development',
4+
bail: true,
5+
};

0 commit comments

Comments
 (0)