Skip to content

Commit 27453ab

Browse files
feat: run as a plugin from the top-level plugins of a multi-configuration
1 parent d16ad96 commit 27453ab

4 files changed

Lines changed: 104 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-dev-server": minor
3+
---
4+
5+
Run as a plugin from the top-level `plugins` of a multi-configuration, serving every configuration from one server.

‎examples/api/plugin/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ const devServerOptions = { ...config.devServer, open: true };
2323
config.plugins.push(new WebpackDevServer(devServerOptions));
2424
```
2525

26+
## Multiple configurations
27+
28+
To serve every configuration of a multi-configuration from one dev server, add
29+
the instance to the top-level `plugins` of the exported array, next to
30+
`parallelism`, rather than to one configuration's `plugins[]`. webpack applies
31+
it once to the `MultiCompiler`; this needs a webpack version that supports
32+
top-level `plugins` on a multi-configuration.
33+
34+
```js
35+
// webpack.config.js
36+
const WebpackDevServer = require("webpack-dev-server");
37+
38+
module.exports = Object.assign([appConfig, workerConfig], {
39+
plugins: [new WebpackDevServer({ port: 8080 })],
40+
});
41+
```
42+
2643
## Run
2744

2845
```console

‎lib/Server.js‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3873,17 +3873,23 @@ class Server {
38733873
await this.stop();
38743874
};
38753875

3876-
// Teardown is the one place a loop is unavoidable. A `MultiCompiler` has no
3877-
// `shutdown` hook, and its aggregate `watchClose` does NOT fire on
3878-
// `compiler.close()` (only on `watching.close()`), so the only signal that
3879-
// survives `compiler.close()` is each child's own `shutdown`. Tapping it
3876+
// `shutdown` survives `compiler.close()`, unlike the aggregate `watchClose`
3877+
// (only `watching.close()` fires it). On a `MultiCompiler` it is a
3878+
// `MultiHook` firing once per child, so `onShutdown` is guarded. Tapping it
38803879
// with `tapPromise` also lets `compiler.close()` await the server actually
38813880
// stopping, so the port is released before the next start.
3882-
const childCompilers = /** @type {MultiCompiler} */ (compiler)
3883-
.compilers || [compiler];
3884-
3885-
for (const childCompiler of childCompilers) {
3886-
childCompiler.hooks.shutdown.tapPromise(pluginName, onShutdown);
3881+
// TODO remove this fallback in the next major, once the minimum supported
3882+
// webpack (5.110.0) has `MultiCompiler.hooks.shutdown`, and tap
3883+
// `compiler.hooks.shutdown` alone.
3884+
const shutdownCompilers = /** @type {Compiler} */ (compiler).hooks.shutdown
3885+
? [compiler]
3886+
: /** @type {MultiCompiler} */ (compiler).compilers;
3887+
3888+
for (const shutdownCompiler of shutdownCompilers) {
3889+
/** @type {Compiler} */ (shutdownCompiler).hooks.shutdown.tapPromise(
3890+
pluginName,
3891+
onShutdown,
3892+
);
38873893
}
38883894
}
38893895
}

‎test/e2e/api-plugin.test.js‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
2121
const port = portsMap["api-plugin"];
2222
const [portA, portB] = portsMap["api-plugin-multi"];
2323

24+
// TODO remove this check in the next major, once the minimum supported webpack
25+
// applies the top-level `plugins` of a multi-configuration to the MultiCompiler.
26+
const supportsMultiCompilerPlugins =
27+
new webpack.MultiCompiler([], { plugins: [] }).options.plugins !== undefined;
28+
2429
const cssFilePath = path.resolve(
2530
__dirname,
2631
"../fixtures/reload-config/main.css",
@@ -657,6 +662,68 @@ describe("API (plugin)", () => {
657662
stopSpy.mockRestore();
658663
});
659664

665+
it(
666+
"should work from the top-level `plugins` of a multi-configuration",
667+
{ skip: !supportsMultiCompilerPlugins },
668+
async () => {
669+
const server = new Server({ port });
670+
const setupSpy = spyOn(server, "setup");
671+
const listenSpy = spyOn(server, "listen");
672+
const stopSpy = spyOn(server, "stop");
673+
const compiler = webpack(
674+
Object.assign([...multiCompilerConfig], { plugins: [server] }),
675+
);
676+
677+
await compile(compiler, port);
678+
679+
try {
680+
const responseOne = await fetch(
681+
`http://127.0.0.1:${port}/one-main.js`,
682+
);
683+
const responseTwo = await fetch(
684+
`http://127.0.0.1:${port}/two-main.js`,
685+
);
686+
687+
expect(responseOne.status).toBe(200);
688+
expect(responseTwo.status).toBe(200);
689+
expect(setupSpy).toHaveBeenCalledTimes(1);
690+
expect(listenSpy).toHaveBeenCalledTimes(1);
691+
} finally {
692+
await new Promise((resolve) => {
693+
compiler.close(resolve);
694+
});
695+
}
696+
697+
expect(stopSpy).toHaveBeenCalledTimes(1);
698+
setupSpy.mockRestore();
699+
listenSpy.mockRestore();
700+
stopSpy.mockRestore();
701+
},
702+
);
703+
704+
it("should stop the server once on webpack without `MultiCompiler.hooks.shutdown`", async () => {
705+
const compiler = webpack(multiCompilerConfig);
706+
// A webpack older than 5.110.0: the same compiler, minus the hook.
707+
const { shutdown, ...hooks } = compiler.hooks;
708+
const olderCompiler = Object.create(compiler, {
709+
hooks: { value: hooks },
710+
});
711+
const server = new Server({ port });
712+
const stopSpy = spyOn(server, "stop");
713+
714+
server.apply(olderCompiler);
715+
716+
await compile(compiler, port);
717+
718+
await new Promise((resolve) => {
719+
compiler.close(resolve);
720+
});
721+
722+
expect(shutdown).toBeDefined();
723+
expect(stopSpy).toHaveBeenCalledTimes(1);
724+
stopSpy.mockRestore();
725+
});
726+
660727
it("should run two independent plugin servers on different child compilers", async () => {
661728
const serverA = new Server({ port: portA });
662729
const serverB = new Server({ port: portB });

0 commit comments

Comments
 (0)