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 {
|
2021-10-20 19:52:11 +02:00
|
|
|
FONT_MANIFEST,
|
2019-05-29 13:57:26 +02:00
|
|
|
PAGES_MANIFEST,
|
|
|
|
SERVER_DIRECTORY,
|
|
|
|
SERVERLESS_DIRECTORY,
|
2022-05-25 11:46:26 +02:00
|
|
|
APP_PATHS_MANIFEST,
|
2021-06-30 13:44:40 +02:00
|
|
|
} from '../shared/lib/constants'
|
|
|
|
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
|
2022-04-30 13:19:27 +02:00
|
|
|
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
|
|
|
|
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
|
2021-10-20 19:52:11 +02:00
|
|
|
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
|
2022-06-06 20:35:26 +02:00
|
|
|
import { PageNotFoundError } from '../shared/lib/utils'
|
2018-02-13 14:27:52 +01:00
|
|
|
|
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,
|
2021-04-06 12:25:04 +02:00
|
|
|
dev?: boolean,
|
2022-05-03 12:37:23 +02:00
|
|
|
locales?: string[],
|
2022-05-25 11:46:26 +02:00
|
|
|
appDirEnabled?: 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
|
|
|
)
|
2022-05-03 12:37:23 +02:00
|
|
|
let rootPathsManifest: undefined | PagesManifest
|
|
|
|
|
2022-05-25 11:46:26 +02:00
|
|
|
if (appDirEnabled) {
|
2022-05-03 12:37:23 +02:00
|
|
|
if (page === '/_root') {
|
|
|
|
return join(serverBuildPath, 'root.js')
|
|
|
|
}
|
2022-05-25 11:46:26 +02:00
|
|
|
rootPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST))
|
2022-05-03 12:37:23 +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) {
|
|
|
|
console.error(err)
|
2022-06-06 20:35:26 +02:00
|
|
|
throw new PageNotFoundError(page)
|
2018-02-13 14:27:52 +01:00
|
|
|
}
|
|
|
|
|
2022-05-03 12:37:23 +02:00
|
|
|
const checkManifest = (manifest: PagesManifest) => {
|
|
|
|
let curPath = manifest[page]
|
|
|
|
|
|
|
|
if (!manifest[curPath] && locales) {
|
|
|
|
const manifestNoLocales: typeof pagesManifest = {}
|
2021-04-06 12:25:04 +02:00
|
|
|
|
2022-05-03 12:37:23 +02:00
|
|
|
for (const key of Object.keys(manifest)) {
|
|
|
|
manifestNoLocales[normalizeLocalePath(key, locales).pathname] =
|
|
|
|
pagesManifest[key]
|
|
|
|
}
|
|
|
|
curPath = manifestNoLocales[page]
|
2021-04-06 12:25:04 +02:00
|
|
|
}
|
2022-05-03 12:37:23 +02:00
|
|
|
return curPath
|
|
|
|
}
|
|
|
|
let pagePath: string | undefined
|
|
|
|
|
|
|
|
if (rootPathsManifest) {
|
|
|
|
pagePath = checkManifest(rootPathsManifest)
|
2021-04-06 12:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pagePath) {
|
2022-05-03 12:37:23 +02:00
|
|
|
pagePath = checkManifest(pagesManifest)
|
2018-02-13 14:27:52 +01:00
|
|
|
}
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2022-05-03 12:37:23 +02:00
|
|
|
if (!pagePath) {
|
2022-06-06 20:35:26 +02:00
|
|
|
throw new PageNotFoundError(page)
|
2022-05-03 12:37:23 +02:00
|
|
|
}
|
2021-04-06 12:25:04 +02:00
|
|
|
return join(serverBuildPath, pagePath)
|
2018-02-13 14:27:52 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
export function requirePage(
|
|
|
|
page: string,
|
|
|
|
distDir: string,
|
2022-05-03 12:37:23 +02:00
|
|
|
serverless: boolean,
|
2022-05-25 11:46:26 +02:00
|
|
|
appDirEnabled?: boolean
|
2019-05-29 13:57:26 +02:00
|
|
|
): any {
|
2022-05-03 12:37:23 +02:00
|
|
|
const pagePath = getPagePath(
|
|
|
|
page,
|
|
|
|
distDir,
|
|
|
|
serverless,
|
|
|
|
false,
|
|
|
|
undefined,
|
2022-05-25 11:46:26 +02:00
|
|
|
appDirEnabled
|
2022-05-03 12:37:23 +02:00
|
|
|
)
|
2019-05-22 18:36:53 +02:00
|
|
|
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
|
|
|
}
|
2020-07-28 12:19:28 +02:00
|
|
|
|
|
|
|
export function requireFontManifest(distDir: string, serverless: boolean) {
|
|
|
|
const serverBuildPath = join(
|
|
|
|
distDir,
|
|
|
|
serverless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
|
|
|
|
)
|
|
|
|
const fontManifest = require(join(serverBuildPath, FONT_MANIFEST))
|
|
|
|
return fontManifest
|
|
|
|
}
|