createDomain.js 747 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. const url = require('url');
  3. const internalIp = require('internal-ip');
  4. module.exports = function createDomain(options, listeningApp) {
  5. const protocol = options.https ? 'https' : 'http';
  6. const appPort = listeningApp ? listeningApp.address().port : 0;
  7. const port = options.socket ? 0 : appPort;
  8. const hostname = options.useLocalIp ? internalIp.v4() : options.host;
  9. // use explicitly defined public url (prefix with protocol if not explicitly given)
  10. if (options.public) {
  11. return /^[a-zA-Z]+:\/\//.test(options.public) ? `${options.public}` : `${protocol}://${options.public}`;
  12. }
  13. // the formatted domain (url without path) of the webpack server
  14. return url.format({
  15. protocol,
  16. hostname,
  17. port
  18. });
  19. };