1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/build/loaders/hot-self-accept-loader.js
Naoyuki Kanezawa e775721f34 Hot reload error page (#190)
* 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
2016-11-04 00:12:37 +09:00

39 lines
1 KiB
JavaScript

import { resolve, relative } from 'path'
module.exports = function (content) {
this.cacheable()
const route = getRoute(this)
return `${content}
if (module.hot) {
module.hot.accept()
var Component = module.exports.default || module.exports
Component.__route = ${JSON.stringify(route)}
if (module.hot.status() !== 'idle') {
var components = next.router.components
for (var r in components) {
if (!components.hasOwnProperty(r)) continue
if (components[r].Component.__route === ${JSON.stringify(route)}) {
next.router.update(r, Component)
}
}
}
}
`
}
const nextPagesDir = resolve(__dirname, '..', '..', '..', 'pages')
function getRoute (loaderContext) {
const pagesDir = resolve(loaderContext.options.context, 'pages')
const { resourcePath } = loaderContext
const dir = [pagesDir, nextPagesDir]
.find((d) => resourcePath.indexOf(d) === 0)
const path = relative(dir, resourcePath)
return '/' + path.replace(/((^|\/)index)?\.js$/, '')
}