|
3 | 3 | var path = require("path");
|
4 | 4 | var open = require("opn");
|
5 | 5 | var fs = require("fs");
|
| 6 | +var net = require("net"); |
6 | 7 | var url = require("url");
|
7 | 8 |
|
8 | 9 | // Local version replaces global one
|
@@ -130,6 +131,11 @@ yargs.options({
|
130 | 131 | default: 8080,
|
131 | 132 | group: CONNECTION_GROUP
|
132 | 133 | },
|
| 134 | + "socket": { |
| 135 | + type: "String", |
| 136 | + describe: "Socket to listen", |
| 137 | + group: CONNECTION_GROUP |
| 138 | + }, |
133 | 139 | "public": {
|
134 | 140 | type: "string",
|
135 | 141 | describe: "The public hostname/ip address of the server",
|
@@ -172,6 +178,9 @@ function processOptions(wpOpt) {
|
172 | 178 | if(argv.port !== 8080 || !options.port)
|
173 | 179 | options.port = argv.port;
|
174 | 180 |
|
| 181 | + if(argv.socket) |
| 182 | + options.socket = argv.socket; |
| 183 | + |
175 | 184 | if(!options.publicPath) {
|
176 | 185 | options.publicPath = firstWpOpt.output && firstWpOpt.output.publicPath || "";
|
177 | 186 | if(!/^(https?:)?\/\//.test(options.publicPath) && options.publicPath[0] !== "/")
|
@@ -299,27 +308,65 @@ function processOptions(wpOpt) {
|
299 | 308 | }));
|
300 | 309 | }
|
301 | 310 |
|
302 |
| - new Server(compiler, options).listen(options.port, options.host, function(err) { |
303 |
| - if(err) throw err; |
| 311 | + var uri = url.format({ |
| 312 | + protocol: protocol, |
| 313 | + hostname: options.host, |
| 314 | + pathname: options.inline !== false ? "/" : "webpack-dev-server/", |
| 315 | + port: options.socket ? 0 : options.port.toString() |
| 316 | + }); |
304 | 317 |
|
305 |
| - var uri = url.format({ |
306 |
| - protocol: protocol, |
307 |
| - hostname: options.host, |
308 |
| - port: options.port.toString(), |
309 |
| - pathname: options.inline !== false ? "/" : "webpack-dev-server/" |
| 318 | + var server = new Server(compiler, options); |
| 319 | + |
| 320 | + if(options.socket) { |
| 321 | + server.listeningApp.on("error", function(e) { |
| 322 | + if(e.code === "EADDRINUSE") { |
| 323 | + var clientSocket = new net.Socket(); |
| 324 | + clientSocket.on("error", function(e) { |
| 325 | + if(e.code === "ECONNREFUSED") { |
| 326 | + // No other server listening on this socket so it can be safely removed |
| 327 | + fs.unlinkSync(options.socket); |
| 328 | + server.listen(options.socket, options.host, function(err) { |
| 329 | + if(err) throw err; |
| 330 | + }); |
| 331 | + } |
| 332 | + }); |
| 333 | + clientSocket.connect({ path: options.socket }, function() { |
| 334 | + throw new Error("This socket is already used"); |
| 335 | + }); |
| 336 | + } |
310 | 337 | });
|
| 338 | + server.listen(options.socket, options.host, function(err) { |
| 339 | + if(err) throw err; |
| 340 | + var READ_WRITE = 438; // chmod 666 (rw rw rw) |
| 341 | + fs.chmod(options.socket, READ_WRITE, function(err) { |
| 342 | + if(err) throw err; |
| 343 | + reportReadiness(uri, options); |
| 344 | + }); |
| 345 | + }); |
| 346 | + } else { |
| 347 | + server.listen(options.port, options.host, function(err) { |
| 348 | + if(err) throw err; |
| 349 | + reportReadiness(uri, options); |
| 350 | + }); |
| 351 | + } |
| 352 | +} |
| 353 | + |
| 354 | +function reportReadiness(uri, options) { |
| 355 | + if(options.socket) { |
| 356 | + console.log("Listening to socket", options.socket); |
| 357 | + } else { |
311 | 358 | console.log(" " + uri);
|
| 359 | + } |
312 | 360 |
|
313 |
| - console.log("webpack result is served from " + options.publicPath); |
314 |
| - if(Array.isArray(options.contentBase)) |
315 |
| - console.log("content is served from " + options.contentBase.join(", ")); |
316 |
| - else if(options.contentBase) |
317 |
| - console.log("content is served from " + options.contentBase); |
318 |
| - if(options.historyApiFallback) |
319 |
| - console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html"); |
320 |
| - if(options.open) |
321 |
| - open(uri); |
322 |
| - }); |
| 361 | + console.log("webpack result is served from " + options.publicPath); |
| 362 | + if(Array.isArray(options.contentBase)) |
| 363 | + console.log("content is served from " + options.contentBase.join(", ")); |
| 364 | + else if(options.contentBase) |
| 365 | + console.log("content is served from " + options.contentBase); |
| 366 | + if(options.historyApiFallback) |
| 367 | + console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html"); |
| 368 | + if(options.open) |
| 369 | + open(uri); |
323 | 370 | }
|
324 | 371 |
|
325 | 372 | processOptions(wpOpt);
|
0 commit comments