diff --git a/examples/cli/ipc/README.md b/examples/cli/ipc/README.md new file mode 100644 index 0000000000..38f79ee6a8 --- /dev/null +++ b/examples/cli/ipc/README.md @@ -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!`. diff --git a/examples/cli/web-server-socket/app.js b/examples/cli/ipc/app.js similarity index 100% rename from examples/cli/web-server-socket/app.js rename to examples/cli/ipc/app.js diff --git a/examples/cli/ipc/webpack.config.js b/examples/cli/ipc/webpack.config.js new file mode 100644 index 0000000000..9ac3d8bf59 --- /dev/null +++ b/examples/cli/ipc/webpack.config.js @@ -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); + }, + }, +}); diff --git a/examples/cli/web-server-socket/README.md b/examples/cli/web-socket-server/README.md similarity index 100% rename from examples/cli/web-server-socket/README.md rename to examples/cli/web-socket-server/README.md diff --git a/examples/cli/web-socket-server/app.js b/examples/cli/web-socket-server/app.js new file mode 100644 index 0000000000..51cf4a396b --- /dev/null +++ b/examples/cli/web-socket-server/app.js @@ -0,0 +1,6 @@ +"use strict"; + +const target = document.querySelector("#target"); + +target.classList.add("pass"); +target.innerHTML = "Success!"; diff --git a/examples/cli/web-server-socket/webpack.config.js b/examples/cli/web-socket-server/webpack.config.js similarity index 100% rename from examples/cli/web-server-socket/webpack.config.js rename to examples/cli/web-socket-server/webpack.config.js