|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import ts from 'typescript'; |
| 10 | + |
| 11 | +import {NgCompiler, NgCompilerHost} from './core'; |
| 12 | +import {NgCompilerOptions} from './core/api'; |
| 13 | +import {TrackedIncrementalBuildStrategy} from './incremental'; |
| 14 | +import {ActivePerfRecorder, PerfPhase} from './perf'; |
| 15 | +import {TsCreateProgramDriver} from './program_driver'; |
| 16 | +import { |
| 17 | + CompilationTicket, |
| 18 | + freshCompilationTicket, |
| 19 | + incrementalFromCompilerTicket, |
| 20 | +} from './core/src/compiler'; |
| 21 | + |
| 22 | +/** |
| 23 | + * A driver for the Angular Compiler that performs "Source-to-Source" transformation. |
| 24 | + * |
| 25 | + * Unlike `NgtscProgram`, this driver does NOT use `program.emit()`. Instead, it: |
| 26 | + * 1. Analyzes the program using `NgCompiler`. |
| 27 | + * 2. Manually runs `ts.transform` with Angular's Ivy transformers. |
| 28 | + * 3. Prints the transformed AST back to a TypeScript string. |
| 29 | + * |
| 30 | + * This mode is designed for a mode where the Angular Compiler |
| 31 | + * acts as a pre-processor for a downstream TypeScript compiler. |
| 32 | + */ |
| 33 | +export class NgtscIsolatedPreprocessor { |
| 34 | + readonly compiler: NgCompiler; |
| 35 | + private tsProgram: ts.Program; |
| 36 | + private host: NgCompilerHost; |
| 37 | + private incrementalStrategy: TrackedIncrementalBuildStrategy; |
| 38 | + |
| 39 | + constructor( |
| 40 | + rootNames: ReadonlyArray<string>, |
| 41 | + private options: NgCompilerOptions, |
| 42 | + delegateHost: ts.CompilerHost, |
| 43 | + oldProgram?: NgtscIsolatedPreprocessor, |
| 44 | + ) { |
| 45 | + // Enable type reification for source-to-source transformation |
| 46 | + this.options = {...options, '_experimentalEmitIntermediateTs': true}; |
| 47 | + |
| 48 | + const perfRecorder = ActivePerfRecorder.zeroedToNow(); |
| 49 | + perfRecorder.phase(PerfPhase.Setup); |
| 50 | + |
| 51 | + const reuseProgram = oldProgram?.compiler.getCurrentProgram(); |
| 52 | + this.host = NgCompilerHost.wrap(delegateHost, rootNames, options, reuseProgram ?? null); |
| 53 | + |
| 54 | + this.tsProgram = ts.createProgram(this.host.inputFiles, options, this.host, reuseProgram); |
| 55 | + |
| 56 | + const programDriver = new TsCreateProgramDriver( |
| 57 | + this.tsProgram, |
| 58 | + this.host, |
| 59 | + this.options, |
| 60 | + this.host.shimExtensionPrefixes, |
| 61 | + ); |
| 62 | + |
| 63 | + this.incrementalStrategy = |
| 64 | + oldProgram !== undefined |
| 65 | + ? oldProgram.incrementalStrategy.toNextBuildStrategy() |
| 66 | + : new TrackedIncrementalBuildStrategy(); |
| 67 | + |
| 68 | + let ticket: CompilationTicket; |
| 69 | + if (oldProgram === undefined) { |
| 70 | + ticket = freshCompilationTicket( |
| 71 | + this.tsProgram, |
| 72 | + this.options, |
| 73 | + this.incrementalStrategy, |
| 74 | + programDriver, |
| 75 | + perfRecorder, |
| 76 | + /* enableTemplateTypeChecker */ !!this.options._enableTemplateTypeChecker, |
| 77 | + /* usePoisonedData */ false, |
| 78 | + ); |
| 79 | + } else { |
| 80 | + ticket = incrementalFromCompilerTicket( |
| 81 | + oldProgram.compiler, |
| 82 | + this.tsProgram, |
| 83 | + this.incrementalStrategy, |
| 84 | + programDriver, |
| 85 | + new Set(), // TODO: track modified resource files |
| 86 | + perfRecorder, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + this.compiler = NgCompiler.fromTicket(ticket, this.host); |
| 91 | + } |
| 92 | + |
| 93 | + transformAndPrint(): {fileName: string; content: string}[] { |
| 94 | + // 1. Generate TCBs |
| 95 | + this.compiler['ensureAnalyzed']().templateTypeChecker.generateAllTypeCheckBlocks(); |
| 96 | + |
| 97 | + const transformers = this.compiler.prepareEmit().transformers; |
| 98 | + // We only care about 'before' transformers for source-to-source |
| 99 | + const beforeTransformers = (transformers.before || |
| 100 | + []) as unknown as ts.TransformerFactory<ts.SourceFile>[]; |
| 101 | + |
| 102 | + const result: {fileName: string; content: string}[] = []; |
| 103 | + const printer = ts.createPrinter({ |
| 104 | + newLine: ts.NewLineKind.LineFeed, |
| 105 | + removeComments: false, |
| 106 | + }); |
| 107 | + |
| 108 | + const program = this.compiler.getCurrentProgram(); |
| 109 | + |
| 110 | + for (const sf of program.getSourceFiles()) { |
| 111 | + if (sf.isDeclarationFile) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + // If it is a TCB file (ends in .ngtypecheck.ts), we want it as-is, without any transformations. |
| 116 | + if (sf.fileName.endsWith('.ngtypecheck.ts')) { |
| 117 | + const content = printer.printFile(sf); |
| 118 | + result.push({fileName: sf.fileName, content}); |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + // Manually transform the source file |
| 123 | + const transformationResult = ts.transform(sf, beforeTransformers, this.options); |
| 124 | + |
| 125 | + if (transformationResult.transformed.length !== 1) { |
| 126 | + transformationResult.dispose(); |
| 127 | + throw new Error( |
| 128 | + `Expected exactly one transformed file, got ${transformationResult.transformed.length}`, |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + const transformedSf = transformationResult.transformed[0]; |
| 133 | + |
| 134 | + const content = printer.printFile(transformedSf); |
| 135 | + result.push({fileName: sf.fileName, content}); |
| 136 | + |
| 137 | + transformationResult.dispose(); |
| 138 | + |
| 139 | + this.compiler.incrementalCompilation.recordSuccessfulEmit(sf); |
| 140 | + } |
| 141 | + |
| 142 | + return result; |
| 143 | + } |
| 144 | +} |
0 commit comments