Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
fix(angular): support angular 2.3+ ngc api
Browse files Browse the repository at this point in the history
  • Loading branch information
danbucholtz committed Jan 27, 2017
1 parent b76c21b commit 13e930a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 54 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@
"xml2js": "^0.4.17"
},
"devDependencies": {
"@angular/common": "2.2.1",
"@angular/compiler": "2.2.1",
"@angular/compiler-cli": "2.2.1",
"@angular/core": "2.2.1",
"@angular/forms": "2.2.1",
"@angular/http": "2.2.1",
"@angular/platform-browser": "2.2.1",
"@angular/platform-browser-dynamic": "2.2.1",
"@angular/platform-server": "2.2.1",
"@angular/common": "4.0.0-beta.5",
"@angular/compiler": "4.0.0-beta.5",
"@angular/compiler-cli": "4.0.0-beta.5",
"@angular/core": "4.0.0-beta.5",
"@angular/forms": "4.0.0-beta.5",
"@angular/http": "4.0.0-beta.5",
"@angular/platform-browser": "4.0.0-beta.5",
"@angular/platform-browser-dynamic": "4.0.0-beta.5",
"@angular/platform-server": "4.0.0-beta.5",
"@types/chalk": "^0.4.30",
"@types/chokidar": "1.4.29",
"@types/clean-css": "^3.4.29",
Expand Down
28 changes: 9 additions & 19 deletions src/aot/aot-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { basename, dirname, extname, join, normalize, relative, resolve } from '

import 'reflect-metadata';
import { CompilerOptions, createProgram, ParsedCommandLine, Program, transpileModule, TranspileOptions, TranspileOutput } from 'typescript';
import { CodeGenerator, NgcCliOptions, NodeReflectorHostContext, ReflectorHost, StaticReflector }from '@angular/compiler-cli';
import { NgcCliOptions }from '@angular/compiler-cli';
import { tsc } from '@angular/tsc-wrapped/src/tsc';
import AngularCompilerOptions from '@angular/tsc-wrapped/src/options';

import { HybridFileSystem } from '../util/hybrid-file-system';
import { getInstance as getHybridFileSystem } from '../util/hybrid-file-system-factory';
import { getInstance } from './compiler-host-factory';
import { NgcCompilerHost } from './compiler-host';
import { patchReflectorHost } from './reflector-host';
import { getFallbackMainContent, replaceBootstrap } from './utils';
import { Logger } from '../logger/logger';
import { printDiagnostics, clearDiagnostics, DiagnosticsType } from '../logger/logger-diagnostics';
Expand All @@ -21,13 +20,13 @@ import { BuildError } from '../util/errors';
import { changeExtension } from '../util/helpers';
import { BuildContext } from '../util/interfaces';

import { doCodegen } from './codegen';

export class AotCompiler {

private tsConfig: ParsedTsConfig;
private angularCompilerOptions: AngularCompilerOptions;
private program: Program;
private reflector: StaticReflector;
private reflectorHost: ReflectorHost;
private compilerHost: NgcCompilerHost;
private fileSystem: HybridFileSystem;
private lazyLoadedModuleDictionary: any;
Expand All @@ -43,8 +42,6 @@ export class AotCompiler {
this.fileSystem = getHybridFileSystem();
this.compilerHost = getInstance(this.tsConfig.parsed.options);
this.program = createProgram(this.tsConfig.parsed.fileNames, this.tsConfig.parsed.options, this.compilerHost);
this.reflectorHost = new ReflectorHost(this.program, this.compilerHost, this.angularCompilerOptions);
this.reflector = new StaticReflector(this.reflectorHost);
}

compile(): Promise<void> {
Expand All @@ -58,20 +55,13 @@ export class AotCompiler {
basePath: this.options.rootDir
};

// Create the Code Generator.
const codeGenerator = CodeGenerator.create(
this.angularCompilerOptions,
i18nOptions,
this.program,
this.compilerHost,
new NodeReflectorHostContext(this.compilerHost)
);

// We need to temporarily patch the CodeGenerator until either it's patched or allows us
// to pass in our own ReflectorHost.
patchReflectorHost(codeGenerator);
Logger.debug('[AotCompiler] compile: starting codegen ... ');
return codeGenerator.codegen({transitiveModules: true});
return doCodegen({
angularCompilerOptions: this.angularCompilerOptions,
cliOptions: i18nOptions,
program: this.program,
compilerHost: this.compilerHost
});
}).then(() => {
Logger.debug('[AotCompiler] compile: starting codegen ... DONE');
Logger.debug('[AotCompiler] compile: Creating and validating new TypeScript Program ...');
Expand Down
13 changes: 13 additions & 0 deletions src/aot/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CodegenOptions } from '../util/interfaces';
import { runCodegen, isAngular22 } from './codegen/codegen-ng22';
import { runCodegen as runCodegen23 } from './codegen/codegen-ng23plus';


export function doCodegen(options: CodegenOptions) {
if (isAngular22()) {
return runCodegen(options);
}
return runCodegen23(options);
}


23 changes: 23 additions & 0 deletions src/aot/codegen/codegen-ng22.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as CompilerCLI from '@angular/compiler-cli';
import { CodegenOptions } from '../../util/interfaces';

export function isAngular22() {
if ((CompilerCLI as any).NodeReflectorHostContext) {
return true;
}
return false;
}

export function runCodegen(options: CodegenOptions) {
const NodeReflectorHostContextConstructor = (CompilerCLI as any).NodeReflectorHostContext;
const instance = new NodeReflectorHostContextConstructor(options.compilerHost);
const codeGenerator = CompilerCLI.CodeGenerator.create(options.angularCompilerOptions,
options.cliOptions,
options.program,
options.compilerHost,
instance);

// angular 2.2 api to codegen
return (codeGenerator.codegen as any)({transitiveModules: true});
}

22 changes: 22 additions & 0 deletions src/aot/codegen/codegen-ng23plus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as CompilerCLI from '@angular/compiler-cli';
import { CodegenOptions } from '../../util/interfaces';

export function isAngular23Plus() {
if ((CompilerCLI as any).NodeCompilerHostContext) {
return true;
}
return false;
}

export function runCodegen(options: CodegenOptions) {
const NodeCompilerHostContext = (CompilerCLI as any).NodeCompilerHostContext;
const instance = new NodeCompilerHostContext();
const codeGenerator = CompilerCLI.CodeGenerator.create(options.angularCompilerOptions,
options.cliOptions,
options.program,
options.compilerHost,
instance);

// angular 2.3+ api for codegen does not take any options
return (codeGenerator.codegen as any)();
}
25 changes: 0 additions & 25 deletions src/aot/reflector-host.ts

This file was deleted.

13 changes: 12 additions & 1 deletion src/util/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as CompilerCLI from '@angular/compiler-cli';
import AngularCompilerOptions from '@angular/tsc-wrapped/src/options';
import { CompilerHost, Program } from 'typescript';

import { FileCache } from './file-cache';
import { VirtualDirStats, VirtualFileStats } from './virtual-file-utils';

Expand Down Expand Up @@ -147,4 +151,11 @@ export interface HydratedDeepLinkConfigEntry extends DeepLinkConfigEntry {
export interface AppNgModuleInfo {
absolutePath: string;
className: string;
};
};

export interface CodegenOptions {
angularCompilerOptions: AngularCompilerOptions;
cliOptions: CompilerCLI.NgcCliOptions;
program: Program;
compilerHost: CompilerHost;
};

0 comments on commit 13e930a

Please sign in to comment.