forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwatch-remove-event-plugin.js
40 lines (32 loc) · 1.11 KB
/
watch-remove-event-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// watch and trigger file remove event
// see: https://github.com/webpack/webpack/issues/1533
export default class WatchRemoveEventPlugin {
constructor () {
this.removedFiles = []
}
apply (compiler) {
compiler.removedFiles = []
compiler.plugin('environment', () => {
if (!compiler.watchFileSystem) return
const { watchFileSystem } = compiler
const { watch } = watchFileSystem
watchFileSystem.watch = (files, dirs, missing, startTime, options, callback, callbackUndelayed) => {
const result = watch.call(watchFileSystem, files, dirs, missing, startTime, options, (...args) => {
compiler.removedFiles = this.removedFiles
this.removedFiles = []
callback(...args)
}, callbackUndelayed)
const watchpack = watchFileSystem.watcher
watchpack.fileWatchers.forEach((w) => {
w.on('remove', this.onRemove.bind(this, watchpack, w.path))
})
return result
}
})
}
onRemove (watchpack, file) {
this.removedFiles.push(file)
watchpack.emit('remove', file)
watchpack._onChange(file)
}
}