Skip to content

Commit c1a62e2

Browse files
committed
feat(tools): allow disabling annotation lowering
1 parent aac37be commit c1a62e2

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎tools/@angular/tsc-wrapped/src/options.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ interface Options extends ts.CompilerOptions {
2929
// Default is true.
3030
generateCodeForLibraries?: boolean;
3131

32+
// Modify how angular annotations are emitted to improve tree-shaking.
33+
annotationsAs?: string; /* 'decorator'|'static field' */
34+
3235
// Print extra information while running the compiler
3336
trace: boolean;
3437

‎tools/@angular/tsc-wrapped/src/tsc.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ export function check(diags: ts.Diagnostic[]) {
5252
}
5353
}
5454

55+
export function validateAngularCompilerOptions(options: AngularCompilerOptions): ts.Diagnostic[] {
56+
if (options.annotationsAs) {
57+
switch (options.annotationsAs) {
58+
case 'decorators':
59+
case 'static fields':
60+
break;
61+
default:
62+
return [{
63+
file: null,
64+
start: null,
65+
length: null,
66+
messageText:
67+
'Angular compiler options "annotationsAs" only supports "static fields" and "decorators"',
68+
category: ts.DiagnosticCategory.Error,
69+
code: 0
70+
}];
71+
}
72+
}
73+
}
74+
5575
export class Tsc implements CompilerInterface {
5676
public ngOptions: AngularCompilerOptions;
5777
public parsed: ts.ParsedCommandLine;
@@ -96,6 +116,8 @@ export class Tsc implements CompilerInterface {
96116
for (const key of Object.keys(this.parsed.options)) {
97117
this.ngOptions[key] = this.parsed.options[key];
98118
}
119+
check(validateAngularCompilerOptions(this.ngOptions));
120+
99121
return {parsed: this.parsed, ngOptions: this.ngOptions};
100122
}
101123

@@ -113,8 +135,10 @@ export class Tsc implements CompilerInterface {
113135
}
114136

115137
emit(compilerHost: TsickleHost, oldProgram: ts.Program): number {
116-
// Create a new program since the host may be different from the old program.
117-
const program = ts.createProgram(this.parsed.fileNames, this.parsed.options, compilerHost);
138+
// Create a program if we are lowering annotations with tsickle.
139+
const program = this.ngOptions.annotationsAs === 'static fields' ?
140+
ts.createProgram(this.parsed.fileNames, this.parsed.options, compilerHost) :
141+
oldProgram;
118142
debug('Emitting outputs...');
119143
const emitResult = program.emit();
120144
const diagnostics: ts.Diagnostic[] = [];

0 commit comments

Comments
 (0)