From a93e8603a4c2639916152a013afed04c0e8f3a35 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Thu, 23 Nov 2023 02:56:44 +0530 Subject: [PATCH] fix: no serve when dev-server is false (#2947) * chore: not create dev-server when false * test: add test for dev server false * fix: exit on undefined behaviour * chore: dont update lockfile * chore: no eol * chore: no eol * chore: no eol * fix: remove old code --------- Co-authored-by: Even Stensberg --- packages/serve/src/index.ts | 7 ------- packages/webpack-cli/src/webpack-cli.ts | 7 +++++-- test/serve/basic/dev-server-false.config.js | 5 +++++ test/serve/basic/serve-basic.test.js | 10 ++++++++++ 4 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 test/serve/basic/dev-server-false.config.js diff --git a/packages/serve/src/index.ts b/packages/serve/src/index.ts index b6bef8847bf..0a79b071f9b 100644 --- a/packages/serve/src/index.ts +++ b/packages/serve/src/index.ts @@ -11,10 +11,8 @@ class ServeCommand { const loadDevServerOptions = () => { // eslint-disable-next-line @typescript-eslint/no-var-requires const devServer = require(WEBPACK_DEV_SERVER_PACKAGE); - // eslint-disable-next-line @typescript-eslint/no-explicit-any const options: Record = cli.webpack.cli.getArguments(devServer.schema); - // New options format // { flag1: {}, flag2: {} } return Object.keys(options).map((key) => { @@ -69,7 +67,6 @@ class ServeCommand { // eslint-disable-next-line @typescript-eslint/no-explicit-any const processors: Array<(opts: Record) => void> = []; - for (const optionName in options) { const kebabedOption = cli.toKebabCase(optionName); const isBuiltInOption = builtInOptions.find( @@ -93,7 +90,6 @@ class ServeCommand { devServerCLIOptions[optionName] = options[optionName]; } } - for (const processor of processors) { processor(devServerCLIOptions); } @@ -108,13 +104,11 @@ class ServeCommand { }; webpackCLIOptions.isWatchingLikeCommand = true; - const compiler = await cli.createCompiler(webpackCLIOptions); if (!compiler) { return; } - const servers: (typeof DevServer)[] = []; if (cli.needWatchStdin(compiler)) { @@ -211,7 +205,6 @@ class ServeCommand { } const devServerOptions: WebpackDevServerOptions = result as WebpackDevServerOptions; - if (devServerOptions.port) { const portNumber = Number(devServerOptions.port); diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 4d94683c2b6..6a58dfd947e 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2378,10 +2378,13 @@ class WebpackCLI implements IWebpackCLI { } else if (typeof options.nodeEnv === "string") { process.env.NODE_ENV = options.nodeEnv; } - let config = await this.loadConfig(options); config = await this.buildConfig(config, options); - + const { devServer } = config.options as boolean | WebpackDevServerOptions["options"]; + const devServerIsFalse = devServer !== undefined && devServer === false; + if (devServerIsFalse && options.argv && options.argv.env && options.argv.env.WEBPACK_SERVE) { + process.exit(0); + } let compiler: WebpackCompiler; try { compiler = this.webpack( diff --git a/test/serve/basic/dev-server-false.config.js b/test/serve/basic/dev-server-false.config.js new file mode 100644 index 00000000000..369fb3b9b76 --- /dev/null +++ b/test/serve/basic/dev-server-false.config.js @@ -0,0 +1,5 @@ +module.exports = { + mode: "development", + devtool: false, + devServer: false, +}; diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index ec3d59daffa..90f344b86f9 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -116,6 +116,16 @@ describe("basic serve usage", () => { expect(stdout).toContain("main.js"); }); + it("should not start dev server when supplied false", async () => { + const { stderr, stdout } = await runWatch(__dirname, [ + "serve", + "--config", + "dev-server-false.config.js", + ]); + expect(stdout).toBeFalsy(); + expect(stderr).toBeFalsy(); + }); + it('should work with the "--stats" option', async () => { const { stderr, stdout } = await runWatch(__dirname, ["serve", "--stats"]);