-
Notifications
You must be signed in to change notification settings - Fork 26k
/
require.ts
127 lines (112 loc) · 3.17 KB
/
require.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { promises } from 'fs'
import { join } from 'path'
import {
FONT_MANIFEST,
MIDDLEWARE_MANIFEST,
PAGES_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
} from '../shared/lib/constants'
import { normalizePagePath, denormalizePagePath } from './normalize-page-path'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { MiddlewareManifest } from '../build/webpack/plugins/middleware-plugin'
import type { WasmBinding } from '../build/webpack/loaders/next-middleware-wasm-loader'
export function pageNotFoundError(page: string): Error {
const err: any = new Error(`Cannot find module for page: ${page}`)
err.code = 'ENOENT'
return err
}
export function getPagePath(
page: string,
distDir: string,
serverless: boolean,
dev?: boolean,
locales?: string[]
): string {
const serverBuildPath = join(
distDir,
serverless && !dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const pagesManifest = require(join(
serverBuildPath,
PAGES_MANIFEST
)) as PagesManifest
try {
page = denormalizePagePath(normalizePagePath(page))
} catch (err) {
console.error(err)
throw pageNotFoundError(page)
}
let pagePath = pagesManifest[page]
if (!pagesManifest[page] && locales) {
const manifestNoLocales: typeof pagesManifest = {}
for (const key of Object.keys(pagesManifest)) {
manifestNoLocales[normalizeLocalePath(key, locales).pathname] =
pagesManifest[key]
}
pagePath = manifestNoLocales[page]
}
if (!pagePath) {
throw pageNotFoundError(page)
}
return join(serverBuildPath, pagePath)
}
export function requirePage(
page: string,
distDir: string,
serverless: boolean
): any {
const pagePath = getPagePath(page, distDir, serverless)
if (pagePath.endsWith('.html')) {
return promises.readFile(pagePath, 'utf8')
}
return require(pagePath)
}
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
}
export function getMiddlewareInfo(params: {
dev?: boolean
distDir: string
page: string
serverless: boolean
}): {
name: string
paths: string[]
env: string[]
wasm: WasmBinding[]
} {
const serverBuildPath = join(
params.distDir,
params.serverless && !params.dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const middlewareManifest: MiddlewareManifest = require(join(
serverBuildPath,
MIDDLEWARE_MANIFEST
))
let page: string
try {
page = denormalizePagePath(normalizePagePath(params.page))
} catch (err) {
throw pageNotFoundError(params.page)
}
let pageInfo = middlewareManifest.middleware[page]
if (!pageInfo) {
throw pageNotFoundError(page)
}
return {
name: pageInfo.name,
paths: pageInfo.files.map((file) => join(params.distDir, file)),
env: pageInfo.env ?? [],
wasm: (pageInfo.wasm ?? []).map((binding) => ({
...binding,
filePath: join(params.distDir, binding.filePath),
})),
}
}