Skip to content

Commit

Permalink
fix: no serve when dev-server is false (#2947)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
rishabh3112 and evenstensberg authored Nov 22, 2023
1 parent bd661fb commit a93e860
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
7 changes: 0 additions & 7 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> = cli.webpack.cli.getArguments(devServer.schema);

// New options format
// { flag1: {}, flag2: {} }
return Object.keys(options).map((key) => {
Expand Down Expand Up @@ -69,7 +67,6 @@ class ServeCommand {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const processors: Array<(opts: Record<string, any>) => void> = [];

for (const optionName in options) {
const kebabedOption = cli.toKebabCase(optionName);
const isBuiltInOption = builtInOptions.find(
Expand All @@ -93,7 +90,6 @@ class ServeCommand {
devServerCLIOptions[optionName] = options[optionName];
}
}

for (const processor of processors) {
processor(devServerCLIOptions);
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -211,7 +205,6 @@ class ServeCommand {
}

const devServerOptions: WebpackDevServerOptions = result as WebpackDevServerOptions;

if (devServerOptions.port) {
const portNumber = Number(devServerOptions.port);

Expand Down
7 changes: 5 additions & 2 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions test/serve/basic/dev-server-false.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
mode: "development",
devtool: false,
devServer: false,
};
10 changes: 10 additions & 0 deletions test/serve/basic/serve-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);

Expand Down

0 comments on commit a93e860

Please sign in to comment.