Skip to content

Commit

Permalink
fix: false positive warning when --watch used (#3783)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 9, 2023
1 parent 8027f71 commit c0436ba
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 23 deletions.
7 changes: 3 additions & 4 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ class ServeCommand {
process.exit(2);
}

const builtInOptions = cli.getBuiltInOptions().filter(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(option: any) => option.name !== "watch",
);
const builtInOptions = cli.getBuiltInOptions();

return [...builtInOptions, ...devServerFlags];
},
Expand Down Expand Up @@ -110,6 +107,8 @@ class ServeCommand {
env: { WEBPACK_SERVE: true, ...options.env },
};

webpackCLIOptions.isWatchingLikeCommand = true;

const compiler = await cli.createCompiler(webpackCLIOptions);

if (!compiler) {
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ interface WebpackRunOptions extends WebpackOptionsNormalized {
argv?: Argv;
env: Env;
failOnWarnings?: boolean;
isWatchingLikeCommand?: boolean;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2163,11 +2163,10 @@ class WebpackCLI implements IWebpackCLI {

// Output warnings
if (
(typeof originalWatchValue !== "undefined" ||
(options.argv && typeof options.argv.watch !== "undefined")) &&
options.isWatchingLikeCommand &&
options.argv &&
options.argv.env &&
(options.argv.env["WEBPACK_WATCH"] || options.argv.env["WEBPACK_SERVE"])
(typeof originalWatchValue !== "undefined" || typeof options.argv.watch !== "undefined")
) {
this.logger.warn(
`No need to use the '${
Expand Down Expand Up @@ -2418,6 +2417,7 @@ class WebpackCLI implements IWebpackCLI {

if (isWatchCommand) {
options.watch = true;
options.isWatchingLikeCommand = true;
}

compiler = await this.createCompiler(options as WebpackDevServerOptions, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ exports[`basic serve usage should log an error on unknown flag: stderr 1`] = `
exports[`basic serve usage should log an error on unknown flag: stdout 1`] = `""`;

exports[`basic serve usage should log error on using '--watch' flag with serve: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--watch'
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`basic serve usage should log error on using '--watch' flag with serve: stdout 1`] = `""`;

exports[`basic serve usage should log error on using '-w' alias with serve: stderr 1`] = `
"[webpack-cli] Error: Unknown option '-w'
[webpack-cli] Run 'webpack --help' to see available commands and options"
"[webpack-cli] No need to use the 'serve' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<network-ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<network-ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/test/serve/basic/public' directory"
`;

exports[`basic serve usage should log error on using '-w' alias with serve: stdout 1`] = `""`;

exports[`basic serve usage should log used supplied config with serve: stderr 1`] = `
" [webpack-cli] Compiler starting...
[webpack-cli] Compiler is using config: '<cwd>/test/serve/basic/log.config.js'
Expand All @@ -34,6 +29,15 @@ exports[`basic serve usage should log used supplied config with serve: stderr 1`
[webpack-cli] Compiler is watching files for updates..."
`;

exports[`basic serve usage should log warning on using '-w' alias with serve: stderr 1`] = `
"[webpack-cli] No need to use the 'serve' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<network-ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<network-ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/test/serve/basic/public' directory"
`;

exports[`basic serve usage should respect the "publicPath" option from configuration (from the "devServer" options): stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:<port>/
Expand Down
12 changes: 7 additions & 5 deletions test/serve/basic/serve-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,19 @@ describe("basic serve usage", () => {
it("should log error on using '--watch' flag with serve", async () => {
const { exitCode, stdout, stderr } = await runWatch(testPath, ["serve", "--watch"]);

expect(exitCode).toBe(2);
expect(exitCode).toBe(0);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
expect(stdout).toContain("HotModuleReplacementPlugin");
expect(stdout).toContain("main.js");
});

it("should log error on using '-w' alias with serve", async () => {
it("should log warning on using '-w' alias with serve", async () => {
const { exitCode, stdout, stderr } = await runWatch(testPath, ["serve", "-w"]);

expect(exitCode).toBe(2);
expect(exitCode).toBe(0);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
expect(stdout).toContain("HotModuleReplacementPlugin");
expect(stdout).toContain("main.js");
});

it("should log an error on unknown flag", async () => {
Expand Down
1 change: 1 addition & 0 deletions test/watch/basic/basic.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
16 changes: 16 additions & 0 deletions test/watch/basic/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ describe("basic", () => {
}
}
});

proc.stderr.on("data", (chunk) => {
const data = chunk.toString();

expect(data).not.toContain(
" No need to use the 'watch' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.",
);
});
});

it("should recompile upon file change using the `watch` command", (done) => {
Expand Down Expand Up @@ -77,6 +85,14 @@ describe("basic", () => {
}
}
});

proc.stderr.on("data", (chunk) => {
const data = chunk.toString();

expect(data).not.toContain(
" No need to use the 'watch' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.",
);
});
});

it("should recompile upon file change using the `watch` command and entries syntax", (done) => {
Expand Down

0 comments on commit c0436ba

Please sign in to comment.