rsnext/server/build/plugins/watch-pages-plugin.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-10-23 18:42:13 +02:00
import { resolve, relative, join, extname } from 'path'
export default class WatchPagesPlugin {
constructor (dir) {
this.dir = resolve(dir, 'pages')
}
apply (compiler) {
compiler.plugin('emit', (compilation, callback) => {
// watch the pages directory
compilation.contextDependencies =
compilation.contextDependencies.concat([this.dir])
callback()
})
const isPageFile = this.isPageFile.bind(this)
const getEntryName = (f) => {
return join('bundles', relative(compiler.options.context, f))
}
compiler.plugin('watch-run', (watching, callback) => {
Object.keys(compiler.fileTimestamps)
.filter(isPageFile)
.forEach((f) => {
const name = getEntryName(f)
if (compiler.hasEntry(name)) return
const entries = ['webpack/hot/dev-server', f]
2016-10-23 18:42:13 +02:00
compiler.addEntry(entries, name)
})
compiler.removedFiles
.filter(isPageFile)
.forEach((f) => {
const name = getEntryName(f)
compiler.removeEntry(name)
})
callback()
})
}
isPageFile (f) {
return f.indexOf(this.dir) === 0 && extname(f) === '.js'
}
}