Skip to content

Commit 974ce25

Browse files
feat: async API (#3608)
1 parent f0a7c31 commit 974ce25

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1483
-4495
lines changed

lib/Server.js

+212-180
Large diffs are not rendered by default.

migration-v4.md

+102
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,106 @@ module.exports = {
491491
- The `sockWrite` public method was renamed to `sendMessage`.
492492
- The `profile` option was removed in favor [`ProfilingPlugin`](https://webpack.js.org/plugins/profiling-plugin/).
493493

494+
### Deprecations
495+
496+
- `constructor` arguments were changed, the first argument is dev server options, the second is compiler
497+
498+
v3:
499+
500+
```js
501+
const devServerOptions = { host: "127.0.0.1", port: 8080 };
502+
const devServer = new Server(compiler, devServerOptions);
503+
```
504+
505+
v4:
506+
507+
```js
508+
const devServerOptions = { host: "127.0.0.1", port: 8080 };
509+
const devServer = new Server(devServerOptions, compiler);
510+
```
511+
512+
- `listen` method is deprecated in favor async `start` or `startCallback` methods
513+
514+
v3:
515+
516+
```js
517+
const devServerOptions = { host: "127.0.0.1", port: 8080 };
518+
const devServer = new Server(compiler, devServerOptions);
519+
520+
devServer.listen(devServerOptions.host, devServerOptions.port, () => {
521+
console.log("Running");
522+
});
523+
```
524+
525+
v4:
526+
527+
```js
528+
const devServerOptions = { host: "127.0.0.1", port: 8080 };
529+
const devServer = new Server(compiler, devServerOptions);
530+
531+
(async () => {
532+
await devServer.start();
533+
534+
console.log("Running");
535+
})();
536+
```
537+
538+
```js
539+
const devServerOptions = { host: "127.0.0.1", port: 8080 };
540+
const devServer = new Server(compiler, devServerOptions);
541+
542+
devServer.startCallback(() => {
543+
console.log("Running");
544+
});
545+
```
546+
547+
- `close` method is deprecated in favor async `stop` or `stopCallback` methods
548+
549+
v3:
550+
551+
```js
552+
const devServerOptions = { host: "127.0.0.1", port: 8080 };
553+
const devServer = new Server(compiler, devServerOptions);
554+
555+
devServer.listen(devServerOptions.host, devServerOptions.port, () => {
556+
console.log("Running");
557+
558+
devServer.close(() => {
559+
console.log("Closed");
560+
});
561+
});
562+
```
563+
564+
v4:
565+
566+
```js
567+
const devServerOptions = { host: "127.0.0.1", port: 8080 };
568+
const devServer = new Server(compiler, devServerOptions);
569+
570+
(async () => {
571+
await devServer.start();
572+
573+
console.log("Running");
574+
575+
await devServer.stop();
576+
577+
console.log("Closed");
578+
})();
579+
```
580+
581+
```js
582+
const devServerOptions = { host: "127.0.0.1", port: 8080 };
583+
const devServer = new Server(compiler, devServerOptions);
584+
585+
devServer.startCallback(() => {
586+
console.log("Running");
587+
588+
devServer.stopCallback(() => {
589+
console.log("Closed");
590+
});
591+
});
592+
```
593+
494594
### Features
495595

496596
- Added the `setupExitSignals` option, it takes a boolean and if `true` (default on CLI), the server will close and exit the process on `SIGINT` and `SIGTERM`.
@@ -503,6 +603,8 @@ module.exports = {
503603
- Overlay can be closed in browser.
504604
- The `allowedHosts` option can be `auto` or custom string with your domain (i.e. default value).
505605
- The `static` option can be disabled using `static: false`.
606+
- Added async `start` and `stop` methods to API
607+
- Added `startCallback` and `stopCallback` methods to API
506608

507609
### Bug Fixes
508610

test/client/bundle.test.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,13 @@ describe("bundle", () => {
2121

2222
server = new Server({ port }, compiler);
2323

24-
await new Promise((resolve, reject) => {
25-
server.listen(port, "127.0.0.1", (error) => {
26-
if (error) {
27-
reject(error);
28-
29-
return;
30-
}
31-
32-
resolve();
33-
});
34-
});
24+
await server.start();
3525

3626
req = request(server.app);
3727
});
3828

3929
afterAll(async () => {
40-
await new Promise((resolve) => {
41-
server.close(() => {
42-
resolve();
43-
});
44-
});
30+
await server.stop();
4531
});
4632

4733
it("should get full user bundle and parse with ES5", async () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`API should work with async API: console messages 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"Hey.",
7+
"[webpack-dev-server] Hot Module Replacement enabled.",
8+
"[webpack-dev-server] Live Reloading enabled.",
9+
]
10+
`;
11+
12+
exports[`API should work with async API: page errors 1`] = `Array []`;
13+
14+
exports[`API should work with callback API: console messages 1`] = `
15+
Array [
16+
"[HMR] Waiting for update signal from WDS...",
17+
"Hey.",
18+
"[webpack-dev-server] Hot Module Replacement enabled.",
19+
"[webpack-dev-server] Live Reloading enabled.",
20+
]
21+
`;
22+
23+
exports[`API should work with callback API: page errors 1`] = `Array []`;
24+
25+
exports[`API should work with deprecated API: console messages 1`] = `
26+
Array [
27+
"[HMR] Waiting for update signal from WDS...",
28+
"Hey.",
29+
"[webpack-dev-server] Hot Module Replacement enabled.",
30+
"[webpack-dev-server] Live Reloading enabled.",
31+
]
32+
`;
33+
34+
exports[`API should work with deprecated API: page errors 1`] = `Array []`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`API should work with async API: console messages 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"Hey.",
7+
"[webpack-dev-server] Hot Module Replacement enabled.",
8+
"[webpack-dev-server] Live Reloading enabled.",
9+
]
10+
`;
11+
12+
exports[`API should work with async API: page errors 1`] = `Array []`;
13+
14+
exports[`API should work with callback API: console messages 1`] = `
15+
Array [
16+
"[HMR] Waiting for update signal from WDS...",
17+
"Hey.",
18+
"[webpack-dev-server] Hot Module Replacement enabled.",
19+
"[webpack-dev-server] Live Reloading enabled.",
20+
]
21+
`;
22+
23+
exports[`API should work with callback API: page errors 1`] = `Array []`;
24+
25+
exports[`API should work with deprecated API: console messages 1`] = `
26+
Array [
27+
"[HMR] Waiting for update signal from WDS...",
28+
"Hey.",
29+
"[webpack-dev-server] Hot Module Replacement enabled.",
30+
"[webpack-dev-server] Live Reloading enabled.",
31+
]
32+
`;
33+
34+
exports[`API should work with deprecated API: page errors 1`] = `Array []`;

0 commit comments

Comments
 (0)