Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no serve when dev-server is false #2947

Merged
merged 8 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
} 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);

Check warning on line 2386 in packages/webpack-cli/src/webpack-cli.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L2386 was not covered by tests
}
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