2019-06-24 15:48:26 +02:00
|
|
|
import { loader } from 'webpack'
|
2019-01-08 23:10:32 +01:00
|
|
|
import loaderUtils from 'loader-utils'
|
|
|
|
|
|
|
|
export type ClientPagesLoaderOptions = {
|
2019-05-29 13:57:26 +02:00
|
|
|
absolutePagePath: string
|
2019-01-08 23:10:32 +01:00
|
|
|
page: string
|
2020-04-18 04:19:37 +02:00
|
|
|
hotRouterUpdates: boolean
|
2019-01-08 23:10:32 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
const nextClientPagesLoader: loader.Loader = function() {
|
2020-04-18 04:19:37 +02:00
|
|
|
const { absolutePagePath, page, hotRouterUpdates } = loaderUtils.getOptions(
|
|
|
|
this
|
|
|
|
) as ClientPagesLoaderOptions
|
2019-01-08 23:10:32 +01:00
|
|
|
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath)
|
|
|
|
const stringifiedPage = JSON.stringify(page)
|
|
|
|
|
|
|
|
return `
|
2020-04-18 04:19:37 +02:00
|
|
|
(window.__NEXT_P = window.__NEXT_P || []).push([
|
|
|
|
${stringifiedPage},
|
|
|
|
function () {
|
|
|
|
var mod = require(${stringifiedAbsolutePagePath});
|
|
|
|
if (${!!hotRouterUpdates} && module.hot) {
|
|
|
|
module.hot.accept(${stringifiedAbsolutePagePath}, function () {
|
|
|
|
if (!next.router.components[${stringifiedPage}]) return;
|
|
|
|
var updatedPage = require(${stringifiedAbsolutePagePath});
|
|
|
|
next.router.update(${stringifiedPage}, updatedPage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return mod;
|
2019-01-08 23:10:32 +01:00
|
|
|
}
|
2020-04-18 04:19:37 +02:00
|
|
|
]);
|
2019-01-08 23:10:32 +01:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
export default nextClientPagesLoader
|