Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show warning when hot is enabled with the HMR plugin in config #3744

Merged
merged 2 commits into from
Aug 25, 2021
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
20 changes: 12 additions & 8 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,16 +1056,20 @@ class Server {
// TODO remove after drop webpack v4 support
compiler.options.plugins = compiler.options.plugins || [];

if (
this.options.hot &&
!compiler.options.plugins.find(
if (this.options.hot) {
const HMRPluginExists = compiler.options.plugins.find(
(p) => p.constructor === webpack.HotModuleReplacementPlugin
)
) {
// apply the HMR plugin, if it didn't exist before.
const plugin = new webpack.HotModuleReplacementPlugin();
);

plugin.apply(compiler);
if (HMRPluginExists) {
this.logger.warn(
`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`
);
} else {
// apply the HMR plugin
const plugin = new webpack.HotModuleReplacementPlugin();
plugin.apply(compiler);
}
}
});

Expand Down
67 changes: 67 additions & 0 deletions test/server/hot-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,73 @@ describe("hot option", () => {
});
});

describe("simple config with already added HMR plugin", () => {
let loggerWarnSpy;
let getInfrastructureLoggerSpy;
let compiler;

beforeEach(() => {
compiler = webpack({
...config,
devServer: { hot: false },
plugins: [...config.plugins, new webpack.HotModuleReplacementPlugin()],
});

loggerWarnSpy = jest.fn();

getInfrastructureLoggerSpy = jest
.spyOn(compiler, "getInfrastructureLogger")
.mockImplementation(() => {
return {
warn: loggerWarnSpy,
info: () => {},
log: () => {},
};
});
});

afterEach(() => {
getInfrastructureLoggerSpy.mockRestore();
loggerWarnSpy.mockRestore();
});

it("should show warning with hot normalized as true", async () => {
server = new Server({ port }, compiler);

await server.start();

expect(loggerWarnSpy).toHaveBeenCalledWith(
`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`
);

await server.stop();
});

it(`should show warning with "hot: true"`, async () => {
server = new Server({ port, hot: true }, compiler);

await server.start();

expect(loggerWarnSpy).toHaveBeenCalledWith(
`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`
);

await server.stop();
});

it(`should show warning with "hot: false"`, async () => {
server = new Server({ port, hot: false }, compiler);

await server.start();

expect(loggerWarnSpy).not.toHaveBeenCalledWith(
`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`
);

await server.stop();
});
});

describe("multi compiler hot config HMR plugin", () => {
it("should register the HMR plugin before compilation is complete", async () => {
let pluginFound = false;
Expand Down