diff --git a/bin/common/utils.js b/bin/common/utils.js index b55e5ee48d..9846a15583 100644 --- a/bin/common/utils.js +++ b/bin/common/utils.js @@ -13,6 +13,24 @@ const esm = require('esm')(module, { const getRootDir = argv => resolve(argv._[0] || '.') const getNuxtConfigFile = argv => resolve(getRootDir(argv), argv['config-file']) +const getLatestHost = (argv) => { + const port = + argv.port || + process.env.NUXT_PORT || + process.env.PORT || + process.env.npm_package_config_nuxt_port + const host = + argv.hostname || + process.env.NUXT_HOST || + process.env.HOST || + process.env.npm_package_config_nuxt_host + const socket = + argv['unix-socket'] || + process.env.UNIX_SOCKET || + process.env.npm_package_config_unix_socket + + return { port, host, socket } +} exports.nuxtConfigFile = getNuxtConfigFile @@ -47,27 +65,9 @@ exports.loadNuxtConfig = (argv) => { if (!options.server) { options.server = {} } - options.server.port = argv.port || options.server.port - options.server.host = argv.hostname || options.server.host - + const { port, host, socket } = getLatestHost(argv) + options.server.port = port || options.server.port || 3000 + options.server.host = host || options.server.host || 'localhost' + options.server.socket = socket || options.server.socket return options } - -exports.getLatestHost = (argv) => { - const port = - argv.port || - process.env.NUXT_PORT || - process.env.PORT || - process.env.npm_package_config_nuxt_port - const host = - argv.hostname || - process.env.NUXT_HOST || - process.env.HOST || - process.env.npm_package_config_nuxt_host - const socket = - argv['unix-socket'] || - process.env.UNIX_SOCKET || - process.env.npm_package_config_unix_socket - - return { port, host, socket } -} diff --git a/bin/nuxt-dev b/bin/nuxt-dev index ffb856d18f..0315a98222 100755 --- a/bin/nuxt-dev +++ b/bin/nuxt-dev @@ -3,7 +3,7 @@ const parseArgs = require('minimist') const consola = require('consola') const { version } = require('../package.json') const { Nuxt, Builder } = require('..') -const { loadNuxtConfig, getLatestHost } = require('./common/utils') +const { loadNuxtConfig } = require('./common/utils') const argv = parseArgs(process.argv.slice(2), { alias: { @@ -74,9 +74,6 @@ const errorHandler = (err, instance) => { return errorHandler(err, oldInstance) } - // Get latest environment variables - const { port, host, socket } = getLatestHost(argv) - return ( Promise.resolve() .then(() => oldInstance && oldInstance.nuxt.clearHook('watch:fileChanged')) @@ -91,7 +88,7 @@ const errorHandler = (err, instance) => { }) .then(() => oldInstance && oldInstance.nuxt.close()) // Start listening - .then(() => nuxt.listen(port, host, socket)) + .then(() => nuxt.listen()) // Show ready message first time, others will be shown through WebpackBar .then(() => !oldInstance && nuxt.showReady(false)) .then(() => builder.watchServer()) diff --git a/bin/nuxt-start b/bin/nuxt-start index fc0cbedfe5..3ccb0dac93 100755 --- a/bin/nuxt-start +++ b/bin/nuxt-start @@ -4,7 +4,7 @@ const { resolve } = require('path') const parseArgs = require('minimist') const consola = require('consola') const { Nuxt } = require('../dist/nuxt-start') -const { loadNuxtConfig, getLatestHost } = require('./common/utils') +const { loadNuxtConfig } = require('./common/utils') const argv = parseArgs(process.argv.slice(2), { alias: { @@ -79,8 +79,6 @@ if (nuxt.options.render.ssr === true) { } } -const { port, host, socket } = getLatestHost(argv) - -nuxt.listen(port, host, socket).then(() => { +nuxt.listen().then(() => { nuxt.showReady(false) }) diff --git a/lib/core/nuxt.js b/lib/core/nuxt.js index 60546cd527..0cc4a330e3 100644 --- a/lib/core/nuxt.js +++ b/lib/core/nuxt.js @@ -142,7 +142,7 @@ export default class Nuxt { this.readyMessage = null } - listen(port = 3000, host = 'localhost', socket = null) { + listen(port, host, socket) { return this.ready().then(() => new Promise((resolve, reject) => { if (!socket && typeof this.options.server.socket === 'string') { socket = this.options.server.socket @@ -153,8 +153,8 @@ export default class Nuxt { if (socket) { args.path = socket } else { - args.port = port - args.host = host + args.port = port || this.options.server.port + args.host = host || this.options.server.host } let appServer diff --git a/test/unit/cli.test.js b/test/unit/cli.test.js index c0ea20046d..64570c57a2 100644 --- a/test/unit/cli.test.js +++ b/test/unit/cli.test.js @@ -1,5 +1,5 @@ -import { spawn } from 'child_process' import { resolve, join } from 'path' +import { spawn } from 'cross-spawn' import { writeFileSync } from 'fs-extra' import { getPort, rp, waitUntil, Utils } from '../utils' diff --git a/test/unit/with-config.test.js b/test/unit/with-config.test.js index 42341199cd..76659d6372 100644 --- a/test/unit/with-config.test.js +++ b/test/unit/with-config.test.js @@ -163,3 +163,16 @@ describe('with-config', () => { await nuxt.close() }) }) + +describe('server config', () => { + test('opens on port defined in server.port', async () => { + const config = await loadFixture('with-config') + config.server.port = port = await getPort() + nuxt = new Nuxt(config) + await nuxt.listen() + await nuxt.renderAndGetWindow(url('/test/')) + }) + afterAll(async () => { + await nuxt.close() + }) +})