Skip to content

Commit 16fcdbc

Browse files
authored
docs: add ipc example (#3667)
1 parent 8915fb8 commit 16fcdbc

File tree

6 files changed

+93
-0
lines changed

6 files changed

+93
-0
lines changed

examples/cli/ipc/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# CLI: IPC
2+
3+
The Unix socket to listen to (instead of a [host](../host-and-port/README.md)).
4+
5+
## true
6+
7+
Setting it to `true` will listen to a socket at `/your-os-temp-dir/webpack-dev-server.sock`:
8+
9+
**webpack.config.js**
10+
11+
```js
12+
module.exports = {
13+
// ...
14+
devServer: {
15+
ipc: true,
16+
},
17+
};
18+
```
19+
20+
Usage via CLI:
21+
22+
```console
23+
npx webpack serve --ipc
24+
```
25+
26+
## string
27+
28+
You can also listen to a different socket with:
29+
30+
**webpack.config.js**
31+
32+
```js
33+
const path = require("path");
34+
35+
module.exports = {
36+
// ...
37+
devServer: {
38+
ipc: path.join(__dirname, "my-socket.sock"),
39+
},
40+
};
41+
```
42+
43+
Usage via CLI:
44+
45+
```console
46+
npx webpack serve --ipc ./my-socket.sock
47+
```
48+
49+
## What Should Happen
50+
51+
1. The script should listen to the socket provided.
52+
1. A proxy server should be created.
53+
1. Go to `http://localhost:8080/`, you should see the text on the page itself change to read `Success!`.
File renamed without changes.

examples/cli/ipc/webpack.config.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
3+
const http = require("http");
4+
const httpProxy = require("http-proxy");
5+
// our setup function adds behind-the-scenes bits to the config that all of our
6+
// examples need
7+
const { setup } = require("../../util");
8+
9+
module.exports = setup({
10+
context: __dirname,
11+
entry: "./app.js",
12+
devServer: {
13+
webSocketServer: "ws",
14+
onAfterSetupMiddleware: (server) => {
15+
const proxyPort = 8080;
16+
const proxyHost = "127.0.0.1";
17+
const proxy = httpProxy.createProxyServer({
18+
target: { socketPath: server.options.ipc },
19+
});
20+
21+
const proxyServer = http.createServer((request, response) => {
22+
// You can define here your custom logic to handle the request
23+
// and then proxy the request.
24+
proxy.web(request, response);
25+
});
26+
27+
proxyServer.on("upgrade", (request, socket, head) => {
28+
proxy.ws(request, socket, head);
29+
});
30+
31+
proxyServer.listen(proxyPort, proxyHost);
32+
},
33+
},
34+
});

examples/cli/web-socket-server/app.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const target = document.querySelector("#target");
4+
5+
target.classList.add("pass");
6+
target.innerHTML = "Success!";

0 commit comments

Comments
 (0)