1/** 2 * Copyright © 2022 650 Industries. 3 * 4 * This source code is licensed under the MIT license found in the 5 * LICENSE file in the root directory of this source tree. 6 */ 7import fs from 'fs'; 8import fetch from 'node-fetch'; 9import path from 'path'; 10import requireString from 'require-from-string'; 11import resolveFrom from 'resolve-from'; 12 13import { delayAsync } from '../../utils/delay'; 14import { memoize } from '../../utils/fn'; 15import { profile } from '../../utils/profile'; 16import { getMetroServerRoot } from './middleware/ManifestMiddleware'; 17 18const debug = require('debug')('expo:start:server:node-renderer') as typeof console.log; 19 20function wrapBundle(str: string) { 21 // Skip the metro runtime so debugging is a bit easier. 22 // Replace the __r() call with an export statement. 23 return str.replace(/^(__r\(.*\);)$/m, 'module.exports = $1'); 24} 25 26function stripProcess(str: string) { 27 // TODO: Remove from the metro prelude 28 return str.replace(/process=this\.process\|\|{},/m, ''); 29} 30 31// TODO(EvanBacon): Group all the code together and version. 32const getRenderModuleId = (projectRoot: string): string => { 33 const moduleId = resolveFrom.silent(projectRoot, 'expo-router/node/render.js'); 34 if (!moduleId) { 35 throw new Error( 36 `A version of expo-router with Node.js support is not installed in the project.` 37 ); 38 } 39 40 return moduleId; 41}; 42 43type StaticRenderOptions = { 44 // Ensure the style format is `css-xxxx` (prod) instead of `css-view-xxxx` (dev) 45 dev?: boolean; 46 minify?: boolean; 47 platform?: string; 48 environment?: 'node'; 49}; 50 51const moveStaticRenderFunction = memoize(async (projectRoot: string, requiredModuleId: string) => { 52 // Copy the file into the project to ensure it works in monorepos. 53 // This means the file cannot have any relative imports. 54 const tempDir = path.join(projectRoot, '.expo/static'); 55 await fs.promises.mkdir(tempDir, { recursive: true }); 56 const moduleId = path.join(tempDir, 'render.js'); 57 await fs.promises.writeFile(moduleId, await fs.promises.readFile(requiredModuleId, 'utf8')); 58 // Sleep to give watchman time to register the file. 59 await delayAsync(50); 60 return moduleId; 61}); 62 63/** @returns the js file contents required to generate the static generation function. */ 64export async function getStaticRenderFunctionsContentAsync( 65 projectRoot: string, 66 devServerUrl: string, 67 { dev = false, minify = false, environment }: StaticRenderOptions = {} 68): Promise<string> { 69 const root = getMetroServerRoot(projectRoot); 70 const requiredModuleId = getRenderModuleId(root); 71 let moduleId = requiredModuleId; 72 73 // Cannot be accessed using Metro's server API, we need to move the file 74 // into the project root and try again. 75 if (path.relative(root, moduleId).startsWith('..')) { 76 moduleId = await moveStaticRenderFunction(projectRoot, requiredModuleId); 77 } 78 79 return requireFileContentsWithMetro(root, devServerUrl, moduleId, { dev, minify, environment }); 80} 81 82async function ensureFileInRootDirectory(projectRoot: string, otherFile: string) { 83 // Cannot be accessed using Metro's server API, we need to move the file 84 // into the project root and try again. 85 if (!path.relative(projectRoot, otherFile).startsWith('../')) { 86 return otherFile; 87 } 88 89 // Copy the file into the project to ensure it works in monorepos. 90 // This means the file cannot have any relative imports. 91 const tempDir = path.join(projectRoot, '.expo/static-tmp'); 92 await fs.promises.mkdir(tempDir, { recursive: true }); 93 const moduleId = path.join(tempDir, path.basename(otherFile)); 94 await fs.promises.writeFile(moduleId, await fs.promises.readFile(otherFile, 'utf8')); 95 // Sleep to give watchman time to register the file. 96 await delayAsync(50); 97 return moduleId; 98} 99 100export async function requireFileContentsWithMetro( 101 projectRoot: string, 102 devServerUrl: string, 103 absoluteFilePath: string, 104 { dev = false, platform = 'web', minify = false, environment }: StaticRenderOptions = {} 105): Promise<string> { 106 const root = getMetroServerRoot(projectRoot); 107 const safeOtherFile = await ensureFileInRootDirectory(projectRoot, absoluteFilePath); 108 const serverPath = path.relative(root, safeOtherFile).replace(/\.[jt]sx?$/, '.bundle'); 109 debug('fetching from Metro:', root, serverPath); 110 111 let url = `${devServerUrl}/${serverPath}?platform=${platform}&dev=${dev}&minify=${minify}`; 112 113 if (environment) { 114 url += `&resolver.environment=${environment}&transform.environment=${environment}`; 115 } 116 117 const res = await fetch(url); 118 119 // TODO: Improve error handling 120 if (res.status === 500) { 121 const text = await res.text(); 122 if (text.startsWith('{"originModulePath"')) { 123 const errorObject = JSON.parse(text); 124 throw new Error(errorObject.message); 125 } 126 throw new Error(`[${res.status}]: ${res.statusText}\n${text}`); 127 } 128 129 if (!res.ok) { 130 throw new Error(`Error fetching bundle for static rendering: ${res.status} ${res.statusText}`); 131 } 132 133 const content = await res.text(); 134 135 let bun = wrapBundle(content); 136 137 // This exposes the entire environment to the bundle. 138 if (environment === 'node') { 139 bun = stripProcess(bun); 140 } 141 142 return bun; 143} 144export async function requireWithMetro<T>( 145 projectRoot: string, 146 devServerUrl: string, 147 absoluteFilePath: string, 148 options: StaticRenderOptions = {} 149): Promise<T> { 150 const content = await requireFileContentsWithMetro( 151 projectRoot, 152 devServerUrl, 153 absoluteFilePath, 154 options 155 ); 156 157 return profile(requireString, 'eval-metro-bundle')(content); 158} 159 160export async function getStaticRenderFunctions( 161 projectRoot: string, 162 devServerUrl: string, 163 options: StaticRenderOptions = {} 164): Promise<any> { 165 const scriptContents = await getStaticRenderFunctionsContentAsync( 166 projectRoot, 167 devServerUrl, 168 options 169 ); 170 return profile(requireString, 'eval-metro-bundle')(scriptContents); 171} 172 173export async function getStaticPageContentsAsync( 174 projectRoot: string, 175 devServerUrl: string, 176 options: StaticRenderOptions = {} 177) { 178 const scriptContents = await getStaticRenderFunctionsContentAsync( 179 projectRoot, 180 devServerUrl, 181 options 182 ); 183 184 const { 185 getStaticContent, 186 // getDataLoader 187 } = profile(requireString, 'eval-metro-bundle')(scriptContents); 188 189 return function loadPageAsync(url: URL) { 190 // const fetchData = getDataLoader(url); 191 192 return { 193 fetchData: false, 194 scriptContents, 195 renderAsync: () => getStaticContent(url), 196 }; 197 }; 198} 199