Skip to content

Commit 62b21ff

Browse files
authored
fix: show deprecation warning for incorrect usage of Node.js API (#3563)
1 parent 4480a9e commit 62b21ff

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

lib/Server.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const os = require("os");
44
const path = require("path");
55
const url = require("url");
6+
const util = require("util");
67
const fs = require("graceful-fs");
78
const ipaddr = require("ipaddr.js");
89
const internalIp = require("internal-ip");
@@ -18,7 +19,16 @@ if (!process.env.WEBPACK_SERVE) {
1819
class Server {
1920
constructor(options = {}, compiler) {
2021
// TODO: remove this after plugin support is published
22+
2123
if (options.hooks) {
24+
const showDeprecationWarning = util.deprecate(
25+
() => {},
26+
"Using 'compiler' as the first argument is deprecated. Please use 'options' as the first argument and 'compiler' as the second argument.",
27+
"DEP_WEBPACK_DEV_SERVER_API"
28+
);
29+
30+
showDeprecationWarning();
31+
2232
[options, compiler] = [compiler, options];
2333
}
2434

test/server/Server.test.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,17 @@ describe("Server", () => {
9191
});
9292

9393
// TODO: remove this after plugin support is published
94-
it("should create and run server with old parameters order", (done) => {
94+
it("should create and run server with old parameters order and log deprecation warning", (done) => {
9595
const compiler = webpack(config);
96+
const util = require("util");
97+
const utilSpy = jest.spyOn(util, "deprecate");
98+
9699
const server = new Server(compiler, baseDevConfig);
97100

101+
expect(utilSpy.mock.calls[0][1]).toBe(
102+
"Using 'compiler' as the first argument is deprecated. Please use 'options' as the first argument and 'compiler' as the second argument."
103+
);
104+
98105
compiler.hooks.done.tap("webpack-dev-server", () => {
99106
expect(entries).toMatchSnapshot("oldparam");
100107

@@ -108,6 +115,8 @@ describe("Server", () => {
108115

109116
getEntries(server);
110117
});
118+
119+
utilSpy.mockRestore();
111120
});
112121
});
113122

@@ -772,7 +781,7 @@ describe("Server", () => {
772781
port: "9999",
773782
};
774783

775-
server = new Server(compiler, options);
784+
server = new Server(options, compiler);
776785

777786
const warnSpy = jest.fn();
778787

test/server/__snapshots__/Server.test.js.snap.webpack4

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Array [
3636
]
3737
`;
3838

39-
exports[`Server DevServerPlugin should create and run server with old parameters order: oldparam 1`] = `
39+
exports[`Server DevServerPlugin should create and run server with old parameters order and log deprecation warning: oldparam 1`] = `
4040
Array [
4141
Array [
4242
"client",

test/server/__snapshots__/Server.test.js.snap.webpack5

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Array [
3636
]
3737
`;
3838

39-
exports[`Server DevServerPlugin should create and run server with old parameters order: oldparam 1`] = `
39+
exports[`Server DevServerPlugin should create and run server with old parameters order and log deprecation warning: oldparam 1`] = `
4040
Array [
4141
Array [
4242
"client",

0 commit comments

Comments
 (0)