rsnext/server/build/loaders/hot-self-accept-loader.js

39 lines
1 KiB
JavaScript
Raw Normal View History

2016-10-17 16:35:31 +02:00
import { resolve, relative } from 'path'
2016-10-14 17:05:08 +02:00
module.exports = function (content) {
this.cacheable()
2016-10-17 16:35:31 +02:00
const route = getRoute(this)
return `${content}
2016-10-14 17:05:08 +02:00
if (module.hot) {
module.hot.accept()
var Component = module.exports.default || module.exports
Component.__route = ${JSON.stringify(route)}
2016-10-19 14:41:45 +02:00
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)
}
}
2016-10-14 17:05:08 +02:00
}
}
`
}
2016-10-17 16:35:31 +02:00
const nextPagesDir = resolve(__dirname, '..', '..', '..', 'pages')
2016-10-17 16:35:31 +02:00
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$/, '')
2016-10-17 16:35:31 +02:00
}