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‎

Lines changed: 53 additions & 0 deletions
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+
constpath=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!`.

‎examples/cli/ipc/webpack.config.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
3+
consthttp=require("http");
4+
consthttpProxy=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+
constproxyPort=8080;
16+
constproxyHost="127.0.0.1";
17+
constproxy=httpProxy.createProxyServer({
18+
target: {socketPath: server.options.ipc},
19+
});
20+
21+
constproxyServer=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+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
consttarget=document.querySelector("#target");
4+
5+
target.classList.add("pass");
6+
target.innerHTML="Success!";
File renamed without changes.

0 commit comments

Comments
(0)