From ccc90c98008e5016bf5cc3f55165aba8785ff9de Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 24 Jul 2018 11:00:49 -0400 Subject: [PATCH] feat(serve): allow specifying public network url via command line --- packages/@vue/cli-service/lib/commands/serve.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/@vue/cli-service/lib/commands/serve.js b/packages/@vue/cli-service/lib/commands/serve.js index ab31fe628..b96d9d9cf 100644 --- a/packages/@vue/cli-service/lib/commands/serve.js +++ b/packages/@vue/cli-service/lib/commands/serve.js @@ -21,7 +21,8 @@ module.exports = (api, options) => { '--mode': `specify env mode (default: development)`, '--host': `specify host (default: ${defaults.host})`, '--port': `specify port (default: ${defaults.port})`, - '--https': `use https (default: ${defaults.https})` + '--https': `use https (default: ${defaults.https})`, + '--public': `specify the public network URL for the HMR client` } }, async function serve (args) { info('Starting development server...') @@ -68,6 +69,7 @@ module.exports = (api, options) => { const host = args.host || process.env.HOST || projectDevServerOptions.host || defaults.host portfinder.basePort = args.port || process.env.PORT || projectDevServerOptions.port || defaults.port const port = await portfinder.getPortPromise() + const publicUrl = args.public || projectDevServerOptions.public const urls = prepareURLs( protocol, @@ -83,8 +85,7 @@ module.exports = (api, options) => { // inject dev & hot-reload middleware entries if (!isProduction) { - const publicOpt = projectDevServerOptions.public - const sockjsUrl = publicOpt ? `//${publicOpt}/sockjs-node` : url.format({ + const sockjsUrl = publicUrl ? `//${publicUrl}/sockjs-node` : url.format({ protocol, port, hostname: urls.lanUrlForConfig || 'localhost', @@ -181,11 +182,14 @@ module.exports = (api, options) => { copied = chalk.dim('(copied to clipboard)') } + const networkUrl = publicUrl + ? (protocol + '://' + publicUrl).replace(/([^/])$/, '$1/') + : urls.lanUrlForTerminal console.log() console.log([ ` App running at:`, ` - Local: ${chalk.cyan(urls.localUrlForTerminal)} ${copied}`, - ` - Network: ${chalk.cyan(urls.lanUrlForTerminal)}` + ` - Network: ${chalk.cyan(networkUrl)}` ].join('\n')) console.log()