2020-06-15 16:41:17 +02:00
|
|
|
import { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST } from '../lib/constants'
|
2019-05-26 23:22:43 +02:00
|
|
|
import { join } from 'path'
|
|
|
|
import { requirePage } from './require'
|
2020-01-25 05:25:11 +01:00
|
|
|
import { BuildManifest } from './get-page-files'
|
|
|
|
import { AppType, DocumentType } from '../lib/utils'
|
2020-02-27 18:57:39 +01:00
|
|
|
import {
|
|
|
|
PageConfig,
|
|
|
|
GetStaticPaths,
|
|
|
|
GetServerSideProps,
|
|
|
|
GetStaticProps,
|
|
|
|
} from 'next/types'
|
2018-12-18 17:12:49 +01:00
|
|
|
|
2019-05-11 13:18:56 +02:00
|
|
|
export function interopDefault(mod: any) {
|
2018-12-18 17:12:49 +01:00
|
|
|
return mod.default || mod
|
|
|
|
}
|
|
|
|
|
2020-01-25 05:25:11 +01:00
|
|
|
export type ManifestItem = {
|
|
|
|
id: number | string
|
|
|
|
name: string
|
|
|
|
file: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] }
|
|
|
|
|
2019-05-26 23:22:43 +02:00
|
|
|
export type LoadComponentsReturnType = {
|
2020-01-25 05:25:11 +01:00
|
|
|
Component: React.ComponentType
|
|
|
|
pageConfig?: PageConfig
|
|
|
|
buildManifest: BuildManifest
|
|
|
|
reactLoadableManifest: ReactLoadableManifest
|
|
|
|
Document: DocumentType
|
|
|
|
App: AppType
|
2020-02-27 18:57:39 +01:00
|
|
|
getStaticProps?: GetStaticProps
|
|
|
|
getStaticPaths?: GetStaticPaths
|
|
|
|
getServerSideProps?: GetServerSideProps
|
2021-03-03 20:20:48 +01:00
|
|
|
ComponentMod: any
|
2019-05-26 23:22:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadComponents(
|
|
|
|
distDir: string,
|
|
|
|
pathname: string,
|
2019-05-29 13:57:26 +02:00
|
|
|
serverless: boolean
|
2019-05-26 23:22:43 +02:00
|
|
|
): Promise<LoadComponentsReturnType> {
|
2019-05-22 18:36:53 +02:00
|
|
|
if (serverless) {
|
|
|
|
const Component = await requirePage(pathname, distDir, serverless)
|
2020-10-14 11:55:42 +02:00
|
|
|
let { getStaticProps, getStaticPaths, getServerSideProps } = Component
|
|
|
|
|
|
|
|
getStaticProps = await getStaticProps
|
|
|
|
getStaticPaths = await getStaticPaths
|
|
|
|
getServerSideProps = await getServerSideProps
|
|
|
|
const pageConfig = (await Component.config) || {}
|
2020-02-26 19:26:55 +01:00
|
|
|
|
2019-09-24 10:50:04 +02:00
|
|
|
return {
|
|
|
|
Component,
|
2020-10-14 11:55:42 +02:00
|
|
|
pageConfig,
|
2020-02-27 18:57:39 +01:00
|
|
|
getStaticProps,
|
|
|
|
getStaticPaths,
|
|
|
|
getServerSideProps,
|
2021-03-03 20:20:48 +01:00
|
|
|
ComponentMod: Component,
|
2020-01-25 05:25:11 +01:00
|
|
|
} as LoadComponentsReturnType
|
2019-05-22 18:36:53 +02:00
|
|
|
}
|
2020-01-03 18:00:06 +01:00
|
|
|
|
2021-03-03 20:20:48 +01:00
|
|
|
const DocumentMod = await requirePage('/_document', distDir, serverless)
|
|
|
|
const AppMod = await requirePage('/_app', distDir, serverless)
|
|
|
|
const ComponentMod = await requirePage(pathname, distDir, serverless)
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2019-05-26 23:22:43 +02:00
|
|
|
const [
|
|
|
|
buildManifest,
|
|
|
|
reactLoadableManifest,
|
|
|
|
Component,
|
|
|
|
Document,
|
|
|
|
App,
|
|
|
|
] = await Promise.all([
|
2018-12-18 17:12:49 +01:00
|
|
|
require(join(distDir, BUILD_MANIFEST)),
|
|
|
|
require(join(distDir, REACT_LOADABLE_MANIFEST)),
|
2019-06-10 02:16:14 +02:00
|
|
|
interopDefault(ComponentMod),
|
2019-05-24 04:05:08 +02:00
|
|
|
interopDefault(DocumentMod),
|
2020-01-03 18:00:06 +01:00
|
|
|
interopDefault(AppMod),
|
2018-12-18 17:12:49 +01:00
|
|
|
])
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
const { getServerSideProps, getStaticProps, getStaticPaths } = ComponentMod
|
2020-02-26 19:26:55 +01:00
|
|
|
|
2019-05-24 04:05:08 +02:00
|
|
|
return {
|
2019-06-10 02:16:14 +02:00
|
|
|
App,
|
2019-05-24 04:05:08 +02:00
|
|
|
Document,
|
2019-06-10 02:16:14 +02:00
|
|
|
Component,
|
|
|
|
buildManifest,
|
|
|
|
reactLoadableManifest,
|
2019-07-01 23:13:52 +02:00
|
|
|
pageConfig: ComponentMod.config || {},
|
2021-03-03 20:20:48 +01:00
|
|
|
ComponentMod,
|
2020-02-27 18:57:39 +01:00
|
|
|
getServerSideProps,
|
|
|
|
getStaticProps,
|
|
|
|
getStaticPaths,
|
2019-05-24 04:05:08 +02:00
|
|
|
}
|
2018-12-18 17:12:49 +01:00
|
|
|
}
|