You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
808 B

4 years ago
  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;