fix: use options.server values port, host, socket (#3942)

* user options.server values port, host, socket

* fix: Remove default value
This commit is contained in:
Dmitry Molotkov 2018-09-23 12:49:51 +03:00 committed by Sébastien Chopin
parent 7a68e1dde1
commit 9c3702c819
6 changed files with 43 additions and 35 deletions

View File

@ -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 }
}

View File

@ -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())

View File

@ -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)
})

View File

@ -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

View File

@ -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'

View File

@ -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()
})
})