mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
e775721f34
* add detach-plugin * detach-plugin: remove unused property * watch-pages-plugin: replace _error.js when user defined one was added/removed * dynamic-entry-plugin: delete cache * fix HMR settings for _error.js * render: pass error only on dev * hot-reload: enable to hot-reload error page * server: check if /_error has compilation errors * webapck-dev-client: fix reloading /_error
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
import { resolve, relative, join, extname } from 'path'
|
|
|
|
export default class WatchPagesPlugin {
|
|
constructor (dir) {
|
|
this.dir = resolve(dir, 'pages')
|
|
this.prevFileDependencies = null
|
|
}
|
|
|
|
apply (compiler) {
|
|
compiler.plugin('emit', (compilation, callback) => {
|
|
// watch the pages directory
|
|
compilation.contextDependencies =
|
|
compilation.contextDependencies.concat([this.dir])
|
|
|
|
this.prevFileDependencies = compilation.fileDependencies
|
|
|
|
callback()
|
|
})
|
|
|
|
const isPageFile = this.isPageFile.bind(this)
|
|
const getEntryName = (f) => {
|
|
return join('bundles', relative(compiler.options.context, f))
|
|
}
|
|
const errorPageName = join('bundles', 'pages', '_error.js')
|
|
|
|
compiler.plugin('watch-run', (watching, callback) => {
|
|
Object.keys(compiler.fileTimestamps)
|
|
.filter(isPageFile)
|
|
.filter((f) => this.prevFileDependencies.indexOf(f) < 0)
|
|
.forEach((f) => {
|
|
const name = getEntryName(f)
|
|
if (name === errorPageName) {
|
|
compiler.removeEntry(name)
|
|
}
|
|
|
|
if (compiler.hasEntry(name)) return
|
|
|
|
const entries = ['webpack/hot/dev-server', f]
|
|
compiler.addEntry(entries, name)
|
|
})
|
|
|
|
compiler.removedFiles
|
|
.filter(isPageFile)
|
|
.forEach((f) => {
|
|
const name = getEntryName(f)
|
|
compiler.removeEntry(name)
|
|
|
|
if (name === errorPageName) {
|
|
compiler.addEntry([
|
|
'webpack/hot/dev-server',
|
|
join(__dirname, '..', '..', '..', 'pages', '_error.js')
|
|
], name)
|
|
}
|
|
})
|
|
|
|
callback()
|
|
})
|
|
}
|
|
|
|
isPageFile (f) {
|
|
return f.indexOf(this.dir) === 0 && extname(f) === '.js'
|
|
}
|
|
}
|
|
|