|
1 | 1 | import fs from "node:fs"; |
| 2 | +import http from "node:http"; |
2 | 3 | import os from "node:os"; |
3 | 4 | import path from "node:path"; |
4 | 5 | import { afterEach, beforeEach, describe, it } from "node:test"; |
@@ -244,6 +245,101 @@ describe("API (plugin)", () => { |
244 | 245 | }); |
245 | 246 | }); |
246 | 247 |
|
| 248 | + it("should trigger a rebuild via `server.invalidate()` in plugin mode", async () => { |
| 249 | + const compiler = webpack(config); |
| 250 | + const server = new Server({ port }); |
| 251 | + server.apply(compiler); |
| 252 | + |
| 253 | + await compile(compiler, port); |
| 254 | + |
| 255 | + const sawInvalid = await new Promise((resolve, reject) => { |
| 256 | + let initialOkSeen = false; |
| 257 | + const ws = new WebSocket(`ws://127.0.0.1:${port}/ws`, { |
| 258 | + headers: { |
| 259 | + host: `127.0.0.1:${port}`, |
| 260 | + origin: `http://127.0.0.1:${port}`, |
| 261 | + }, |
| 262 | + }); |
| 263 | + |
| 264 | + ws.on("error", reject); |
| 265 | + ws.on("message", (raw) => { |
| 266 | + const { type } = JSON.parse(raw.toString()); |
| 267 | + |
| 268 | + if (!initialOkSeen && type === "ok") { |
| 269 | + initialOkSeen = true; |
| 270 | + // Must invalidate the host's `watching` (the middleware has none in |
| 271 | + // plugin mode) instead of throwing. |
| 272 | + server.invalidate(); |
| 273 | + return; |
| 274 | + } |
| 275 | + |
| 276 | + if (type === "invalid") { |
| 277 | + ws.close(); |
| 278 | + resolve(true); |
| 279 | + } |
| 280 | + }); |
| 281 | + }); |
| 282 | + |
| 283 | + expect(sawInvalid).toBe(true); |
| 284 | + |
| 285 | + await new Promise((resolve) => { |
| 286 | + compiler.close(resolve); |
| 287 | + }); |
| 288 | + }); |
| 289 | + |
| 290 | + it("should trigger a rebuild via the /webpack-dev-server/invalidate route in plugin mode", async () => { |
| 291 | + const compiler = webpack(config); |
| 292 | + const server = new Server({ port }); |
| 293 | + server.apply(compiler); |
| 294 | + |
| 295 | + await compile(compiler, port); |
| 296 | + |
| 297 | + const sawInvalid = await new Promise((resolve, reject) => { |
| 298 | + let initialOkSeen = false; |
| 299 | + const ws = new WebSocket(`ws://127.0.0.1:${port}/ws`, { |
| 300 | + headers: { |
| 301 | + host: `127.0.0.1:${port}`, |
| 302 | + origin: `http://127.0.0.1:${port}`, |
| 303 | + }, |
| 304 | + }); |
| 305 | + |
| 306 | + ws.on("error", reject); |
| 307 | + ws.on("message", (raw) => { |
| 308 | + const { type } = JSON.parse(raw.toString()); |
| 309 | + |
| 310 | + if (!initialOkSeen && type === "ok") { |
| 311 | + initialOkSeen = true; |
| 312 | + // Hit the route as a browser would (same-origin, so it passes the |
| 313 | + // cross-origin check) — it must trigger a rebuild, not crash. |
| 314 | + http |
| 315 | + .get( |
| 316 | + `http://127.0.0.1:${port}/webpack-dev-server/invalidate`, |
| 317 | + { |
| 318 | + headers: { |
| 319 | + host: `127.0.0.1:${port}`, |
| 320 | + origin: `http://127.0.0.1:${port}`, |
| 321 | + }, |
| 322 | + }, |
| 323 | + (res) => res.resume(), |
| 324 | + ) |
| 325 | + .on("error", reject); |
| 326 | + return; |
| 327 | + } |
| 328 | + |
| 329 | + if (type === "invalid") { |
| 330 | + ws.close(); |
| 331 | + resolve(true); |
| 332 | + } |
| 333 | + }); |
| 334 | + }); |
| 335 | + |
| 336 | + expect(sawInvalid).toBe(true); |
| 337 | + |
| 338 | + await new Promise((resolve) => { |
| 339 | + compiler.close(resolve); |
| 340 | + }); |
| 341 | + }); |
| 342 | + |
247 | 343 | it("should use constructor options instead of compiler.options.devServer", async () => { |
248 | 344 | // Plugin reads its options from its constructor argument; values on |
249 | 345 | // `compiler.options.devServer` are intentionally ignored. This protects |
|
0 commit comments