2020-05-02 06:10:19 +02:00
|
|
|
import { promises } from 'fs'
|
2019-05-29 13:57:26 +02:00
|
|
|
import { join } from 'path'
|
|
|
|
import {
|
|
|
|
PAGES_MANIFEST,
|
|
|
|
SERVER_DIRECTORY,
|
|
|
|
SERVERLESS_DIRECTORY,
|
|
|
|
} from '../lib/constants'
|
2020-06-04 19:32:45 +02:00
|
|
|
import { normalizePagePath, denormalizePagePath } from './normalize-page-path'
|
2020-05-25 23:15:56 +02:00
|
|
|
import { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
|
2018-02-05 18:46:36 +01:00
|
|
|
|
2018-12-31 14:44:27 +01:00
|
|
|
export function pageNotFoundError(page: string): Error {
|
2018-12-07 13:35:01 +01:00
|
|
|
const err: any = new Error(`Cannot find module for page: ${page}`)
|
2018-02-13 14:27:52 +01:00
|
|
|
err.code = 'ENOENT'
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
export function getPagePath(
|
|
|
|
page: string,
|
|
|
|
distDir: string,
|
2019-06-21 04:47:45 +02:00
|
|
|
serverless: boolean,
|
|
|
|
dev?: boolean
|
2019-05-29 13:57:26 +02:00
|
|
|
): string {
|
|
|
|
const serverBuildPath = join(
|
|
|
|
distDir,
|
2019-06-21 04:47:45 +02:00
|
|
|
serverless && !dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
|
2019-05-29 13:57:26 +02:00
|
|
|
)
|
2020-05-25 23:15:56 +02:00
|
|
|
const pagesManifest = require(join(
|
|
|
|
serverBuildPath,
|
|
|
|
PAGES_MANIFEST
|
|
|
|
)) as PagesManifest
|
2018-02-13 14:27:52 +01:00
|
|
|
|
|
|
|
try {
|
2020-06-04 19:32:45 +02:00
|
|
|
page = denormalizePagePath(normalizePagePath(page))
|
2018-02-13 14:27:52 +01:00
|
|
|
} catch (err) {
|
2018-12-31 14:44:27 +01:00
|
|
|
// tslint:disable-next-line
|
2018-02-13 14:27:52 +01:00
|
|
|
console.error(err)
|
|
|
|
throw pageNotFoundError(page)
|
|
|
|
}
|
|
|
|
|
2018-03-30 15:08:09 +02:00
|
|
|
if (!pagesManifest[page]) {
|
2020-06-04 19:32:45 +02:00
|
|
|
throw pageNotFoundError(page)
|
2018-02-13 14:27:52 +01:00
|
|
|
}
|
2018-03-30 15:08:09 +02:00
|
|
|
return join(serverBuildPath, pagesManifest[page])
|
2018-02-13 14:27:52 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
export function requirePage(
|
|
|
|
page: string,
|
|
|
|
distDir: string,
|
|
|
|
serverless: boolean
|
|
|
|
): any {
|
2019-05-22 18:36:53 +02:00
|
|
|
const pagePath = getPagePath(page, distDir, serverless)
|
|
|
|
if (pagePath.endsWith('.html')) {
|
2020-05-02 06:10:19 +02:00
|
|
|
return promises.readFile(pagePath, 'utf8')
|
2019-05-22 18:36:53 +02:00
|
|
|
}
|
2019-04-11 20:59:26 +02:00
|
|
|
return require(pagePath)
|
2018-02-05 18:46:36 +01:00
|
|
|
}
|