Skip to content

Commit cff074e

Browse files
committed
fix: correct errors when a compiler is not passed to the constructor
1 parent 3ea59f3 commit cff074e

3 files changed

Lines changed: 65 additions & 7 deletions

File tree

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[]}
@@ -1587,6 +1590,9 @@ class Server {
15871590
* @returns {void}
15881591
*/
15891592
setupProgressPlugin() {
1593+
// In the case where there is no compiler and it’s not being used as a plugin.
1594+
if (this.compiler === undefined) return;
1595+
15901596
const { ProgressPlugin } =
15911597
/** @type {MultiCompiler} */
15921598
(this.compiler).compilers
@@ -1631,6 +1637,7 @@ class Server {
16311637
* @returns {Promise<void>}
16321638
*/
16331639
async initialize() {
1640+
if (this.compiler === undefined) return;
16341641
this.setupHooks();
16351642

16361643
await this.setupApp();
@@ -1706,7 +1713,7 @@ class Server {
17061713
needForceShutdown = true;
17071714

17081715
this.stopCallback(() => {
1709-
if (typeof this.compiler.close === "function") {
1716+
if (typeof this.compiler?.close === "function") {
17101717
this.compiler.close(() => {
17111718
// eslint-disable-next-line n/no-process-exit
17121719
process.exit();
@@ -1781,11 +1788,14 @@ class Server {
17811788
* @returns {void}
17821789
*/
17831790
setupHooks() {
1791+
if (this.compiler === undefined) return;
1792+
17841793
this.compiler.hooks.invalid.tap("webpack-dev-server", () => {
17851794
if (this.webSocketServer) {
17861795
this.sendMessage(this.webSocketServer.clients, "invalid");
17871796
}
17881797
});
1798+
17891799
this.compiler.hooks.done.tap(
17901800
"webpack-dev-server",
17911801
/**
@@ -1840,6 +1850,7 @@ class Server {
18401850
* @returns {void}
18411851
*/
18421852
setupMiddlewares() {
1853+
if (this.compiler === undefined) return;
18431854
/**
18441855
* @type {Array<Middleware>}
18451856
*/
@@ -2331,6 +2342,7 @@ class Server {
23312342
// middleware for serving webpack bundle
23322343
/** @type {import("webpack-dev-middleware").API<Request, Response>} */
23332344
this.middleware = webpackDevMiddleware(
2345+
// @ts-expect-error
23342346
this.compiler,
23352347
this.options.devMiddleware,
23362348
);
@@ -3437,8 +3449,8 @@ class Server {
34373449
* @returns {void}
34383450
*/
34393451
apply(compiler) {
3440-
const pluginName = this.constructor.name;
34413452
this.compiler = compiler;
3453+
this.logger = this.compiler.getInfrastructureLogger(pluginName);
34423454

34433455
this.compiler.hooks.watchRun.tapPromise(pluginName, async () => {
34443456
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)