Server middleware watcher (#3718)

See https://github.com/nuxt/nuxt.js/issues/1509

Status: improving tests.
This commit is contained in:
Jonas Galvez 2018-08-14 15:35:25 -03:00 committed by Clark Du
parent 85a8af570d
commit f184016197
5 changed files with 24 additions and 20 deletions

View File

@ -50,7 +50,6 @@ if (argv.help) {
}
// Start dev
let hooked = false
let dev = startDev()
function startDev(oldInstance) {
@ -80,16 +79,14 @@ function startDev(oldInstance) {
return onError(err, oldInstance)
}
if (!hooked) {
nuxt.hook('watch:fileChanged', (fname) => {
consola.debug(`[${fname}] changed`)
dev = dev.then((instance) => {
consola.debug('Rebuilding the app...')
return startDev(instance)
})
nuxt.hook('watch:fileChanged', (fname) => {
consola.debug(`[${fname}] changed`)
dev = dev.then((instance) => {
consola.debug('Rebuilding the app...')
return startDev(instance)
})
hooked = true
}
})
// Get latest environment variables
const { port, host } = nuxt.options.server

View File

@ -639,6 +639,7 @@ export default class Builder {
})
const nuxtRestartWatch = _.concat(
this.options.serverMiddleware.map(this.nuxt.resolveAlias),
this.options.watch.map(this.nuxt.resolveAlias),
path.join(this.options.rootDir, 'nuxt.config.js')
)
@ -646,7 +647,7 @@ export default class Builder {
.watch(nuxtRestartWatch, options)
.on('change', (_path) => {
const parsedPath = path.parse(_path)
restartServer(`${parsedPath.base}${parsedPath.ext}`)
restartServer(`${parsedPath.name}${parsedPath.ext}`)
})
}

1
test/fixtures/cli/middleware.js vendored Normal file
View File

@ -0,0 +1 @@
// This file is used to test custom chokidar watchers.

View File

@ -1,4 +1,5 @@
export default {
serverMiddleware: ['~/middleware.js'],
watch: ['~/custom.file'],
hooks(hook) {
hook('listen', (server, { port, host }) => {

View File

@ -1,7 +1,7 @@
import { spawn } from 'child_process'
import { resolve, join } from 'path'
import { writeFileSync } from 'fs-extra'
import { getPort, rp, waitUntil } from '../utils'
import { getPort, rp, waitUntil, Utils } from '../utils'
let port
const rootDir = resolve(__dirname, '..', 'fixtures/cli')
@ -34,11 +34,15 @@ describe.skip.appveyor('cli', () => {
const customFilePath = join(rootDir, 'custom.file')
writeFileSync(customFilePath, 'This file is used to test custom chokidar watchers.')
// Must see two compilations in the log
expect(
stdout.indexOf('Compiled client') !==
stdout.lastIndexOf('Compiled client')
)
// Change file specified in `serverMiddleware` (nuxt.config.js)
const serverMiddlewarePath = join(rootDir, 'middleware.js')
writeFileSync(serverMiddlewarePath, '// This file is used to test custom chokidar watchers.\n')
// Wait 2s for picking up changes
await Utils.waitFor(2000)
// [Add actual test for changes here]
await close(nuxtDev)
})
@ -59,9 +63,9 @@ describe.skip.appveyor('cli', () => {
nuxtStart.stdout.on('data', (data) => { stdout += data })
nuxtStart.on('error', (err) => { error = err })
// Wait max 20s for the starting
if (await waitUntil(() => stdout.includes(`${port}`))) {
error = 'server failed to start successfully in 20 seconds'
// Wait max 40s for the starting
if (await waitUntil(() => stdout.includes(`${port}`), 40)) {
error = 'server failed to start successfully in 40 seconds'
}
expect(error).toBe(undefined)