createDomain.js 808 B

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