|
| 1 | +import { readFileSync } from 'fs'; |
| 2 | +import { extname } from 'path'; |
| 3 | + |
| 4 | +import 'reflect-metadata'; |
| 5 | +import { CompilerOptions, createProgram, ParsedCommandLine, Program, transpileModule, TranspileOptions, TranspileOutput } from 'typescript'; |
| 6 | +import { CodeGenerator, NgcCliOptions, NodeReflectorHostContext, ReflectorHost, StaticReflector }from '@angular/compiler-cli'; |
| 7 | +import { tsc } from '@angular/tsc-wrapped/src/tsc'; |
| 8 | +import AngularCompilerOptions from '@angular/tsc-wrapped/src/options'; |
| 9 | + |
| 10 | +import { HybridFileSystem } from '../util/hybrid-file-system'; |
| 11 | +import { getInstance as getHybridFileSystem } from '../util/hybrid-file-system-factory'; |
| 12 | +import { getInstance } from '../aot/compiler-host-factory'; |
| 13 | +import { NgcCompilerHost } from '../aot/compiler-host'; |
| 14 | +import { patchReflectorHost } from '../aot/reflector-host'; |
| 15 | +import { removeDecorators } from '../util/typescript-utils'; |
| 16 | +import { getMainFileTypescriptContentForAotBuild } from './utils'; |
| 17 | +import { printDiagnostics, clearDiagnostics, DiagnosticsType } from '../logger/logger-diagnostics'; |
| 18 | +import { runTypeScriptDiagnostics } from '../logger/logger-typescript'; |
| 19 | +import { BuildError } from '../util/errors'; |
| 20 | +import { changeExtension } from '../util/helpers'; |
| 21 | +import { BuildContext } from '../util/interfaces'; |
| 22 | + |
| 23 | +export class AotCompiler { |
| 24 | + |
| 25 | + private tsConfig: ParsedTsConfig; |
| 26 | + private angularCompilerOptions: AngularCompilerOptions; |
| 27 | + private program: Program; |
| 28 | + private reflector: StaticReflector; |
| 29 | + private reflectorHost: ReflectorHost; |
| 30 | + private compilerHost: NgcCompilerHost; |
| 31 | + private fileSystem: HybridFileSystem; |
| 32 | + |
| 33 | + constructor(private context: BuildContext, private options: AotOptions) { |
| 34 | + this.tsConfig = getNgcConfig(this.context, this.options.tsConfigPath); |
| 35 | + |
| 36 | + this.angularCompilerOptions = Object.assign({}, this.tsConfig.ngOptions, { |
| 37 | + basePath: this.options.rootDir, |
| 38 | + entryPoint: this.options.entryPoint |
| 39 | + }); |
| 40 | + |
| 41 | + this.fileSystem = getHybridFileSystem(); |
| 42 | + this.compilerHost = getInstance(this.tsConfig.parsed.options); |
| 43 | + this.program = createProgram(this.tsConfig.parsed.fileNames, this.tsConfig.parsed.options, this.compilerHost); |
| 44 | + this.reflectorHost = new ReflectorHost(this.program, this.compilerHost, this.angularCompilerOptions); |
| 45 | + this.reflector = new StaticReflector(this.reflectorHost); |
| 46 | + } |
| 47 | + |
| 48 | + compile() { |
| 49 | + clearDiagnostics(this.context, DiagnosticsType.TypeScript); |
| 50 | + const i18nOptions: NgcCliOptions = { |
| 51 | + i18nFile: undefined, |
| 52 | + i18nFormat: undefined, |
| 53 | + locale: undefined, |
| 54 | + basePath: this.options.rootDir |
| 55 | + }; |
| 56 | + |
| 57 | + // Create the Code Generator. |
| 58 | + const codeGenerator = CodeGenerator.create( |
| 59 | + this.angularCompilerOptions, |
| 60 | + i18nOptions, |
| 61 | + this.program, |
| 62 | + this.compilerHost, |
| 63 | + new NodeReflectorHostContext(this.compilerHost) |
| 64 | + ); |
| 65 | + |
| 66 | + // We need to temporarily patch the CodeGenerator until either it's patched or allows us |
| 67 | + // to pass in our own ReflectorHost. |
| 68 | + patchReflectorHost(codeGenerator); |
| 69 | + return codeGenerator.codegen({transitiveModules: true}) |
| 70 | + .then(() => { |
| 71 | + // Create a new Program, based on the old one. This will trigger a resolution of all |
| 72 | + // transitive modules, which include files that might just have been generated. |
| 73 | + this.program = createProgram(this.tsConfig.parsed.fileNames, this.tsConfig.parsed.options, this.compilerHost, this.program); |
| 74 | + |
| 75 | + const tsDiagnostics = this.program.getSyntacticDiagnostics() |
| 76 | + .concat(this.program.getSemanticDiagnostics()) |
| 77 | + .concat(this.program.getOptionsDiagnostics()); |
| 78 | + |
| 79 | + if (tsDiagnostics.length) { |
| 80 | + throw tsDiagnostics; |
| 81 | + } |
| 82 | + }) |
| 83 | + .then(() => { |
| 84 | + for ( const fileName of this.tsConfig.parsed.fileNames) { |
| 85 | + const content = readFileSync(fileName).toString(); |
| 86 | + this.context.fileCache.set(fileName, { path: fileName, content: content}); |
| 87 | + } |
| 88 | + }) |
| 89 | + .then(() => { |
| 90 | + const mainContent = getMainFileTypescriptContentForAotBuild(); |
| 91 | + this.context.fileCache.set(this.options.entryPoint, { path: this.options.entryPoint, content: mainContent}); |
| 92 | + }) |
| 93 | + .then(() => { |
| 94 | + const tsFiles = this.context.fileCache.getAll().filter(file => extname(file.path) === '.ts' && file.path.indexOf('.d.ts') === -1); |
| 95 | + for (const tsFile of tsFiles) { |
| 96 | + const cleanedFileContent = removeDecorators(tsFile.path, tsFile.content); |
| 97 | + tsFile.content = cleanedFileContent; |
| 98 | + const transpileOutput = this.transpileFileContent(tsFile.path, cleanedFileContent, this.tsConfig.parsed.options); |
| 99 | + const diagnostics = runTypeScriptDiagnostics(this.context, transpileOutput.diagnostics); |
| 100 | + if (diagnostics.length) { |
| 101 | + // darn, we've got some things wrong, transpile failed :( |
| 102 | + printDiagnostics(this.context, DiagnosticsType.TypeScript, diagnostics, true, true); |
| 103 | + throw new BuildError(); |
| 104 | + } |
| 105 | + |
| 106 | + const jsFilePath = changeExtension(tsFile.path, '.js'); |
| 107 | + this.fileSystem.addVirtualFile(jsFilePath, transpileOutput.outputText); |
| 108 | + this.fileSystem.addVirtualFile(jsFilePath + '.map', transpileOutput.sourceMapText); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + transpileFileContent(fileName: string, sourceText: string, options: CompilerOptions): TranspileOutput { |
| 114 | + const sourceFile = this.program.getSourceFile(fileName); |
| 115 | + const diagnostics = this.program.getSyntacticDiagnostics(sourceFile) |
| 116 | + .concat(this.program.getSemanticDiagnostics(sourceFile)) |
| 117 | + .concat(this.program.getDeclarationDiagnostics(sourceFile)); |
| 118 | + if (diagnostics.length) { |
| 119 | + throw diagnostics; |
| 120 | + } |
| 121 | + |
| 122 | + const transpileOptions: TranspileOptions = { |
| 123 | + compilerOptions: options, |
| 124 | + fileName: fileName, |
| 125 | + reportDiagnostics: true |
| 126 | + }; |
| 127 | + |
| 128 | + return transpileModule(sourceText, transpileOptions); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export interface AotOptions { |
| 133 | + tsConfigPath: string; |
| 134 | + rootDir: string; |
| 135 | + entryPoint: string; |
| 136 | +} |
| 137 | + |
| 138 | +export function getNgcConfig(context: BuildContext, tsConfigPath?: string): ParsedTsConfig { |
| 139 | + |
| 140 | + const tsConfigFile = tsc.readConfiguration(tsConfigPath, process.cwd()); |
| 141 | + if (!tsConfigFile) { |
| 142 | + throw new BuildError(`tsconfig: invalid tsconfig file, "${tsConfigPath}"`); |
| 143 | + |
| 144 | + } |
| 145 | + return tsConfigFile; |
| 146 | +} |
| 147 | + |
| 148 | +export interface ParsedTsConfig { |
| 149 | + parsed: ParsedCommandLine; |
| 150 | + ngOptions: AngularCompilerOptions; |
| 151 | +} |
0 commit comments