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
// 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() {
@ -512,6 +493,8 @@ export default class Builder {
watchOptions: this.options.watchers.webpack
}, this.options.build.devMiddleware)))
this.webpackDevMiddleware.close = pify(this.webpackDevMiddleware.close)
this.webpackHotMiddleware = pify(webpackHotMiddleware(compiler, Object.assign({
log: false,
heartbeat: 10000
@ -528,18 +511,19 @@ export default class Builder {
}
watchFiles() {
const src = this.options.srcDir
const patterns = [
r(this.options.srcDir, 'layouts'),
r(this.options.srcDir, 'store'),
r(this.options.srcDir, 'middleware'),
r(this.options.srcDir, 'layouts/*.vue'),
r(this.options.srcDir, 'layouts/**/*.vue')
r(src, 'layouts'),
r(src, 'store'),
r(src, 'middleware'),
r(src, 'layouts/*.vue'),
r(src, 'layouts/**/*.vue')
]
if (this._nuxtPages) {
patterns.push(r(this.options.srcDir, 'pages'))
patterns.push(r(this.options.srcDir, 'pages/*.vue'))
patterns.push(r(this.options.srcDir, 'pages/**/*.vue'))
}
this._nuxtPages && patterns.push(
r(src, 'pages'),
r(src, 'pages/*.vue'),
r(src, 'pages/**/*.vue')
)
const options = Object.assign({}, this.options.watchers.chokidar, {
ignoreInitial: true
})
@ -555,6 +539,21 @@ export default class Builder {
this.customFilesWatcher = chokidar.watch(_.uniq(this.options.build.watch), options)
.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 = {