Skip to content

Commit 308e853

Browse files
authored
fix: handle undefined options in Server constructor (#5695)
* fix: handle undefined options in Server constructor and update type definitions * fix: handle undefined options in Server constructor and add schema validation for null options
1 parent 8b2b915 commit 308e853

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"webpack-dev-server": patch
3+
---
4+
5+
fix: allow `undefined` as the `Server` constructor `options` argument again
6+
7+
Restores accepting `undefined` (defaulting it to `{}`) for the `options`
8+
argument, so passing a webpack config's optional `devServer` field type-checks and works as before.

lib/Server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,12 @@ const DEFAULT_ALLOWED_PROTOCOLS = /^(file|.+-extension):/i;
325325
*/
326326
class Server {
327327
/**
328-
* @param {Configuration<A, S>} options options
328+
* @param {Configuration<A, S> | undefined} options options
329329
* @param {Compiler | MultiCompiler} compiler compiler
330330
*/
331331
constructor(options, compiler) {
332+
options = options === undefined ? {} : options;
333+
332334
validate(/** @type {Schema} */ (schema), options, {
333335
name: "Dev Server",
334336
baseDataPath: "options",

test/e2e/api.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ describe("API", () => {
7171
});
7272
});
7373

74+
describe("constructor options", () => {
75+
it("should default `undefined` options to `{}`", async () => {
76+
const compiler = webpack(config);
77+
const server = new Server(undefined, compiler);
78+
79+
expect(server.options).toEqual({});
80+
81+
try {
82+
await server.start();
83+
} finally {
84+
await server.stop();
85+
}
86+
});
87+
88+
it("should reject `null` options via schema validation", () => {
89+
const compiler = webpack(config);
90+
91+
expect(() => new Server(null, compiler)).toThrow(
92+
/Invalid options object/,
93+
);
94+
});
95+
});
96+
7497
describe("latest async API", () => {
7598
it("should work with async API", async () => {
7699
const compiler = webpack(config);

types/lib/Server.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,10 +1173,13 @@ declare class Server<
11731173
*/
11741174
private static isWebTarget;
11751175
/**
1176-
* @param {Configuration<A, S>} options options
1176+
* @param {Configuration<A, S> | undefined} options options
11771177
* @param {Compiler | MultiCompiler} compiler compiler
11781178
*/
1179-
constructor(options: Configuration<A, S>, compiler: Compiler | MultiCompiler);
1179+
constructor(
1180+
options: Configuration<A, S> | undefined,
1181+
compiler: Compiler | MultiCompiler,
1182+
);
11801183
compiler: import("webpack").Compiler | import("webpack").MultiCompiler;
11811184
/**
11821185
* @type {ReturnType<Compiler["getInfrastructureLogger"]>}

0 commit comments

Comments
 (0)