import { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST } from '../lib/constants' import { join } from 'path' import { requirePage } from './require' import { BuildManifest } from './get-page-files' import { AppType, DocumentType } from '../lib/utils' import { PageConfig, GetStaticPaths, GetServerSideProps, GetStaticProps, } from 'next/types' export function interopDefault(mod: any) { return mod.default || mod } export type ManifestItem = { id: number | string name: string file: string } type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] } export type LoadComponentsReturnType = { Component: React.ComponentType pageConfig?: PageConfig buildManifest: BuildManifest reactLoadableManifest: ReactLoadableManifest Document: DocumentType App: AppType getStaticProps?: GetStaticProps getStaticPaths?: GetStaticPaths getServerSideProps?: GetServerSideProps ComponentMod: any } export async function loadComponents( distDir: string, pathname: string, serverless: boolean ): Promise { if (serverless) { const Component = await requirePage(pathname, distDir, serverless) let { getStaticProps, getStaticPaths, getServerSideProps } = Component getStaticProps = await getStaticProps getStaticPaths = await getStaticPaths getServerSideProps = await getServerSideProps const pageConfig = (await Component.config) || {} return { Component, pageConfig, getStaticProps, getStaticPaths, getServerSideProps, ComponentMod: Component, } as LoadComponentsReturnType } const DocumentMod = await requirePage('/_document', distDir, serverless) const AppMod = await requirePage('/_app', distDir, serverless) const ComponentMod = await requirePage(pathname, distDir, serverless) const [ buildManifest, reactLoadableManifest, Component, Document, App, ] = await Promise.all([ require(join(distDir, BUILD_MANIFEST)), require(join(distDir, REACT_LOADABLE_MANIFEST)), interopDefault(ComponentMod), interopDefault(DocumentMod), interopDefault(AppMod), ]) const { getServerSideProps, getStaticProps, getStaticPaths } = ComponentMod return { App, Document, Component, buildManifest, reactLoadableManifest, pageConfig: ComponentMod.config || {}, ComponentMod, getServerSideProps, getStaticProps, getStaticPaths, } }