(args: NodeFileTraceOptions)
| 10 | import type { NodeFileTraceOptions } from './types.js'; |
| 11 | |
| 12 | export const nodeFileTrace = async (args: NodeFileTraceOptions) => { |
| 13 | const { span } = args; |
| 14 | const files: Files = {}; |
| 15 | const { tracedPaths } = args; |
| 16 | |
| 17 | // Rolldown builds source files into the outDir, node_modules are not included. |
| 18 | const compiledSourceFiles = await glob('**/*', { |
| 19 | cwd: args.outDir, |
| 20 | follow: true, |
| 21 | includeDirectories: true, |
| 22 | }); |
| 23 | for (const file of Object.keys(compiledSourceFiles)) { |
| 24 | files[file] = compiledSourceFiles[file]; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * While we're not using NFT to process source code, we are using it |
| 29 | * to tree shake node deps, and include any fs reads for files that are |
| 30 | * not part of the traced paths or compiled source files. |
| 31 | * Most of this is identical to the `@vercel/node` implementation |
| 32 | */ |
| 33 | const runNft = () => |
| 34 | nft(Array.from(tracedPaths), { |
| 35 | base: args.repoRootPath, |
| 36 | processCwd: args.workPath, |
| 37 | ts: true, |
| 38 | mixedModules: true, |
| 39 | moduleSyncCatchall: true, |
| 40 | async resolve(id, parent, job, cjsResolve) { |
| 41 | return nftResolveDependency(id, parent, job, cjsResolve); |
| 42 | }, |
| 43 | async readFile(fsPath) { |
| 44 | try { |
| 45 | let source: string | Buffer = await readFile(fsPath); |
| 46 | |
| 47 | // NFT doesn't support TypeScript, so we need to transform the source code. |
| 48 | if ( |
| 49 | (fsPath.endsWith('.ts') && !fsPath.endsWith('.d.ts')) || |
| 50 | fsPath.endsWith('.tsx') || |
| 51 | fsPath.endsWith('.mts') || |
| 52 | fsPath.endsWith('.cts') |
| 53 | ) { |
| 54 | const transformResult = await transform(fsPath, source.toString()); |
| 55 | source = transformResult.code; |
| 56 | } |
| 57 | |
| 58 | return source; |
| 59 | } catch (error: unknown) { |
| 60 | if ( |
| 61 | isNativeError(error) && |
| 62 | 'code' in error && |
| 63 | (error.code === 'ENOENT' || error.code === 'EISDIR') |
| 64 | ) { |
| 65 | return null; |
| 66 | } |
| 67 | throw error; |
| 68 | } |
| 69 | }, |
no test coverage detected