/
getStaticRenderFunctions.ts
261 lines (227 loc) · 8.59 KB
/
getStaticRenderFunctions.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Copyright © 2022 650 Industries.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs';
import fetch from 'node-fetch';
import path from 'path';
import requireString from 'require-from-string';
import resolveFrom from 'resolve-from';
import { logMetroError, logMetroErrorAsync } from './metro/metroErrorInterface';
import { getMetroServerRoot } from './middleware/ManifestMiddleware';
import { createBundleUrlPath, ExpoMetroOptions } from './middleware/metroOptions';
import { augmentLogs } from './serverLogLikeMetro';
import { stripAnsi } from '../../utils/ansi';
import { delayAsync } from '../../utils/delay';
import { SilentError } from '../../utils/errors';
import { memoize } from '../../utils/fn';
import { profile } from '../../utils/profile';
/** The list of input keys will become optional, everything else will remain the same. */
export type PickPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
class MetroNodeError extends Error {
constructor(
message: string,
public rawObject: any
) {
super(message);
}
}
const debug = require('debug')('expo:start:server:node-renderer') as typeof console.log;
const cachedSourceMaps: Map<string, { url: string; map: string }> = new Map();
// Support unhandled rejections
// Detect if running in Bun
// @ts-expect-error: This is a global variable that is set by Bun.
if (!process.isBun) {
require('source-map-support').install({
retrieveSourceMap(source: string) {
if (cachedSourceMaps.has(source)) {
return cachedSourceMaps.get(source);
}
return null;
},
});
}
function wrapBundle(str: string) {
// Skip the metro runtime so debugging is a bit easier.
// Replace the __r() call with an export statement.
// Use gm to apply to the last require line. This is needed when the bundle has side-effects.
return str.replace(/^(__r\(.*\);)$/gm, 'module.exports = $1');
}
// TODO(EvanBacon): Group all the code together and version.
const getRenderModuleId = (
projectRoot: string,
entry: string = 'expo-router/node/render.js'
): string => {
const moduleId = resolveFrom.silent(projectRoot, entry);
if (!moduleId) {
throw new Error(
`A version of expo-router with Node.js support is not installed in the project.`
);
}
return moduleId;
};
const moveStaticRenderFunction = memoize(async (projectRoot: string, requiredModuleId: string) => {
// Copy the file into the project to ensure it works in monorepos.
// This means the file cannot have any relative imports.
const tempDir = path.join(projectRoot, '.expo/static');
await fs.promises.mkdir(tempDir, { recursive: true });
const moduleId = path.join(tempDir, 'render.js');
await fs.promises.writeFile(moduleId, await fs.promises.readFile(requiredModuleId, 'utf8'));
// Sleep to give watchman time to register the file.
await delayAsync(50);
return moduleId;
});
/** @returns the js file contents required to generate the static generation function. */
async function getStaticRenderFunctionsContentAsync(
projectRoot: string,
devServerUrl: string,
props: PickPartial<ExpoMetroOptions, 'mainModuleName' | 'bytecode'>,
entry?: string
): Promise<{ src: string; filename: string }> {
const root = getMetroServerRoot(projectRoot);
const requiredModuleId = getRenderModuleId(root, entry);
let moduleId = requiredModuleId;
// Cannot be accessed using Metro's server API, we need to move the file
// into the project root and try again.
if (path.relative(root, moduleId).startsWith('..')) {
moduleId = await moveStaticRenderFunction(projectRoot, requiredModuleId);
}
return requireFileContentsWithMetro(root, devServerUrl, moduleId, props);
}
async function ensureFileInRootDirectory(projectRoot: string, otherFile: string) {
// Cannot be accessed using Metro's server API, we need to move the file
// into the project root and try again.
if (!path.relative(projectRoot, otherFile).startsWith('..' + path.sep)) {
return otherFile;
}
// Copy the file into the project to ensure it works in monorepos.
// This means the file cannot have any relative imports.
const tempDir = path.join(projectRoot, '.expo/static-tmp');
await fs.promises.mkdir(tempDir, { recursive: true });
const moduleId = path.join(tempDir, path.basename(otherFile));
await fs.promises.writeFile(moduleId, await fs.promises.readFile(otherFile, 'utf8'));
// Sleep to give watchman time to register the file.
await delayAsync(50);
return moduleId;
}
export async function createMetroEndpointAsync(
projectRoot: string,
devServerUrl: string,
absoluteFilePath: string,
props: PickPartial<ExpoMetroOptions, 'mainModuleName' | 'bytecode'>
): Promise<string> {
const root = getMetroServerRoot(projectRoot);
const safeOtherFile = await ensureFileInRootDirectory(projectRoot, absoluteFilePath);
const serverPath = path.relative(root, safeOtherFile).replace(/\.[jt]sx?$/, '');
const urlFragment = createBundleUrlPath({
mainModuleName: serverPath,
lazy: false,
asyncRoutes: false,
inlineSourceMap: false,
engine: 'hermes',
minify: false,
bytecode: false,
...props,
});
let url: string;
if (devServerUrl) {
url = new URL(urlFragment.replace(/^\//, ''), devServerUrl).toString();
} else {
url = '/' + urlFragment.replace(/^\/+/, '');
}
return url;
}
export async function requireFileContentsWithMetro(
projectRoot: string,
devServerUrl: string,
absoluteFilePath: string,
props: PickPartial<ExpoMetroOptions, 'mainModuleName' | 'bytecode'>
): Promise<{ src: string; filename: string }> {
const url = await createMetroEndpointAsync(projectRoot, devServerUrl, absoluteFilePath, props);
return await metroFetchAsync(projectRoot, url);
}
async function metroFetchAsync(
projectRoot: string,
url: string
): Promise<{ src: string; filename: string }> {
debug('Fetching from Metro:', url);
// TODO: Skip the dev server and use the Metro instance directly for better results, faster.
const res = await fetch(url);
// TODO: Improve error handling
if (res.status === 500) {
const text = await res.text();
if (text.startsWith('{"originModulePath"') || text.startsWith('{"type":"TransformError"')) {
const errorObject = JSON.parse(text);
throw new MetroNodeError(stripAnsi(errorObject.message) ?? errorObject.message, errorObject);
}
throw new Error(`[${res.status}]: ${res.statusText}\n${text}`);
}
if (!res.ok) {
throw new Error(`Error fetching bundle for static rendering: ${res.status} ${res.statusText}`);
}
const content = await res.text();
const map = await fetch(url.replace('.bundle?', '.map?')).then((r) => r.json());
cachedSourceMaps.set(url, { url: projectRoot, map });
return { src: wrapBundle(content), filename: url };
}
export async function getStaticRenderFunctionsForEntry<T = any>(
projectRoot: string,
devServerUrl: string,
options: PickPartial<ExpoMetroOptions, 'mainModuleName' | 'bytecode'>,
entry: string
) {
const { src: scriptContents, filename } = await getStaticRenderFunctionsContentAsync(
projectRoot,
devServerUrl,
options,
entry
);
return {
filename,
fn: await evalMetroAndWrapFunctions<T>(projectRoot, scriptContents, filename),
};
}
function evalMetroAndWrapFunctions<T = Record<string, any>>(
projectRoot: string,
script: string,
filename: string
): Promise<T> {
const contents = evalMetro(projectRoot, script, filename);
// wrap each function with a try/catch that uses Metro's error formatter
return Object.keys(contents).reduce((acc, key) => {
const fn = contents[key];
if (typeof fn !== 'function') {
return { ...acc, [key]: fn };
}
acc[key] = async function (...props: any[]) {
try {
return await fn.apply(this, props);
} catch (error: any) {
await logMetroError(projectRoot, { error });
throw new SilentError(error);
}
};
return acc;
}, {} as any);
}
export function evalMetro(projectRoot: string, src: string, filename: string) {
try {
return evalMetroNoHandling(projectRoot, src, filename);
} catch (error: any) {
// Format any errors that were thrown in the global scope of the evaluation.
if (error instanceof Error) {
logMetroErrorAsync({ projectRoot, error }).catch((internalError) => {
debug('Failed to log metro error:', internalError);
throw error;
});
} else {
throw error;
}
}
}
export function evalMetroNoHandling(projectRoot: string, src: string, filename: string) {
augmentLogs(projectRoot);
return profile(requireString, 'eval-metro-bundle')(src, filename);
}