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: support devServer: false #4045

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

// 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 @@ -90,6 +91,7 @@
devServerCLIOptions[optionName] = options[optionName];
}
}

for (const processor of processors) {
processor(devServerCLIOptions);
}
Expand All @@ -104,11 +106,13 @@
};

webpackCLIOptions.isWatchingLikeCommand = true;

const compiler = await cli.createCompiler(webpackCLIOptions);

if (!compiler) {
return;
}

const servers: (typeof DevServer)[] = [];

if (cli.needWatchStdin(compiler)) {
Expand Down Expand Up @@ -146,6 +150,12 @@
const usedPorts: number[] = [];

for (const compilerForDevServer of compilersForDevServer) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (compilerForDevServer.options.devServer === false) {
continue;

Check warning on line 156 in packages/serve/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/serve/src/index.ts#L156

Added line #L156 was not covered by tests
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const args = devServerFlags.reduce((accumulator: Record<string, any>, flag: any) => {
accumulator[flag.name] = flag;
Expand Down Expand Up @@ -205,6 +215,7 @@
}

const devServerOptions: WebpackDevServerOptions = result as WebpackDevServerOptions;

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

Expand Down Expand Up @@ -233,6 +244,11 @@
process.exit(2);
}
}

if (servers.length === 0) {
cli.logger.error("No dev server configurations to run");
process.exit(2);

Check warning on line 250 in packages/serve/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/serve/src/index.ts#L249-L250

Added lines #L249 - L250 were not covered by tests
}
},
);
}
Expand Down
8 changes: 3 additions & 5 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2378,14 +2378,12 @@ 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(
config.options as WebpackConfiguration,
Expand Down