UNPKG

838 BJavaScriptView Raw
1'use strict';
2
3/* eslint-disable
4 no-nested-ternary,
5 multiline-ternary,
6 space-before-function-paren
7*/
8const url = require('url');
9const ip = require('internal-ip');
10
11function createDomain(options, server) {
12 const protocol = options.https ? 'https' : 'http';
13 const hostname = options.useLocalIp
14 ? ip.v4.sync() || 'localhost'
15 : options.host;
16
17 const port = options.socket ? 0 : server ? server.address().port : 0;
18 // use explicitly defined public url
19 // (prefix with protocol if not explicitly given)
20 if (options.public) {
21 return /^[a-zA-Z]+:\/\//.test(options.public)
22 ? `${options.public}`
23 : `${protocol}://${options.public}`;
24 }
25 // the formatted domain (url without path) of the webpack server
26 return url.format({
27 protocol,
28 hostname,
29 port,
30 });
31}
32
33module.exports = createDomain;