Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 16 additions & 12 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3660,19 +3660,23 @@ class Server {
);

if (this.middleware) {
await /** @type {Promise<void>} */ (
new Promise((resolve, reject) => {
/** @type {import("webpack-dev-middleware").API<Request, Response>} */
(this.middleware).close((error) => {
if (error) {
reject(error);
return;
}
// In plugin mode the middleware has no `watching` of its own to close
// (the host's `compiler.close()` handles it).
if (!this.isPlugin) {
await /** @type {Promise<void>} */ (
new Promise((resolve, reject) => {
/** @type {import("webpack-dev-middleware").API<Request, Response>} */
(this.middleware).close((error) => {
if (error) {
reject(error);
return;
}

resolve();
});
})
);
resolve();
});
})
);
}

this.middleware = undefined;
}
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/api-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ describe("API (plugin)", () => {
stopSpy.mockRestore();
});

it("should tear down without error when the compiler is closed", async () => {
// In plugin mode the host owns `watching`, so `stop()` must not call
// `middleware.close()` — otherwise `compiler.close` surfaces a TypeError.
const compiler = webpack(config);
const server = new Server({ port });

server.apply(compiler);

await compile(compiler, port);

const closeError = await new Promise((resolve) => {
compiler.close((error) => resolve(error));
});

expect(closeError).toBeFalsy();
});

it("should stay passive in build mode (compiler.run)", async () => {
// The shared fixture writes output to "/", which would be unwritable
// outside of webpack-dev-middleware's in-memory FS. Use a tmp dir so the
Expand Down
Loading