refactor: make unwatch async

This commit is contained in:
Clark Du 2017-11-24 11:43:01 +08:00 committed by Pooya Parsa
parent 967a442d5e
commit c15543cb83

View File

@ -60,28 +60,9 @@ export default class Builder {
this._buildStatus = STATUS.INITIAL this._buildStatus = STATUS.INITIAL
// Stop watching on nuxt.close() // Stop watching on nuxt.close()
this.nuxt.hook('close', () => this.unwatch()) if (this.options.dev) {
this.nuxt.hook('close', async () => { await this.unwatch() })
} }
unwatch() {
if (this.filesWatcher) {
this.filesWatcher.close()
}
if (this.customFilesWatcher) {
this.customFilesWatcher.close()
}
this.compilersWatching.forEach((watching) => watching.close())
// Stop webpack middleware
return new Promise(resolve => {
if (this.webpackDevMiddleware) {
this.webpackDevMiddleware.close(() => resolve())
} else {
resolve()
}
})
} }
get plugins() { get plugins() {
@ -512,6 +493,8 @@ export default class Builder {
watchOptions: this.options.watchers.webpack watchOptions: this.options.watchers.webpack
}, this.options.build.devMiddleware))) }, this.options.build.devMiddleware)))
this.webpackDevMiddleware.close = pify(this.webpackDevMiddleware.close)
this.webpackHotMiddleware = pify(webpackHotMiddleware(compiler, Object.assign({ this.webpackHotMiddleware = pify(webpackHotMiddleware(compiler, Object.assign({
log: false, log: false,
heartbeat: 10000 heartbeat: 10000
@ -528,18 +511,19 @@ export default class Builder {
} }
watchFiles() { watchFiles() {
const src = this.options.srcDir
const patterns = [ const patterns = [
r(this.options.srcDir, 'layouts'), r(src, 'layouts'),
r(this.options.srcDir, 'store'), r(src, 'store'),
r(this.options.srcDir, 'middleware'), r(src, 'middleware'),
r(this.options.srcDir, 'layouts/*.vue'), r(src, 'layouts/*.vue'),
r(this.options.srcDir, 'layouts/**/*.vue') r(src, 'layouts/**/*.vue')
] ]
if (this._nuxtPages) { this._nuxtPages && patterns.push(
patterns.push(r(this.options.srcDir, 'pages')) r(src, 'pages'),
patterns.push(r(this.options.srcDir, 'pages/*.vue')) r(src, 'pages/*.vue'),
patterns.push(r(this.options.srcDir, 'pages/**/*.vue')) r(src, 'pages/**/*.vue')
} )
const options = Object.assign({}, this.options.watchers.chokidar, { const options = Object.assign({}, this.options.watchers.chokidar, {
ignoreInitial: true ignoreInitial: true
}) })
@ -555,6 +539,21 @@ export default class Builder {
this.customFilesWatcher = chokidar.watch(_.uniq(this.options.build.watch), options) this.customFilesWatcher = chokidar.watch(_.uniq(this.options.build.watch), options)
.on('change', refreshFiles) .on('change', refreshFiles)
} }
async unwatch() {
if (this.filesWatcher) {
this.filesWatcher.close()
}
if (this.customFilesWatcher) {
this.customFilesWatcher.close()
}
this.compilersWatching.forEach(watching => watching.close())
// Stop webpack middleware
await this.webpackDevMiddleware.close()
}
} }
const STATUS = { const STATUS = {