MCPcopy Create free account
hub / github.com/vercel/vercel / readFile

Function readFile

packages/node/src/build.ts:238–301  ·  view source on GitHub ↗
(fsPath)

Source from the content-addressed store, hash-verified

236 },
237 ignore: config.excludeFiles,
238 async readFile(fsPath) {
239 const relPath = relative(baseDir, fsPath);
240
241 // If this file has already been read then return from the cache
242 const cached = sourceCache.get(relPath);
243 if (typeof cached !== 'undefined') return cached;
244
245 try {
246 let entry: File | undefined;
247 let source: string | Buffer = readFileSync(fsPath);
248
249 const { mode } = lstatSync(fsPath);
250 if (isSymbolicLink(mode)) {
251 entry = new FileFsRef({ fsPath, mode });
252 }
253
254 if (isEdgeFunction && basename(fsPath) === 'package.json') {
255 // For Edge Functions, patch "main" field to prefer "browser" or "module"
256 const pkgJson = JSON.parse(source.toString());
257 for (const prop of ['browser', 'module']) {
258 const val = pkgJson[prop];
259 if (typeof val === 'string') {
260 debug(`Using "${prop}" field in ${fsPath}`);
261 pkgJson.main = val;
262
263 // Create the `entry` with the original so that the output is unmodified
264 if (!entry) {
265 entry = new FileBlob({ data: source, mode });
266 }
267
268 // Return the modified `package.json` to nft
269 source = JSON.stringify(pkgJson);
270 break;
271 }
272 }
273 }
274
275 if (
276 (fsPath.endsWith('.ts') && !fsPath.endsWith('.d.ts')) ||
277 fsPath.endsWith('.tsx') ||
278 fsPath.endsWith('.mts') ||
279 fsPath.endsWith('.cts')
280 ) {
281 source = compileTypeScript(fsPath, source.toString());
282 }
283
284 if (!entry) {
285 entry = new FileBlob({ data: source, mode });
286 }
287 fsCache.set(relPath, entry);
288 sourceCache.set(relPath, source);
289 return source;
290 } catch (error: unknown) {
291 if (
292 isErrnoException(error) &&
293 (error.code === 'ENOENT' || error.code === 'EISDIR')
294 ) {
295 // `null` represents a not found

Callers 15

parseVercelConfigFunction · 0.50
maybeReadFunction · 0.50
buildFileTreeFunction · 0.50
readFileOrNullFunction · 0.50
frameworks.tsFile · 0.50
cli-config.test.tsFile · 0.50
renameHandlerFunctionFunction · 0.50
copyEntrypointFunction · 0.50
copyDevServerFunction · 0.50
writeEntrypointFunction · 0.50
writeGoModFunction · 0.50

Calls 9

relativeFunction · 0.90
isSymbolicLinkFunction · 0.90
debugFunction · 0.90
isErrnoExceptionFunction · 0.90
compileTypeScriptFunction · 0.85
getMethod · 0.65
parseMethod · 0.45
toStringMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected