Skip to content

Commit 9d6dbda

Browse files
authored
fix: avoid deprecation message
1 parent 2c85b9b commit 9d6dbda

File tree

8 files changed

+202
-63
lines changed

8 files changed

+202
-63
lines changed

packages/serve/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ServeCommand {
2424
process.exit(2);
2525
}
2626

27-
const builtInOptions = cli.getBuiltInOptions();
27+
const builtInOptions = cli.getBuiltInOptions().filter((option) => option.name !== 'watch');
2828

2929
return [...builtInOptions, ...devServerFlags];
3030
},

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

+40-16
Original file line numberDiff line numberDiff line change
@@ -537,27 +537,17 @@ class WebpackCLI {
537537
const isWatchCommandUsed = isCommand(commandName, watchCommandOptions);
538538

539539
if (isBuildCommandUsed || isWatchCommandUsed) {
540+
const options = this.getBuiltInOptions();
541+
540542
await this.makeCommand(
541543
isBuildCommandUsed ? buildCommandOptions : watchCommandOptions,
542-
this.getBuiltInOptions(),
544+
isWatchCommandUsed ? options.filter((option) => option.name !== 'watch') : options,
543545
async (entries, options) => {
544546
if (entries.length > 0) {
545547
options.entry = [...entries, ...(options.entry || [])];
546548
}
547549

548-
if (isWatchCommandUsed) {
549-
if (typeof options.watch !== 'undefined') {
550-
this.logger.warn(
551-
`No need to use the ${
552-
options.watch ? "'--watch, -w'" : "'--no-watch'"
553-
} option together with the 'watch' command, it does not make sense`,
554-
);
555-
}
556-
557-
options.watch = true;
558-
}
559-
560-
await this.buildCommand(options);
550+
await this.buildCommand(options, isWatchCommandUsed);
561551
},
562552
);
563553
} else if (isCommand(commandName, helpCommandOptions)) {
@@ -1369,6 +1359,31 @@ class WebpackCLI {
13691359
process.exit(2);
13701360
}
13711361

1362+
const outputHints = (configOptions) => {
1363+
if (
1364+
configOptions.watch &&
1365+
options.argv &&
1366+
options.argv.env &&
1367+
(options.argv.env['WEBPACK_WATCH'] || options.argv.env['WEBPACK_SERVE'])
1368+
) {
1369+
this.logger.warn(
1370+
`No need to use the '${
1371+
options.argv.env['WEBPACK_WATCH'] ? 'watch' : 'serve'
1372+
}' command together with '{ watch: true }' configuration, it does not make sense.`,
1373+
);
1374+
1375+
if (options.argv.env['WEBPACK_SERVE']) {
1376+
configOptions.watch = false;
1377+
}
1378+
}
1379+
1380+
return configOptions;
1381+
};
1382+
1383+
config.options = Array.isArray(config.options)
1384+
? config.options.map((options) => outputHints(options))
1385+
: outputHints(config.options);
1386+
13721387
if (this.webpack.cli) {
13731388
const processArguments = (configOptions) => {
13741389
const args = this.getBuiltInOptions()
@@ -1666,7 +1681,7 @@ class WebpackCLI {
16661681
return compiler;
16671682
}
16681683

1669-
async buildCommand(options) {
1684+
async buildCommand(options, isWatchCommand) {
16701685
let compiler;
16711686

16721687
const callback = (error, stats) => {
@@ -1731,7 +1746,16 @@ class WebpackCLI {
17311746
}
17321747
};
17331748

1734-
options.argv = { ...options, env: { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true, ...options.env } };
1749+
const env =
1750+
isWatchCommand || options.watch
1751+
? { WEBPACK_WATCH: true, ...options.env }
1752+
: { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true, ...options.env };
1753+
1754+
options.argv = { ...options, env };
1755+
1756+
if (isWatchCommand) {
1757+
options.watch = true;
1758+
}
17351759

17361760
compiler = await this.createCompiler(options, callback);
17371761

test/serve/basic/serve-basic.test.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,33 @@ describe('basic serve usage', () => {
296296
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
297297
});
298298

299-
it('should log and error on unknown flag', async () => {
299+
it('should work and log warning on the `watch option in a configuration', async () => {
300+
const { stderr, stdout } = await runServe(__dirname, ['--config', './watch.config.js', '--port', port]);
301+
302+
expect(stderr).toContain(
303+
"No need to use the 'serve' command together with '{ watch: true }' configuration, it does not make sense.",
304+
);
305+
expect(stdout).toContain('development');
306+
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
307+
});
308+
309+
it("should log error on using '--watch' flag with serve", async () => {
310+
const { stdout, stderr } = await runServe(testPath, ['--watch']);
311+
312+
expect(stderr).toContain("Error: Unknown option '--watch'");
313+
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
314+
expect(stdout).toBeFalsy();
315+
});
316+
317+
it("should log error on using '-w' alias with serve", async () => {
318+
const { stdout, stderr } = await runServe(testPath, ['-w']);
319+
320+
expect(stderr).toContain("Error: Unknown option '-w'");
321+
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
322+
expect(stdout).toBeFalsy();
323+
});
324+
325+
it('should log an error on unknown flag', async () => {
300326
const { exitCode, stdout, stderr } = await runServe(testPath, ['--port', port, '--unknown-flag']);
301327

302328
expect(exitCode).toBe(2);

test/serve/basic/watch.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin');
2+
3+
module.exports = {
4+
mode: 'development',
5+
devtool: false,
6+
plugins: [new WebpackCLITestPlugin(['mode'], false, 'hooks.compilation.taps')],
7+
watch: true,
8+
};

test/watch/basic/basic.test.js

+20-45
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { run, runAndGetWatchProc, isWebpack5 } = require('../../utils/test-utils'
55
const { writeFileSync } = require('fs');
66
const { resolve } = require('path');
77

8-
const wordsInStatsv4 = ['Hash', 'Version', 'Time', 'Built at:', 'main.js'];
8+
const wordsInStatsv4 = ['Hash', 'Built at:', 'main.js'];
99
const wordsInStatsv5 = ['asset', 'index.js', 'compiled successfully'];
1010

1111
describe('basic', () => {
@@ -118,11 +118,10 @@ describe('basic', () => {
118118
});
119119
});
120120

121-
it('should recompile upon file change using the `command` option and the `--watch` option and log warning', (done) => {
122-
const proc = runAndGetWatchProc(__dirname, ['watch', '--watch', '--mode', 'development'], false, '', true);
121+
it('should log warning about the `watch` option in the configuration and recompile upon file change using the `watch` command', (done) => {
122+
const proc = runAndGetWatchProc(__dirname, ['--watch', '--mode', 'development', '--config', './watch.config.js'], false, '', true);
123123

124124
let modified = false;
125-
let hasWarning = false;
126125

127126
proc.stdout.on('data', (chunk) => {
128127
const data = stripAnsi(chunk.toString());
@@ -138,7 +137,7 @@ describe('basic', () => {
138137
}
139138
}
140139

141-
if (!modified && !hasWarning) {
140+
if (!modified) {
142141
process.nextTick(() => {
143142
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`);
144143
});
@@ -154,51 +153,27 @@ describe('basic', () => {
154153
proc.stderr.on('data', (chunk) => {
155154
const data = stripAnsi(chunk.toString());
156155

157-
hasWarning = true;
158-
159-
expect(data).toContain("No need to use the '--watch, -w' option together with the 'watch' command, it does not make sense");
156+
expect(data).toContain(
157+
"No need to use the 'watch' command together with '{ watch: true }' configuration, it does not make sense.",
158+
);
160159
});
161160
});
162161

163-
it('should recompile upon file change using the `command` option and the `--no-watch` option and log warning', (done) => {
164-
const proc = runAndGetWatchProc(__dirname, ['watch', '--no-watch', '--mode', 'development'], false, '', true);
165-
166-
let modified = false;
167-
let hasWarning = false;
168-
169-
proc.stdout.on('data', (chunk) => {
170-
const data = stripAnsi(chunk.toString());
171-
172-
if (data.includes('index.js')) {
173-
if (isWebpack5) {
174-
for (const word of wordsInStatsv5) {
175-
expect(data).toContain(word);
176-
}
177-
} else {
178-
for (const word of wordsInStatsv4) {
179-
expect(data).toContain(word);
180-
}
181-
}
182-
183-
if (!modified && !hasWarning) {
184-
process.nextTick(() => {
185-
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`);
186-
});
162+
it('should recompile upon file change using the `command` option and the `--watch` option and log warning', async () => {
163+
const { exitCode, stderr, stdout } = await run(__dirname, ['watch', '--watch', '--mode', 'development']);
187164

188-
modified = true;
189-
} else {
190-
proc.kill();
191-
done();
192-
}
193-
}
194-
});
195-
196-
proc.stderr.on('data', (chunk) => {
197-
const data = stripAnsi(chunk.toString());
165+
expect(exitCode).toBe(2);
166+
expect(stderr).toContain("Error: Unknown option '--watch'");
167+
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
168+
expect(stdout).toBeFalsy();
169+
});
198170

199-
hasWarning = true;
171+
it('should recompile upon file change using the `command` option and the `--no-watch` option and log warning', async () => {
172+
const { exitCode, stderr, stdout } = await run(__dirname, ['watch', '--no-watch', '--mode', 'development']);
200173

201-
expect(data).toContain("No need to use the '--no-watch' option together with the 'watch' command, it does not make sense");
202-
});
174+
expect(exitCode).toBe(2);
175+
expect(stderr).toContain("Error: Unknown option '--no-watch'");
176+
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
177+
expect(stdout).toBeFalsy();
203178
});
204179
});
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('watch flag test');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
const stripAnsi = require('strip-ansi');
4+
const { runAndGetWatchProc, isWebpack5 } = require('../../utils/test-utils');
5+
const { writeFileSync } = require('fs');
6+
const { resolve } = require('path');
7+
8+
const wordsInStatsv4 = ['Hash', 'Built at:', 'main.js'];
9+
const wordsInStatsv5 = ['asset', 'index.js', 'compiled successfully'];
10+
11+
describe('watch variable', () => {
12+
it('should pass `WEBPACK_WATCH` env variable and recompile upon file change using the `watch` command', (done) => {
13+
const proc = runAndGetWatchProc(__dirname, ['watch', '--mode', 'development'], false, '', true);
14+
15+
let modified = false;
16+
17+
proc.stdout.on('data', (chunk) => {
18+
const data = stripAnsi(chunk.toString());
19+
20+
expect(data).not.toContain('FAIL');
21+
22+
if (data.includes('index.js')) {
23+
if (isWebpack5) {
24+
for (const word of wordsInStatsv5) {
25+
expect(data).toContain(word);
26+
}
27+
} else {
28+
for (const word of wordsInStatsv4) {
29+
expect(data).toContain(word);
30+
}
31+
}
32+
33+
if (!modified) {
34+
process.nextTick(() => {
35+
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`);
36+
});
37+
38+
modified = true;
39+
} else {
40+
proc.kill();
41+
done();
42+
}
43+
}
44+
});
45+
});
46+
47+
it('should pass `WEBPACK_WATCH` env variable and recompile upon file change using the `--watch` option', (done) => {
48+
const proc = runAndGetWatchProc(__dirname, ['--watch', '--mode', 'development'], false, '', true);
49+
50+
let modified = false;
51+
52+
proc.stdout.on('data', (chunk) => {
53+
const data = stripAnsi(chunk.toString());
54+
55+
expect(data).not.toContain('FAIL');
56+
57+
if (data.includes('index.js')) {
58+
if (isWebpack5) {
59+
for (const word of wordsInStatsv5) {
60+
expect(data).toContain(word);
61+
}
62+
} else {
63+
for (const word of wordsInStatsv4) {
64+
expect(data).toContain(word);
65+
}
66+
}
67+
68+
if (!modified) {
69+
process.nextTick(() => {
70+
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`);
71+
});
72+
73+
modified = true;
74+
} else {
75+
proc.kill();
76+
done();
77+
}
78+
}
79+
});
80+
});
81+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const isInProcess = process.env.WEBPACK_WATCH;
2+
3+
class CustomTestPlugin {
4+
constructor(isInEnvironment) {
5+
this.isInEnvironment = isInEnvironment;
6+
}
7+
apply(compiler) {
8+
compiler.hooks.done.tap('testPlugin', () => {
9+
if (!isInProcess && this.isInEnvironment) {
10+
console.log('PASS');
11+
} else {
12+
console.log('FAIL');
13+
}
14+
});
15+
}
16+
}
17+
18+
module.exports = (env) => {
19+
return {
20+
mode: 'development',
21+
devtool: false,
22+
plugins: [new CustomTestPlugin(env.WEBPACK_WATCH)],
23+
};
24+
};

0 commit comments

Comments
 (0)