Skip to content

Commit

Permalink
docs: add ipc example (#3667)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Aug 18, 2021
1 parent 8915fb8 commit 16fcdbc
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
53 changes: 53 additions & 0 deletions examples/cli/ipc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# CLI: IPC

The Unix socket to listen to (instead of a [host](../host-and-port/README.md)).

## true

Setting it to `true` will listen to a socket at `/your-os-temp-dir/webpack-dev-server.sock`:

**webpack.config.js**

```js
module.exports = {
// ...
devServer: {
ipc: true,
},
};
```

Usage via CLI:

```console
npx webpack serve --ipc
```

## string

You can also listen to a different socket with:

**webpack.config.js**

```js
const path = require("path");

module.exports = {
// ...
devServer: {
ipc: path.join(__dirname, "my-socket.sock"),
},
};
```

Usage via CLI:

```console
npx webpack serve --ipc ./my-socket.sock
```

## What Should Happen

1. The script should listen to the socket provided.
1. A proxy server should be created.
1. Go to `http://localhost:8080/`, you should see the text on the page itself change to read `Success!`.
File renamed without changes.
34 changes: 34 additions & 0 deletions examples/cli/ipc/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict";

const http = require("http");
const httpProxy = require("http-proxy");
// our setup function adds behind-the-scenes bits to the config that all of our
// examples need
const { setup } = require("../../util");

module.exports = setup({
context: __dirname,
entry: "./app.js",
devServer: {
webSocketServer: "ws",
onAfterSetupMiddleware: (server) => {
const proxyPort = 8080;
const proxyHost = "127.0.0.1";
const proxy = httpProxy.createProxyServer({
target: { socketPath: server.options.ipc },
});

const proxyServer = http.createServer((request, response) => {
// You can define here your custom logic to handle the request
// and then proxy the request.
proxy.web(request, response);
});

proxyServer.on("upgrade", (request, socket, head) => {
proxy.ws(request, socket, head);
});

proxyServer.listen(proxyPort, proxyHost);
},
},
});
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/cli/web-socket-server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

const target = document.querySelector("#target");

target.classList.add("pass");
target.innerHTML = "Success!";

0 comments on commit 16fcdbc

Please sign in to comment.