Skip to content

Commit

Permalink
fix: show deprecation warning for incorrect usage of Node.js API (#3563)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Aug 6, 2021
1 parent 4480a9e commit 62b21ff
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/Server.js
Expand Up @@ -3,6 +3,7 @@
const os = require("os");
const path = require("path");
const url = require("url");
const util = require("util");
const fs = require("graceful-fs");
const ipaddr = require("ipaddr.js");
const internalIp = require("internal-ip");
Expand All @@ -18,7 +19,16 @@ if (!process.env.WEBPACK_SERVE) {
class Server {
constructor(options = {}, compiler) {
// TODO: remove this after plugin support is published

if (options.hooks) {
const showDeprecationWarning = util.deprecate(
() => {},
"Using 'compiler' as the first argument is deprecated. Please use 'options' as the first argument and 'compiler' as the second argument.",
"DEP_WEBPACK_DEV_SERVER_API"
);

showDeprecationWarning();

[options, compiler] = [compiler, options];
}

Expand Down
13 changes: 11 additions & 2 deletions test/server/Server.test.js
Expand Up @@ -91,10 +91,17 @@ describe("Server", () => {
});

// TODO: remove this after plugin support is published
it("should create and run server with old parameters order", (done) => {
it("should create and run server with old parameters order and log deprecation warning", (done) => {
const compiler = webpack(config);
const util = require("util");
const utilSpy = jest.spyOn(util, "deprecate");

const server = new Server(compiler, baseDevConfig);

expect(utilSpy.mock.calls[0][1]).toBe(
"Using 'compiler' as the first argument is deprecated. Please use 'options' as the first argument and 'compiler' as the second argument."
);

compiler.hooks.done.tap("webpack-dev-server", () => {
expect(entries).toMatchSnapshot("oldparam");

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

getEntries(server);
});

utilSpy.mockRestore();
});
});

Expand Down Expand Up @@ -772,7 +781,7 @@ describe("Server", () => {
port: "9999",
};

server = new Server(compiler, options);
server = new Server(options, compiler);

const warnSpy = jest.fn();

Expand Down
2 changes: 1 addition & 1 deletion test/server/__snapshots__/Server.test.js.snap.webpack4
Expand Up @@ -36,7 +36,7 @@ Array [
]
`;

exports[`Server DevServerPlugin should create and run server with old parameters order: oldparam 1`] = `
exports[`Server DevServerPlugin should create and run server with old parameters order and log deprecation warning: oldparam 1`] = `
Array [
Array [
"client",
Expand Down
2 changes: 1 addition & 1 deletion test/server/__snapshots__/Server.test.js.snap.webpack5
Expand Up @@ -36,7 +36,7 @@ Array [
]
`;

exports[`Server DevServerPlugin should create and run server with old parameters order: oldparam 1`] = `
exports[`Server DevServerPlugin should create and run server with old parameters order and log deprecation warning: oldparam 1`] = `
Array [
Array [
"client",
Expand Down

0 comments on commit 62b21ff

Please sign in to comment.