Skip to content

Commit

Permalink
feat: async API (#3608)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 12, 2021
1 parent f0a7c31 commit 974ce25
Show file tree
Hide file tree
Showing 49 changed files with 1,483 additions and 4,495 deletions.
392 changes: 212 additions & 180 deletions lib/Server.js

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions migration-v4.md
Expand Up @@ -491,6 +491,106 @@ module.exports = {
- The `sockWrite` public method was renamed to `sendMessage`.
- The `profile` option was removed in favor [`ProfilingPlugin`](https://webpack.js.org/plugins/profiling-plugin/).

### Deprecations

- `constructor` arguments were changed, the first argument is dev server options, the second is compiler

v3:

```js
const devServerOptions = { host: "127.0.0.1", port: 8080 };
const devServer = new Server(compiler, devServerOptions);
```

v4:

```js
const devServerOptions = { host: "127.0.0.1", port: 8080 };
const devServer = new Server(devServerOptions, compiler);
```

- `listen` method is deprecated in favor async `start` or `startCallback` methods

v3:

```js
const devServerOptions = { host: "127.0.0.1", port: 8080 };
const devServer = new Server(compiler, devServerOptions);

devServer.listen(devServerOptions.host, devServerOptions.port, () => {
console.log("Running");
});
```

v4:

```js
const devServerOptions = { host: "127.0.0.1", port: 8080 };
const devServer = new Server(compiler, devServerOptions);

(async () => {
await devServer.start();

console.log("Running");
})();
```

```js
const devServerOptions = { host: "127.0.0.1", port: 8080 };
const devServer = new Server(compiler, devServerOptions);

devServer.startCallback(() => {
console.log("Running");
});
```

- `close` method is deprecated in favor async `stop` or `stopCallback` methods

v3:

```js
const devServerOptions = { host: "127.0.0.1", port: 8080 };
const devServer = new Server(compiler, devServerOptions);

devServer.listen(devServerOptions.host, devServerOptions.port, () => {
console.log("Running");

devServer.close(() => {
console.log("Closed");
});
});
```

v4:

```js
const devServerOptions = { host: "127.0.0.1", port: 8080 };
const devServer = new Server(compiler, devServerOptions);

(async () => {
await devServer.start();

console.log("Running");

await devServer.stop();

console.log("Closed");
})();
```

```js
const devServerOptions = { host: "127.0.0.1", port: 8080 };
const devServer = new Server(compiler, devServerOptions);

devServer.startCallback(() => {
console.log("Running");

devServer.stopCallback(() => {
console.log("Closed");
});
});
```

### Features

- 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`.
Expand All @@ -503,6 +603,8 @@ module.exports = {
- Overlay can be closed in browser.
- The `allowedHosts` option can be `auto` or custom string with your domain (i.e. default value).
- The `static` option can be disabled using `static: false`.
- Added async `start` and `stop` methods to API
- Added `startCallback` and `stopCallback` methods to API

### Bug Fixes

Expand Down
18 changes: 2 additions & 16 deletions test/client/bundle.test.js
Expand Up @@ -21,27 +21,13 @@ describe("bundle", () => {

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

await new Promise((resolve, reject) => {
server.listen(port, "127.0.0.1", (error) => {
if (error) {
reject(error);

return;
}

resolve();
});
});
await server.start();

req = request(server.app);
});

afterAll(async () => {
await new Promise((resolve) => {
server.close(() => {
resolve();
});
});
await server.stop();
});

it("should get full user bundle and parse with ES5", async () => {
Expand Down
34 changes: 34 additions & 0 deletions test/e2e/__snapshots__/api.test.js.snap.webpack4
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`API should work with async API: console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`API should work with async API: page errors 1`] = `Array []`;

exports[`API should work with callback API: console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`API should work with callback API: page errors 1`] = `Array []`;

exports[`API should work with deprecated API: console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`API should work with deprecated API: page errors 1`] = `Array []`;
34 changes: 34 additions & 0 deletions test/e2e/__snapshots__/api.test.js.snap.webpack5
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`API should work with async API: console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`API should work with async API: page errors 1`] = `Array []`;

exports[`API should work with callback API: console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`API should work with callback API: page errors 1`] = `Array []`;

exports[`API should work with deprecated API: console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`API should work with deprecated API: page errors 1`] = `Array []`;

0 comments on commit 974ce25

Please sign in to comment.