From f184016197053251d0d60c4d1b8ae4091d0ea6da Mon Sep 17 00:00:00 2001 From: Jonas Galvez Date: Tue, 14 Aug 2018 15:35:25 -0300 Subject: [PATCH] Server middleware watcher (#3718) See https://github.com/nuxt/nuxt.js/issues/1509 Status: improving tests. --- bin/nuxt-dev | 17 +++++++---------- lib/builder/builder.js | 3 ++- test/fixtures/cli/middleware.js | 1 + test/fixtures/cli/nuxt.config.js | 1 + test/unit/cli.test.js | 22 +++++++++++++--------- 5 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 test/fixtures/cli/middleware.js diff --git a/bin/nuxt-dev b/bin/nuxt-dev index a535f36aa2..7e03ce816c 100755 --- a/bin/nuxt-dev +++ b/bin/nuxt-dev @@ -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 diff --git a/lib/builder/builder.js b/lib/builder/builder.js index a404b4e6af..b3808135fd 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -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}`) }) } diff --git a/test/fixtures/cli/middleware.js b/test/fixtures/cli/middleware.js new file mode 100644 index 0000000000..d855a7198d --- /dev/null +++ b/test/fixtures/cli/middleware.js @@ -0,0 +1 @@ +// This file is used to test custom chokidar watchers. diff --git a/test/fixtures/cli/nuxt.config.js b/test/fixtures/cli/nuxt.config.js index 74df9f5ca1..f2ad46aa7a 100644 --- a/test/fixtures/cli/nuxt.config.js +++ b/test/fixtures/cli/nuxt.config.js @@ -1,4 +1,5 @@ export default { + serverMiddleware: ['~/middleware.js'], watch: ['~/custom.file'], hooks(hook) { hook('listen', (server, { port, host }) => { diff --git a/test/unit/cli.test.js b/test/unit/cli.test.js index 0aff495503..c0ea20046d 100644 --- a/test/unit/cli.test.js +++ b/test/unit/cli.test.js @@ -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)