Skip to content

Commit 9c33fbf

Browse files
feat: add the --dev-server-per-compiler option to webpack serve
In multi-compiler mode every dev server serves all compilers by default, the same as dev servers without plugin support do. With the new option, each dev server is applied as a plugin only to the compiler that defines it, and each compiler's stats are printed using the stats options of its own dev server. Dev servers without plugin support ignore the option with a warning. Also require webpack-dev-server@6.1 for development and mark the workaround for dev servers without plugin support for removal in the next major release. Claude-Session: https://claude.ai/code/session_013FhutRFjuQemBzekJMYcqL
1 parent d7a934d commit 9c33fbf

12 files changed

Lines changed: 295 additions & 51 deletions

File tree

‎.changeset/dev-server-6-plugin.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
---
44

55
feat: run `webpack-dev-server@6` as a compiler plugin. The CLI drives watch compilation, prints build stats and closes the compiler on shutdown. Older dev servers continue to manage their own compilation.
6+
7+
In multi-compiler mode, every dev server serves all compilers by default. Use the new `--dev-server-per-compiler` option of `webpack serve` to serve each compiler only by the dev server from its own configuration.

‎SERVE-OPTIONS-v6.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Options
1818
-j, --json [pathToJsonFile] Prints result as JSON or store it in a file.
1919
--fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack.
2020
--disable-interpret Disable interpret for loading the config file.
21+
--dev-server-per-compiler Serve each compiler only by the dev server from its own configuration in multi-compiler mode, instead of serving all compilers by every dev server.
2122
-d, --devtool <value> A developer tool to enhance debugging (false | eval | [inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map).
2223
--no-devtool Negative 'devtool' option.
2324
--entry <value...> A module that is loaded upon startup. Only the last one is exported.

‎package-lock.json‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"typescript": "^6.0.3",
9696
"webpack": "^5.112.0",
9797
"webpack-bundle-analyzer": "^5.4.0",
98-
"webpack-dev-server": "^6.0.0"
98+
"webpack-dev-server": "^6.1.0"
9999
},
100100
"peerDependencies": {
101101
"webpack": "5.x.x"

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

Lines changed: 110 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,21 @@ class WebpackCLI {
21752175
},
21762176
options: (cmd) => {
21772177
const { webpack, devServer } = cmd.context;
2178-
const webpackOptions = this.schemaToOptions(webpack, undefined, this.#CLIOptions);
2178+
const webpackOptions = this.schemaToOptions(webpack, undefined, [
2179+
...this.#CLIOptions,
2180+
{
2181+
name: "dev-server-per-compiler",
2182+
configs: [
2183+
{
2184+
type: "enum",
2185+
values: [true],
2186+
},
2187+
],
2188+
description:
2189+
"Serve each compiler only by the dev server from its own configuration in multi-compiler mode, instead of serving all compilers by every dev server.",
2190+
hidden: false,
2191+
},
2192+
]);
21792193
// @ts-expect-error different versions of the `Schema` type
21802194
const devServerOptions = this.schemaToOptions(webpack, devServer.schema, undefined, {
21812195
hidden: false,
@@ -2222,8 +2236,22 @@ class WebpackCLI {
22222236
}
22232237

22242238
const DevServer: DevServerConstructor = devServer;
2239+
// TODO in the next major release, require `webpack-dev-server@6` and remove this check
2240+
// together with the `start()`/`stop()` workaround for dev servers without plugin support
22252241
const isDevServerPlugin =
22262242
typeof (DevServer.prototype as { apply?: unknown }).apply === "function";
2243+
const isMultiCompiler = this.isMultipleCompiler(compiler);
2244+
let isDevServerPerCompiler = Boolean(options.devServerPerCompiler) && isMultiCompiler;
2245+
2246+
if (isDevServerPerCompiler && !isDevServerPlugin) {
2247+
this.logger.warn(
2248+
"The '--dev-server-per-compiler' option requires webpack-dev-server with plugin support (v6 or later), all compilers will be served by every dev server.",
2249+
);
2250+
2251+
isDevServerPerCompiler = false;
2252+
}
2253+
2254+
// TODO in the next major release, remove it, servers without plugin support only
22272255
const servers: InstanceType<DevServerConstructor>[] = [];
22282256

22292257
if (!isDevServerPlugin && this.#needWatchStdin(compiler)) {
@@ -2235,12 +2263,15 @@ class WebpackCLI {
22352263
process.stdin.resume();
22362264
}
22372265

2238-
const compilers = this.isMultipleCompiler(compiler) ? compiler.compilers : [compiler];
2266+
const compilers = isMultiCompiler ? compiler.compilers : [compiler as Compiler];
22392267
const possibleCompilers = compilers.filter((compiler) => compiler.options.devServer);
22402268
const compilersForDevServer =
22412269
possibleCompilers.length > 0 ? possibleCompilers : [compilers[0]];
22422270
const usedPorts: number[] = [];
2243-
const devServerConfigurations: DevServerConfiguration[] = [];
2271+
const devServers: {
2272+
compiler: Compiler;
2273+
configuration: DevServerConfiguration;
2274+
}[] = [];
22442275
const validatePort = ({ port }: DevServerConfiguration): void => {
22452276
if (port && port !== "auto") {
22462277
const portNumber = Number(port);
@@ -2291,30 +2322,33 @@ class WebpackCLI {
22912322
validatePort(devServerConfiguration);
22922323
}
22932324

2294-
devServerConfigurations.push(devServerConfiguration);
2325+
devServers.push({
2326+
compiler: compilerForDevServer,
2327+
configuration: devServerConfiguration,
2328+
});
22952329
}
22962330

2297-
for (const devServerConfiguration of devServerConfigurations) {
2331+
for (const { compiler: compilerForDevServer, configuration } of devServers) {
22982332
if (!isDevServerPlugin) {
2299-
validatePort(devServerConfiguration);
2333+
validatePort(configuration);
23002334
}
23012335

23022336
try {
23032337
if (isDevServerPlugin) {
2304-
let { port } = devServerConfiguration;
2338+
let { port } = configuration;
23052339

23062340
if (
2307-
devServerConfigurations.length > 1 &&
2308-
!devServerConfiguration.ipc &&
2341+
devServers.length > 1 &&
2342+
!configuration.ipc &&
23092343
(typeof port === "undefined" || port === "auto")
23102344
) {
23112345
const { default: getPort, portNumbers } = await import("get-port");
23122346
const basePort = Number.parseInt(
23132347
process.env.WEBPACK_DEV_SERVER_BASE_PORT ?? "8080",
23142348
10,
23152349
);
2316-
const host = devServerConfiguration.host
2317-
? await DevServer.getHostname(devServerConfiguration.host)
2350+
const host = configuration.host
2351+
? await DevServer.getHostname(configuration.host)
23182352
: undefined;
23192353

23202354
// Plugins select ports before any server starts listening.
@@ -2326,19 +2360,21 @@ class WebpackCLI {
23262360
usedPorts.push(port);
23272361
}
23282362

2329-
// v5 typings lack the plugin constructor.
2363+
// By default, every dev server serves all compilers, regardless of which defines
2364+
// `devServer`, the same as dev servers without plugin support do.
2365+
// The CLI owns signal handling and closes the compiler on shutdown.
2366+
// TODO in the next major release, use `DevServer` directly, the cast is only
2367+
// needed for `webpack-dev-server@5` typings, which lack the plugin constructor
23302368
const DevServerPlugin = DevServer as unknown as new (
23312369
options: DevServerConfiguration,
23322370
) => { apply(compiler: Compiler | MultiCompiler): void };
23332371

2334-
// Serve all child compilers, regardless of which defines devServer.
2335-
new DevServerPlugin({
2336-
...devServerConfiguration,
2337-
port,
2338-
setupExitSignals: false,
2339-
}).apply(compiler);
2372+
new DevServerPlugin({ ...configuration, port, setupExitSignals: false }).apply(
2373+
isDevServerPerCompiler ? compilerForDevServer : compiler,
2374+
);
23402375
} else {
2341-
const server = new DevServer(devServerConfiguration, compiler);
2376+
// TODO in the next major release, remove it, servers without plugin support only
2377+
const server = new DevServer(configuration, compiler);
23422378

23432379
await server.start();
23442380

@@ -2362,7 +2398,8 @@ class WebpackCLI {
23622398
process.exit(2);
23632399
}
23642400

2365-
// Older servers manage compilation and signals themselves.
2401+
// TODO in the next major release, remove it, servers without plugin support manage
2402+
// compilation and signals themselves
23662403
if (!isDevServerPlugin) {
23672404
return;
23682405
}
@@ -2379,6 +2416,47 @@ class WebpackCLI {
23792416
process.stdin.resume();
23802417
}
23812418

2419+
const getStatsOptions = (
2420+
compiler: Compiler,
2421+
configuration?: DevServerConfiguration,
2422+
): StatsOptions => {
2423+
const middlewareStats = configuration?.devMiddleware?.stats;
2424+
2425+
if (typeof middlewareStats === "undefined") {
2426+
return compiler.options.stats as StatsOptions;
2427+
}
2428+
2429+
const statsOptions: StatsOptions =
2430+
typeof middlewareStats === "boolean"
2431+
? { preset: middlewareStats ? "normal" : "none" }
2432+
: typeof middlewareStats === "string"
2433+
? { preset: middlewareStats }
2434+
: { ...middlewareStats };
2435+
2436+
if (typeof statsOptions.colors === "undefined") {
2437+
statsOptions.colors = (compiler.options.stats as StatsOptions).colors;
2438+
}
2439+
2440+
return statsOptions;
2441+
};
2442+
const printStats = (
2443+
stats: Stats | MultiStats,
2444+
getConfiguration: (compiler: Compiler) => DevServerConfiguration | undefined,
2445+
): void => {
2446+
const statsOptions = isMultiCompiler
2447+
? {
2448+
children: compilers.map((compiler) =>
2449+
getStatsOptions(compiler, getConfiguration(compiler)),
2450+
),
2451+
}
2452+
: getStatsOptions(compiler as Compiler, getConfiguration(compiler as Compiler));
2453+
const printedStats = stats.toString(statsOptions);
2454+
2455+
if (printedStats) {
2456+
this.logger.raw(printedStats);
2457+
}
2458+
};
2459+
23822460
const watchCallback = (error: Error | null, stats?: Stats | MultiStats): void => {
23832461
if (error) {
23842462
if (this.isValidationError(error)) {
@@ -2398,39 +2476,22 @@ class WebpackCLI {
23982476
process.exitCode = 1;
23992477
}
24002478

2401-
// Each middleware can override stats for the whole compiler.
2402-
for (const devServerConfiguration of devServerConfigurations) {
2403-
const middlewareStats = devServerConfiguration.devMiddleware?.stats;
2404-
const getStatsOptions = (compiler: Compiler): StatsOptions => {
2405-
if (typeof middlewareStats === "undefined") {
2406-
return compiler.options.stats as StatsOptions;
2407-
}
2408-
2409-
const statsOptions: StatsOptions =
2410-
typeof middlewareStats === "boolean"
2411-
? { preset: middlewareStats ? "normal" : "none" }
2412-
: typeof middlewareStats === "string"
2413-
? { preset: middlewareStats }
2414-
: { ...middlewareStats };
2415-
2416-
if (typeof statsOptions.colors === "undefined") {
2417-
statsOptions.colors = (compiler.options.stats as StatsOptions).colors;
2418-
}
2419-
2420-
return statsOptions;
2421-
};
2422-
const statsOptions = this.isMultipleCompiler(compiler)
2423-
? { children: compiler.compilers.map(getStatsOptions) }
2424-
: getStatsOptions(compiler);
2425-
const printedStats = stats.toString(statsOptions);
2426-
2427-
if (printedStats) {
2428-
this.logger.raw(printedStats);
2479+
if (isDevServerPerCompiler) {
2480+
// Each compiler is printed using the stats options of its own dev server
2481+
printStats(
2482+
stats,
2483+
(compiler) =>
2484+
devServers.find((devServer) => devServer.compiler === compiler)?.configuration,
2485+
);
2486+
} else {
2487+
// Each middleware prints stats for the whole compiler
2488+
for (const { configuration } of devServers) {
2489+
printStats(stats, () => configuration);
24292490
}
24302491
}
24312492
};
24322493

2433-
if (this.isMultipleCompiler(compiler)) {
2494+
if (isMultiCompiler) {
24342495
compiler.watch(
24352496
compiler.compilers.map((compiler) => compiler.options.watchOptions || {}),
24362497
watchCallback,

0 commit comments

Comments
 (0)