Skip to content

docs: add ipc example #3667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
},
},
});
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!";