import { resolve, relative } from 'path' module.exports = function (content, sourceMap) { this.cacheable() const route = getRoute(this) // 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 this.callback(null, `${content} (function (Component, route) { if(!Component) return if (!module.hot) return module.hot.accept() Component.__route = route if (module.hot.status() === 'idle') return var components = next.router.components for (var r in components) { if (!components.hasOwnProperty(r)) continue if (components[r].Component.__route === route) { next.router.update(r, Component) } } })(typeof __webpack_exports__ !== 'undefined' ? __webpack_exports__.default : (module.exports.default || module.exports), ${JSON.stringify(route)}) `, sourceMap) } 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$/, '') }