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 deprecation warning for incorrect usage of Node.js API #3563

Merged
merged 5 commits into from
Aug 6, 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
10 changes: 10 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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