| 45 | } |
| 46 | |
| 47 | export function compileFile( |
| 48 | workspace: Workspace, |
| 49 | syntaxCheck: boolean, |
| 50 | options: NativeCompilerOptions, |
| 51 | inputFile: string, |
| 52 | modulePath: string, |
| 53 | ): NativeCompilerIR.Base[] { |
| 54 | const openedFile = workspace.getOpenedFile(inputFile); |
| 55 | const sourceFile = openedFile.sourceFile; |
| 56 | const typeChecker = openedFile.workspaceProject.typeChecker; |
| 57 | |
| 58 | if (syntaxCheck) { |
| 59 | const diagnostics = workspace.getDiagnosticsSync(inputFile); |
| 60 | checkErrors(sourceFile, diagnostics.diagnostics); |
| 61 | } |
| 62 | |
| 63 | if (Workspace.requiresTSXProcessor(inputFile)) { |
| 64 | // If we need the TSX processor, we need to go through the regular TS compiler |
| 65 | // to transpile the TSX AST before we compile the output using the NativeCompiler |
| 66 | |
| 67 | const cancelToken = new CancelationTokenImpl(); |
| 68 | |
| 69 | const outputIR: NativeCompilerIR.Base[] = []; |
| 70 | const transforms: ts.TransformerFactory<ts.SourceFile>[] = [ |
| 71 | (context) => (node) => { |
| 72 | const compiler = new NativeCompiler(node, inputFile, modulePath, typeChecker, options); |
| 73 | outputIR.push(...compiler.compile()); |
| 74 | |
| 75 | // Once we are done compiled with the NativeCompiler, we can ask the TS compiler to stop |
| 76 | // processing this file. We don't need TS to JS compilation. |
| 77 | cancelToken.requestCancellation(); |
| 78 | |
| 79 | // Return empty source file as we finished the processing |
| 80 | return context.factory.updateSourceFile( |
| 81 | node, |
| 82 | [], |
| 83 | node.isDeclarationFile, |
| 84 | undefined, |
| 85 | undefined, |
| 86 | undefined, |
| 87 | undefined, |
| 88 | ); |
| 89 | return node; |
| 90 | }, |
| 91 | ]; |
| 92 | |
| 93 | workspace.doEmitFile(inputFile, cancelToken, { |
| 94 | before: transforms, |
| 95 | }); |
| 96 | |
| 97 | return outputIR; |
| 98 | } else { |
| 99 | const compiler = new NativeCompiler(sourceFile, inputFile, modulePath, typeChecker, options); |
| 100 | return compiler.compile(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function generateNativeModuleName(relativePaths: string[]): string { |