Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/plugin-mode-close.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webpack-dev-middleware": patch
---

Fixed a crash when calling `close()` in plugin mode (`isPlugin = true`). Since the host (webpack-cli, webpack-dev-server, etc.) owns `compiler.watch()`, the middleware has no `watching` of its own to close, so `close()` now just calls the callback instead of throwing.
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,19 @@ function wdm(compiler, options = {}, isPlugin = false) {
instance.invalidate = (callback = noop) => {
middleware.ready(filledContext, callback);

// TODO for plugin usage (`isPlugin = true`) `watching` is `undefined` and this throws —
// invalidate the host's `compiler.watching` (each child's one for a `MultiCompiler`) instead
filledContext.watching.invalidate();
};

instance.close = (callback = noop) => {
// For plugin usage the host (webpack-cli, webpack-dev-server, etc.) owns `compiler.watch()`,
// so there is no `watching` of our own to close (`compiler.close()` on the host handles it)
if (!filledContext.watching) {
callback(null);
return;
}

filledContext.watching.close(callback);
};

Expand Down
39 changes: 39 additions & 0 deletions test/pluginMode.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import middleware from "../src";

import webpackConfig from "./fixtures/webpack.config";
import getCompiler from "./helpers/getCompiler";

jest.spyOn(globalThis.console, "log").mockImplementation();

// When used as a plugin (`isPlugin = true`) the host (webpack-cli,
// webpack-dev-server, etc.) owns `compiler.watch()`, so the middleware has no
// `watching` of its own
describe("plugin mode", () => {
describe("close method", () => {
it("should not throw and call the callback when the host is not watching", (done) => {
const compiler = getCompiler(webpackConfig);
const instance = middleware(compiler, {}, true);

instance.close((error) => {
expect(error).toBeNull();

done();
});
});

it("should not close the watching owned by the host", (done) => {
const compiler = getCompiler(webpackConfig);
const watching = compiler.watch({}, () => {});
const instance = middleware(compiler, {}, true);

instance.waitUntilValid(() => {
instance.close((error) => {
expect(error).toBeNull();
expect(watching.closed).toBe(false);

watching.close(done);
});
});
});
});
});
Loading