Skip to content

Commit a93e860

Browse files
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 <[email protected]>
1 parent bd661fb commit a93e860

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

packages/serve/src/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ class ServeCommand {
1111
const loadDevServerOptions = () => {
1212
// eslint-disable-next-line @typescript-eslint/no-var-requires
1313
const devServer = require(WEBPACK_DEV_SERVER_PACKAGE);
14-
1514
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1615
const options: Record<string, any> = cli.webpack.cli.getArguments(devServer.schema);
17-
1816
// New options format
1917
// { flag1: {}, flag2: {} }
2018
return Object.keys(options).map((key) => {
@@ -69,7 +67,6 @@ class ServeCommand {
6967

7068
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7169
const processors: Array<(opts: Record<string, any>) => void> = [];
72-
7370
for (const optionName in options) {
7471
const kebabedOption = cli.toKebabCase(optionName);
7572
const isBuiltInOption = builtInOptions.find(
@@ -93,7 +90,6 @@ class ServeCommand {
9390
devServerCLIOptions[optionName] = options[optionName];
9491
}
9592
}
96-
9793
for (const processor of processors) {
9894
processor(devServerCLIOptions);
9995
}
@@ -108,13 +104,11 @@ class ServeCommand {
108104
};
109105

110106
webpackCLIOptions.isWatchingLikeCommand = true;
111-
112107
const compiler = await cli.createCompiler(webpackCLIOptions);
113108

114109
if (!compiler) {
115110
return;
116111
}
117-
118112
const servers: (typeof DevServer)[] = [];
119113

120114
if (cli.needWatchStdin(compiler)) {
@@ -211,7 +205,6 @@ class ServeCommand {
211205
}
212206

213207
const devServerOptions: WebpackDevServerOptions = result as WebpackDevServerOptions;
214-
215208
if (devServerOptions.port) {
216209
const portNumber = Number(devServerOptions.port);
217210

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,10 +2378,13 @@ class WebpackCLI implements IWebpackCLI {
23782378
} else if (typeof options.nodeEnv === "string") {
23792379
process.env.NODE_ENV = options.nodeEnv;
23802380
}
2381-
23822381
let config = await this.loadConfig(options);
23832382
config = await this.buildConfig(config, options);
2384-
2383+
const { devServer } = config.options as boolean | WebpackDevServerOptions["options"];
2384+
const devServerIsFalse = devServer !== undefined && devServer === false;
2385+
if (devServerIsFalse && options.argv && options.argv.env && options.argv.env.WEBPACK_SERVE) {
2386+
process.exit(0);
2387+
}
23852388
let compiler: WebpackCompiler;
23862389
try {
23872390
compiler = this.webpack(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
mode: "development",
3+
devtool: false,
4+
devServer: false,
5+
};

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ describe("basic serve usage", () => {
116116
expect(stdout).toContain("main.js");
117117
});
118118

119+
it("should not start dev server when supplied false", async () => {
120+
const { stderr, stdout } = await runWatch(__dirname, [
121+
"serve",
122+
"--config",
123+
"dev-server-false.config.js",
124+
]);
125+
expect(stdout).toBeFalsy();
126+
expect(stderr).toBeFalsy();
127+
});
128+
119129
it('should work with the "--stats" option', async () => {
120130
const { stderr, stdout } = await runWatch(__dirname, ["serve", "--stats"]);
121131

0 commit comments

Comments
 (0)