diff --git a/lib/Server.js b/lib/Server.js index 8eebe35578..9ad4489b84 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -711,6 +711,21 @@ class Server { }); } + showStatus() { + const suffix = + this.options.inline !== false || this.options.lazy === true + ? '/' + : '/webpack-dev-server/'; + const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`; + + status( + uri, + this.options, + this.log, + this.options.stats && this.options.stats.colors + ); + } + listen(port, hostname, fn) { this.hostname = hostname; @@ -878,21 +893,6 @@ class Server { return false; } - showStatus() { - const suffix = - this.options.inline !== false || this.options.lazy === true - ? '/' - : '/webpack-dev-server/'; - const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`; - - status( - uri, - this.options, - this.log, - this.options.stats && this.options.stats.colors - ); - } - // eslint-disable-next-line sockWrite(sockets, type, data) { sockets.forEach((socket) => { diff --git a/lib/utils/runOpen.js b/lib/utils/runOpen.js new file mode 100644 index 0000000000..50acf163bd --- /dev/null +++ b/lib/utils/runOpen.js @@ -0,0 +1,21 @@ +'use strict'; + +const open = require('opn'); + +function runOpen(uri, options, log) { + let openOptions = {}; + let openMessage = 'Unable to open browser'; + + if (typeof options.open === 'string') { + openOptions = { app: options.open }; + openMessage += `: ${options.open}`; + } + + open(`${uri}${options.openPage || ''}`, openOptions).catch(() => { + log.warn( + `${openMessage}. If you are running in a headless environment, please do not use the --open flag` + ); + }); +} + +module.exports = runOpen; diff --git a/lib/utils/status.js b/lib/utils/status.js index 4c552a4639..2a8ba63f91 100644 --- a/lib/utils/status.js +++ b/lib/utils/status.js @@ -1,7 +1,7 @@ 'use strict'; -const open = require('opn'); const colors = require('./colors'); +const runOpen = require('./runOpen'); // TODO: don't emit logs when webpack-dev-server is used via Node.js API function status(uri, options, log, useColor) { @@ -44,19 +44,7 @@ function status(uri, options, log, useColor) { } if (options.open) { - let openOptions = {}; - let openMessage = 'Unable to open browser'; - - if (typeof options.open === 'string') { - openOptions = { app: options.open }; - openMessage += `: ${options.open}`; - } - - open(`${uri}${options.openPage || ''}`, openOptions).catch(() => { - log.warn( - `${openMessage}. If you are running in a headless environment, please do not use the --open flag` - ); - }); + runOpen(uri, options, log); } }