Skip to content

Commit 8664549

Browse files
committed
feat: enhance plugin API support and update tests for new compile behavior
1 parent 0fdb03f commit 8664549

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

test/e2e/__snapshots__/api.test.js.snap.webpack5

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,13 @@ exports[`API latest async API should work with callback API: console messages 1`
165165
`;
166166

167167
exports[`API latest async API should work with callback API: page errors 1`] = `[]`;
168+
169+
exports[`API should work with plugin API: console messages 1`] = `
170+
[
171+
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
172+
"[HMR] Waiting for update signal from WDS...",
173+
"Hey.",
174+
]
175+
`;
176+
177+
exports[`API should work with plugin API: page errors 1`] = `[]`;

test/e2e/api.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ describe("API", () => {
1515
const server = new Server({ port });
1616

1717
server.apply(compiler);
18-
await compile(compiler);
18+
19+
// Use compile helper which waits for the server to be ready
20+
const { watching } = await compile(compiler, port);
1921

2022
const { page, browser } = await runBrowser();
2123

@@ -40,7 +42,7 @@ describe("API", () => {
4042
expect(pageErrors).toMatchSnapshot("page errors");
4143

4244
await browser.close();
43-
compiler.watching.close();
45+
watching.close();
4446
});
4547

4648
describe("WEBPACK_SERVE environment variable", () => {

test/helpers/compile.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
"use strict";
22

3-
module.exports = (compiler) =>
3+
// Helper function to check if server is ready using fetch
4+
const waitForServer = async (port, timeout = 10000) => {
5+
const start = Date.now();
6+
7+
while (Date.now() - start < timeout) {
8+
try {
9+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
10+
await fetch(`http://127.0.0.1:${port}/`);
11+
return; // Server is ready
12+
} catch {
13+
// Server not ready yet, wait and retry
14+
await new Promise((resolve) => {
15+
setTimeout(resolve, 100);
16+
});
17+
}
18+
}
19+
20+
throw new Error(`Server on port ${port} not ready after ${timeout}ms`);
21+
};
22+
23+
module.exports = (compiler, port = null) =>
424
new Promise((resolve, reject) => {
5-
compiler.run((error, stats) => {
25+
const watching = compiler.watch({}, async (error, stats) => {
626
if (error) {
27+
watching.close();
728
return reject(error);
829
}
930

10-
return resolve(stats);
31+
// If a port is provided, wait for the server to be ready
32+
if (port) {
33+
try {
34+
await waitForServer(port);
35+
} catch (err) {
36+
watching.close();
37+
return reject(err);
38+
}
39+
}
40+
41+
// Return both stats and watching for caller to manage
42+
resolve({ stats, watching });
1143
});
1244
});

0 commit comments

Comments
 (0)