Skip to content

Commit 1ece81c

Browse files
committed
fix: correct errors when a compiler is not passed to the constructor
1 parent e823546 commit 1ece81c

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

lib/Server.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,14 @@ class Server {
328328
baseDataPath: "options",
329329
});
330330

331-
this.compiler = compiler;
332-
/**
333-
* @type {ReturnType<Compiler["getInfrastructureLogger"]>}
334-
*/
335-
this.logger = this.compiler.getInfrastructureLogger(pluginName);
331+
if (compiler) {
332+
this.compiler = compiler;
333+
334+
/**
335+
* @type {ReturnType<Compiler["getInfrastructureLogger"]>}
336+
*/
337+
this.logger = this.compiler.getInfrastructureLogger(pluginName);
338+
}
336339
this.options = options;
337340
/**
338341
* @type {FSWatcher[]}
@@ -1589,6 +1592,9 @@ class Server {
15891592
* @returns {void}
15901593
*/
15911594
setupProgressPlugin() {
1595+
// In the case where there is no compiler and it’s not being used as a plugin.
1596+
if (this.compiler === undefined) return;
1597+
15921598
const { ProgressPlugin } =
15931599
/** @type {MultiCompiler} */
15941600
(this.compiler).compilers
@@ -1633,6 +1639,7 @@ class Server {
16331639
* @returns {Promise<void>}
16341640
*/
16351641
async initialize() {
1642+
if (this.compiler === undefined) return;
16361643
this.setupHooks();
16371644

16381645
await this.setupApp();
@@ -1708,7 +1715,7 @@ class Server {
17081715
needForceShutdown = true;
17091716

17101717
this.stopCallback(() => {
1711-
if (typeof this.compiler.close === "function") {
1718+
if (typeof this.compiler?.close === "function") {
17121719
this.compiler.close(() => {
17131720
// eslint-disable-next-line n/no-process-exit
17141721
process.exit();
@@ -1783,11 +1790,14 @@ class Server {
17831790
* @returns {void}
17841791
*/
17851792
setupHooks() {
1793+
if (this.compiler === undefined) return;
1794+
17861795
this.compiler.hooks.invalid.tap("webpack-dev-server", () => {
17871796
if (this.webSocketServer) {
17881797
this.sendMessage(this.webSocketServer.clients, "invalid");
17891798
}
17901799
});
1800+
17911801
this.compiler.hooks.done.tap(
17921802
"webpack-dev-server",
17931803
/**
@@ -1842,6 +1852,7 @@ class Server {
18421852
* @returns {void}
18431853
*/
18441854
setupMiddlewares() {
1855+
if (this.compiler === undefined) return;
18451856
/**
18461857
* @type {Array<Middleware>}
18471858
*/
@@ -2333,6 +2344,7 @@ class Server {
23332344
// middleware for serving webpack bundle
23342345
/** @type {import("webpack-dev-middleware").API<Request, Response>} */
23352346
this.middleware = webpackDevMiddleware(
2347+
// @ts-expect-error
23362348
this.compiler,
23372349
this.options.devMiddleware,
23382350
);
@@ -3407,8 +3419,8 @@ class Server {
34073419
* @returns {void}
34083420
*/
34093421
apply(compiler) {
3410-
const pluginName = this.constructor.name;
34113422
this.compiler = compiler;
3423+
this.logger = this.compiler.getInfrastructureLogger(pluginName);
34123424

34133425
this.compiler.hooks.watchRun.tapPromise(pluginName, async () => {
34143426
await this.start();

test/e2e/api.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,45 @@ const path = require("node:path");
44
const webpack = require("webpack");
55
const Server = require("../../lib/Server");
66
const config = require("../fixtures/client-config/webpack.config");
7+
const compile = require("../helpers/compile");
78
const runBrowser = require("../helpers/run-browser");
89
const sessionSubscribe = require("../helpers/session-subscribe");
910
const port = require("../ports-map").api;
1011

1112
describe("API", () => {
13+
it("should work with plugin API", async () => {
14+
const compiler = webpack(config);
15+
const server = new Server({ port });
16+
17+
server.apply(compiler);
18+
await compile(compiler);
19+
20+
const { page, browser } = await runBrowser();
21+
22+
const pageErrors = [];
23+
const consoleMessages = [];
24+
25+
page
26+
.on("console", (message) => {
27+
consoleMessages.push(message);
28+
})
29+
.on("pageerror", (error) => {
30+
pageErrors.push(error);
31+
});
32+
33+
await page.goto(`http://127.0.0.1:${port}/`, {
34+
waitUntil: "networkidle0",
35+
});
36+
37+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
38+
"console messages",
39+
);
40+
expect(pageErrors).toMatchSnapshot("page errors");
41+
42+
await browser.close();
43+
compiler.watching.close();
44+
});
45+
1246
describe("WEBPACK_SERVE environment variable", () => {
1347
const OLD_ENV = process.env;
1448
let server;

test/helpers/compile.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
module.exports = (compiler) =>
4+
new Promise((resolve, reject) => {
5+
compiler.run((error, stats) => {
6+
if (error) {
7+
return reject(error);
8+
}
9+
10+
return resolve(stats);
11+
});
12+
});

0 commit comments

Comments
 (0)