Merge branch 'dev' into missing-chunk-reload

This commit is contained in:
Sébastien Chopin 2018-09-23 11:03:07 +01:00 committed by GitHub
commit 3368cd2d9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 getRootDir = argv => resolve(argv._[0] || '.')
const getNuxtConfigFile = argv => resolve(getRootDir(argv), argv['config-file']) 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 exports.nuxtConfigFile = getNuxtConfigFile
@ -47,27 +65,9 @@ exports.loadNuxtConfig = (argv) => {
if (!options.server) { if (!options.server) {
options.server = {} options.server = {}
} }
options.server.port = argv.port || options.server.port const { port, host, socket } = getLatestHost(argv)
options.server.host = argv.hostname || options.server.host 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 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 consola = require('consola')
const { version } = require('../package.json') const { version } = require('../package.json')
const { Nuxt, Builder } = require('..') const { Nuxt, Builder } = require('..')
const { loadNuxtConfig, getLatestHost } = require('./common/utils') const { loadNuxtConfig } = require('./common/utils')
const argv = parseArgs(process.argv.slice(2), { const argv = parseArgs(process.argv.slice(2), {
alias: { alias: {
@ -74,9 +74,6 @@ const errorHandler = (err, instance) => {
return errorHandler(err, oldInstance) return errorHandler(err, oldInstance)
} }
// Get latest environment variables
const { port, host, socket } = getLatestHost(argv)
return ( return (
Promise.resolve() Promise.resolve()
.then(() => oldInstance && oldInstance.nuxt.clearHook('watch:fileChanged')) .then(() => oldInstance && oldInstance.nuxt.clearHook('watch:fileChanged'))
@ -91,7 +88,7 @@ const errorHandler = (err, instance) => {
}) })
.then(() => oldInstance && oldInstance.nuxt.close()) .then(() => oldInstance && oldInstance.nuxt.close())
// Start listening // Start listening
.then(() => nuxt.listen(port, host, socket)) .then(() => nuxt.listen())
// Show ready message first time, others will be shown through WebpackBar // Show ready message first time, others will be shown through WebpackBar
.then(() => !oldInstance && nuxt.showReady(false)) .then(() => !oldInstance && nuxt.showReady(false))
.then(() => builder.watchServer()) .then(() => builder.watchServer())

View File

@ -4,7 +4,7 @@ const { resolve } = require('path')
const parseArgs = require('minimist') const parseArgs = require('minimist')
const consola = require('consola') const consola = require('consola')
const { Nuxt } = require('../dist/nuxt-start') const { Nuxt } = require('../dist/nuxt-start')
const { loadNuxtConfig, getLatestHost } = require('./common/utils') const { loadNuxtConfig } = require('./common/utils')
const argv = parseArgs(process.argv.slice(2), { const argv = parseArgs(process.argv.slice(2), {
alias: { alias: {
@ -79,8 +79,6 @@ if (nuxt.options.render.ssr === true) {
} }
} }
const { port, host, socket } = getLatestHost(argv) nuxt.listen().then(() => {
nuxt.listen(port, host, socket).then(() => {
nuxt.showReady(false) nuxt.showReady(false)
}) })

View File

@ -142,7 +142,7 @@ export default class Nuxt {
this.readyMessage = null this.readyMessage = null
} }
listen(port = 3000, host = 'localhost', socket = null) { listen(port, host, socket) {
return this.ready().then(() => new Promise((resolve, reject) => { return this.ready().then(() => new Promise((resolve, reject) => {
if (!socket && typeof this.options.server.socket === 'string') { if (!socket && typeof this.options.server.socket === 'string') {
socket = this.options.server.socket socket = this.options.server.socket
@ -153,8 +153,8 @@ export default class Nuxt {
if (socket) { if (socket) {
args.path = socket args.path = socket
} else { } else {
args.port = port args.port = port || this.options.server.port
args.host = host args.host = host || this.options.server.host
} }
let appServer let appServer

View File

@ -1,5 +1,5 @@
import { spawn } from 'child_process'
import { resolve, join } from 'path' import { resolve, join } from 'path'
import { spawn } from 'cross-spawn'
import { writeFileSync } from 'fs-extra' import { writeFileSync } from 'fs-extra'
import { getPort, rp, waitUntil, Utils } from '../utils' import { getPort, rp, waitUntil, Utils } from '../utils'

View File

@ -163,3 +163,16 @@ describe('with-config', () => {
await nuxt.close() 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()
})
})