Skip to content
Merged
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.
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,22 @@ 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) {
filledContext.logger.warn(
"The `close` method was called, but there is no own `watching` instance to close. When using the middleware as a plugin, the host owns watching, so use `compiler.close()` instead.",
);
callback(null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's output a warning here that this usage is not recommended

return;
}

filledContext.watching.close(callback);
};

Expand Down
5 changes: 5 additions & 0 deletions test/__snapshots__/pluginMode.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`plugin mode close method should not close the watching owned by the host: warning 1`] = `"The \`close\` method was called, but there is no own \`watching\` instance to close. When using the middleware as a plugin, the host owns watching, so use \`compiler.close()\` instead."`;

exports[`plugin mode close method should not throw and call the callback when the host is not watching: warning 1`] = `"The \`close\` method was called, but there is no own \`watching\` instance to close. When using the middleware as a plugin, the host owns watching, so use \`compiler.close()\` instead."`;
49 changes: 49 additions & 0 deletions test/pluginMode.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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);
const warnSpy = jest
.spyOn(instance.context.logger, "warn")
.mockImplementation();

instance.close((error) => {
expect(error).toBeNull();
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toMatchSnapshot("warning");

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);
const warnSpy = jest
.spyOn(instance.context.logger, "warn")
.mockImplementation();

instance.waitUntilValid(() => {
instance.close((error) => {
expect(error).toBeNull();
expect(watching.closed).toBe(false);
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toMatchSnapshot("warning");

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