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

Commit 13e930a

Browse files
authored
fix(angular): support angular 2.3+ ngc api
1 parent b76c21b commit 13e930a

File tree

7 files changed

+88
-54
lines changed

7 files changed

+88
-54
lines changed

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@
6060
"xml2js": "^0.4.17"
6161
},
6262
"devDependencies": {
63-
"@angular/common": "2.2.1",
64-
"@angular/compiler": "2.2.1",
65-
"@angular/compiler-cli": "2.2.1",
66-
"@angular/core": "2.2.1",
67-
"@angular/forms": "2.2.1",
68-
"@angular/http": "2.2.1",
69-
"@angular/platform-browser": "2.2.1",
70-
"@angular/platform-browser-dynamic": "2.2.1",
71-
"@angular/platform-server": "2.2.1",
63+
"@angular/common": "4.0.0-beta.5",
64+
"@angular/compiler": "4.0.0-beta.5",
65+
"@angular/compiler-cli": "4.0.0-beta.5",
66+
"@angular/core": "4.0.0-beta.5",
67+
"@angular/forms": "4.0.0-beta.5",
68+
"@angular/http": "4.0.0-beta.5",
69+
"@angular/platform-browser": "4.0.0-beta.5",
70+
"@angular/platform-browser-dynamic": "4.0.0-beta.5",
71+
"@angular/platform-server": "4.0.0-beta.5",
7272
"@types/chalk": "^0.4.30",
7373
"@types/chokidar": "1.4.29",
7474
"@types/clean-css": "^3.4.29",

src/aot/aot-compiler.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import { basename, dirname, extname, join, normalize, relative, resolve } from '
33

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

1010
import { HybridFileSystem } from '../util/hybrid-file-system';
1111
import { getInstance as getHybridFileSystem } from '../util/hybrid-file-system-factory';
1212
import { getInstance } from './compiler-host-factory';
1313
import { NgcCompilerHost } from './compiler-host';
14-
import { patchReflectorHost } from './reflector-host';
1514
import { getFallbackMainContent, replaceBootstrap } from './utils';
1615
import { Logger } from '../logger/logger';
1716
import { printDiagnostics, clearDiagnostics, DiagnosticsType } from '../logger/logger-diagnostics';
@@ -21,13 +20,13 @@ import { BuildError } from '../util/errors';
2120
import { changeExtension } from '../util/helpers';
2221
import { BuildContext } from '../util/interfaces';
2322

23+
import { doCodegen } from './codegen';
24+
2425
export class AotCompiler {
2526

2627
private tsConfig: ParsedTsConfig;
2728
private angularCompilerOptions: AngularCompilerOptions;
2829
private program: Program;
29-
private reflector: StaticReflector;
30-
private reflectorHost: ReflectorHost;
3130
private compilerHost: NgcCompilerHost;
3231
private fileSystem: HybridFileSystem;
3332
private lazyLoadedModuleDictionary: any;
@@ -43,8 +42,6 @@ export class AotCompiler {
4342
this.fileSystem = getHybridFileSystem();
4443
this.compilerHost = getInstance(this.tsConfig.parsed.options);
4544
this.program = createProgram(this.tsConfig.parsed.fileNames, this.tsConfig.parsed.options, this.compilerHost);
46-
this.reflectorHost = new ReflectorHost(this.program, this.compilerHost, this.angularCompilerOptions);
47-
this.reflector = new StaticReflector(this.reflectorHost);
4845
}
4946

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

61-
// Create the Code Generator.
62-
const codeGenerator = CodeGenerator.create(
63-
this.angularCompilerOptions,
64-
i18nOptions,
65-
this.program,
66-
this.compilerHost,
67-
new NodeReflectorHostContext(this.compilerHost)
68-
);
69-
70-
// We need to temporarily patch the CodeGenerator until either it's patched or allows us
71-
// to pass in our own ReflectorHost.
72-
patchReflectorHost(codeGenerator);
7358
Logger.debug('[AotCompiler] compile: starting codegen ... ');
74-
return codeGenerator.codegen({transitiveModules: true});
59+
return doCodegen({
60+
angularCompilerOptions: this.angularCompilerOptions,
61+
cliOptions: i18nOptions,
62+
program: this.program,
63+
compilerHost: this.compilerHost
64+
});
7565
}).then(() => {
7666
Logger.debug('[AotCompiler] compile: starting codegen ... DONE');
7767
Logger.debug('[AotCompiler] compile: Creating and validating new TypeScript Program ...');

src/aot/codegen.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { CodegenOptions } from '../util/interfaces';
2+
import { runCodegen, isAngular22 } from './codegen/codegen-ng22';
3+
import { runCodegen as runCodegen23 } from './codegen/codegen-ng23plus';
4+
5+
6+
export function doCodegen(options: CodegenOptions) {
7+
if (isAngular22()) {
8+
return runCodegen(options);
9+
}
10+
return runCodegen23(options);
11+
}
12+
13+

src/aot/codegen/codegen-ng22.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as CompilerCLI from '@angular/compiler-cli';
2+
import { CodegenOptions } from '../../util/interfaces';
3+
4+
export function isAngular22() {
5+
if ((CompilerCLI as any).NodeReflectorHostContext) {
6+
return true;
7+
}
8+
return false;
9+
}
10+
11+
export function runCodegen(options: CodegenOptions) {
12+
const NodeReflectorHostContextConstructor = (CompilerCLI as any).NodeReflectorHostContext;
13+
const instance = new NodeReflectorHostContextConstructor(options.compilerHost);
14+
const codeGenerator = CompilerCLI.CodeGenerator.create(options.angularCompilerOptions,
15+
options.cliOptions,
16+
options.program,
17+
options.compilerHost,
18+
instance);
19+
20+
// angular 2.2 api to codegen
21+
return (codeGenerator.codegen as any)({transitiveModules: true});
22+
}
23+

src/aot/codegen/codegen-ng23plus.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as CompilerCLI from '@angular/compiler-cli';
2+
import { CodegenOptions } from '../../util/interfaces';
3+
4+
export function isAngular23Plus() {
5+
if ((CompilerCLI as any).NodeCompilerHostContext) {
6+
return true;
7+
}
8+
return false;
9+
}
10+
11+
export function runCodegen(options: CodegenOptions) {
12+
const NodeCompilerHostContext = (CompilerCLI as any).NodeCompilerHostContext;
13+
const instance = new NodeCompilerHostContext();
14+
const codeGenerator = CompilerCLI.CodeGenerator.create(options.angularCompilerOptions,
15+
options.cliOptions,
16+
options.program,
17+
options.compilerHost,
18+
instance);
19+
20+
// angular 2.3+ api for codegen does not take any options
21+
return (codeGenerator.codegen as any)();
22+
}

src/aot/reflector-host.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/util/interfaces.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import * as CompilerCLI from '@angular/compiler-cli';
2+
import AngularCompilerOptions from '@angular/tsc-wrapped/src/options';
3+
import { CompilerHost, Program } from 'typescript';
4+
15
import { FileCache } from './file-cache';
26
import { VirtualDirStats, VirtualFileStats } from './virtual-file-utils';
37

@@ -147,4 +151,11 @@ export interface HydratedDeepLinkConfigEntry extends DeepLinkConfigEntry {
147151
export interface AppNgModuleInfo {
148152
absolutePath: string;
149153
className: string;
150-
};
154+
};
155+
156+
export interface CodegenOptions {
157+
angularCompilerOptions: AngularCompilerOptions;
158+
cliOptions: CompilerCLI.NgcCliOptions;
159+
program: Program;
160+
compilerHost: CompilerHost;
161+
};

0 commit comments

Comments
 (0)