2018-06-14 17:30:14 +00:00
|
|
|
import { relative } from 'path'
|
2018-03-31 21:19:06 +00:00
|
|
|
import loaderUtils from 'loader-utils'
|
2016-10-14 15:05:08 +00:00
|
|
|
|
2018-11-21 15:04:37 +00:00
|
|
|
module.exports = function (content, sourceMap) {
|
2016-10-14 15:05:08 +00:00
|
|
|
this.cacheable()
|
|
|
|
|
2018-11-21 15:04:37 +00:00
|
|
|
const options = loaderUtils.getOptions(this)
|
2018-06-16 13:51:00 +00:00
|
|
|
if (!options.extensions) {
|
|
|
|
throw new Error('extensions is not provided to hot-self-accept-loader. Please upgrade all next-plugins to the latest version.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.include) {
|
|
|
|
throw new Error('include option is not provided to hot-self-accept-loader. Please upgrade all next-plugins to the latest version.')
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:30:14 +00:00
|
|
|
const route = getRoute(this.resourcePath, options)
|
2016-10-17 14:35:31 +00:00
|
|
|
|
2018-01-31 17:20:27 +00:00
|
|
|
// Webpack has a built in system to prevent default from colliding, giving it a random letter per export.
|
|
|
|
// We can safely check if Component is undefined since all other pages imported into the entrypoint don't have __webpack_exports__.default
|
2016-12-02 01:43:38 +00:00
|
|
|
this.callback(null, `${content}
|
2016-12-06 09:24:42 +00:00
|
|
|
(function (Component, route) {
|
2018-01-31 17:20:27 +00:00
|
|
|
if(!Component) return
|
2016-12-06 09:24:42 +00:00
|
|
|
if (!module.hot) return
|
2016-10-14 15:05:08 +00:00
|
|
|
module.hot.accept()
|
2016-12-06 09:24:42 +00:00
|
|
|
Component.__route = route
|
2016-11-03 15:12:37 +00:00
|
|
|
|
2016-12-06 09:24:42 +00:00
|
|
|
if (module.hot.status() === 'idle') return
|
2016-11-03 15:12:37 +00:00
|
|
|
|
2016-12-06 09:24:42 +00:00
|
|
|
var components = next.router.components
|
|
|
|
for (var r in components) {
|
|
|
|
if (!components.hasOwnProperty(r)) continue
|
2016-11-03 15:12:37 +00:00
|
|
|
|
2016-12-06 09:24:42 +00:00
|
|
|
if (components[r].Component.__route === route) {
|
|
|
|
next.router.update(r, Component)
|
2016-11-03 15:12:37 +00:00
|
|
|
}
|
2016-10-14 15:05:08 +00:00
|
|
|
}
|
2017-07-10 04:40:32 +00:00
|
|
|
})(typeof __webpack_exports__ !== 'undefined' ? __webpack_exports__.default : (module.exports.default || module.exports), ${JSON.stringify(route)})
|
2016-12-02 01:43:38 +00:00
|
|
|
`, sourceMap)
|
2016-10-14 15:05:08 +00:00
|
|
|
}
|
2016-10-17 14:35:31 +00:00
|
|
|
|
2018-11-21 15:04:37 +00:00
|
|
|
function getRoute (resourcePath, options) {
|
2018-06-14 17:30:14 +00:00
|
|
|
const dir = options.include.find((d) => resourcePath.indexOf(d) === 0)
|
|
|
|
|
|
|
|
if (!dir) {
|
|
|
|
throw new Error(`'hot-self-accept-loader' was called on a file that isn't a page.`)
|
|
|
|
}
|
|
|
|
|
2018-03-31 21:19:06 +00:00
|
|
|
const path = relative(dir, resourcePath).replace(options.extensions, '.js')
|
2016-11-03 15:12:37 +00:00
|
|
|
return '/' + path.replace(/((^|\/)index)?\.js$/, '')
|
2016-10-17 14:35:31 +00:00
|
|
|
}
|