From b00b80a45bd78b5fc5f695ccfc41d15f48dbde6d Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 5 Jun 2017 11:34:25 -0700 Subject: [PATCH] feat(compiler-cli): introduce synchronous codegen API --- packages/compiler-cli/src/codegen.ts | 29 ++++++++++++++-------- packages/compiler-cli/src/compiler_host.ts | 4 ++- packages/compiler/src/i18n/extractor.ts | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/compiler-cli/src/codegen.ts b/packages/compiler-cli/src/codegen.ts index 25a3bac8a6..47c0f96e49 100644 --- a/packages/compiler-cli/src/codegen.ts +++ b/packages/compiler-cli/src/codegen.ts @@ -40,17 +40,24 @@ export class CodeGenerator { return this.compiler .analyzeModulesAsync(this.program.getSourceFiles().map( sf => this.ngCompilerHost.getCanonicalFileName(sf.fileName))) - .then(analyzedModules => this.compiler.emitAllImpls(analyzedModules)) - .then(generatedModules => { - return generatedModules.map(generatedModule => { - const sourceFile = this.program.getSourceFile(generatedModule.srcFileUrl); - const emitPath = this.ngCompilerHost.calculateEmitPath(generatedModule.genFileUrl); - const source = - generatedModule.source || compiler.toTypeScript(generatedModule, PREAMBLE); - this.host.writeFile(emitPath, source, false, () => {}, [sourceFile]); - return emitPath; - }); - }); + .then(analyzedModules => this.emit(analyzedModules)); + } + + codegenSync(): string[] { + const analyzed = this.compiler.analyzeModulesSync(this.program.getSourceFiles().map( + sf => this.ngCompilerHost.getCanonicalFileName(sf.fileName))); + return this.emit(analyzed); + } + + private emit(analyzedModules: compiler.NgAnalyzedModules) { + const generatedModules = this.compiler.emitAllImpls(analyzedModules); + return generatedModules.map(generatedModule => { + const sourceFile = this.program.getSourceFile(generatedModule.srcFileUrl); + const emitPath = this.ngCompilerHost.calculateEmitPath(generatedModule.genFileUrl); + const source = generatedModule.source || compiler.toTypeScript(generatedModule, PREAMBLE); + this.host.writeFile(emitPath, source, false, () => {}, [sourceFile]); + return emitPath; + }); } static create( diff --git a/packages/compiler-cli/src/compiler_host.ts b/packages/compiler-cli/src/compiler_host.ts index afb1d6f4a8..41d781a9ad 100644 --- a/packages/compiler-cli/src/compiler_host.ts +++ b/packages/compiler-cli/src/compiler_host.ts @@ -258,7 +258,9 @@ export class CompilerHost implements AotCompilerHost { return v3Metadata; } - loadResource(filePath: string): Promise { return this.context.readResource(filePath); } + loadResource(filePath: string): Promise|string { + return this.context.readResource(filePath); + } loadSummary(filePath: string): string|null { if (this.context.fileExists(filePath)) { diff --git a/packages/compiler/src/i18n/extractor.ts b/packages/compiler/src/i18n/extractor.ts index 5b4c2922ef..9af0ac30d4 100644 --- a/packages/compiler/src/i18n/extractor.ts +++ b/packages/compiler/src/i18n/extractor.ts @@ -41,7 +41,7 @@ export interface ExtractorHost extends StaticSymbolResolverHost, AotSummaryResol /** * Loads a resource (e.g. html / css) */ - loadResource(path: string): Promise; + loadResource(path: string): Promise|string; } export class Extractor {