Skip to content

Commit ee04217

Browse files
filipesilvajasonaden
authored andcommitted
refactor(compiler-cli): expose ngtools api separately (angular#18978)
PR Close angular#18978
1 parent a1293b2 commit ee04217

File tree

5 files changed

+139
-9
lines changed

5 files changed

+139
-9
lines changed

packages/compiler-cli/ngtools2.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
export {CompilerHost, CustomTransformers, Diagnostic, EmitFlags, Program, createCompilerHost, createProgram, formatDiagnostics} from './src/ngtools_api2';
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* This is a private API for @ngtools/webpack. This API should be stable for NG 5.
11+
*
12+
* It contains copies of the interfaces needed and wrapper functions to ensure that
13+
* they are not broken accidentally.
14+
*
15+
* Once the ngc api is public and stable, this can be removed.
16+
*/
17+
18+
import {ParseSourceSpan} from '@angular/compiler';
19+
import * as ts from 'typescript';
20+
21+
import {formatDiagnostics as formatDiagnosticsOrig} from './perform_compile';
22+
import {createCompilerHost as createCompilerOrig} from './transformers/compiler_host';
23+
import {createProgram as createProgramOrig} from './transformers/program';
24+
25+
// Interfaces from ./transformers/api;
26+
export interface Diagnostic {
27+
messageText: string;
28+
span?: ParseSourceSpan;
29+
category: ts.DiagnosticCategory;
30+
code: number;
31+
source: 'angular';
32+
}
33+
34+
export interface CompilerOptions extends ts.CompilerOptions {
35+
basePath?: string;
36+
skipMetadataEmit?: boolean;
37+
strictMetadataEmit?: boolean;
38+
skipTemplateCodegen?: boolean;
39+
flatModuleOutFile?: string;
40+
flatModuleId?: string;
41+
generateCodeForLibraries?: boolean;
42+
annotateForClosureCompiler?: boolean;
43+
annotationsAs?: 'decorators'|'static fields';
44+
trace?: boolean;
45+
enableLegacyTemplate?: boolean;
46+
disableExpressionLowering?: boolean;
47+
i18nOutLocale?: string;
48+
i18nOutFormat?: string;
49+
i18nOutFile?: string;
50+
i18nInFormat?: string;
51+
i18nInLocale?: string;
52+
i18nInFile?: string;
53+
i18nInMissingTranslations?: 'error'|'warning'|'ignore';
54+
preserveWhitespaces?: boolean;
55+
}
56+
57+
export interface CompilerHost extends ts.CompilerHost {
58+
moduleNameToFileName(moduleName: string, containingFile?: string): string|null;
59+
fileNameToModuleName(importedFilePath: string, containingFilePath: string): string|null;
60+
resourceNameToFileName(resourceName: string, containingFilePath: string): string|null;
61+
toSummaryFileName(fileName: string, referringSrcFileName: string): string;
62+
fromSummaryFileName(fileName: string, referringLibFileName: string): string;
63+
readResource?(fileName: string): Promise<string>|string;
64+
}
65+
66+
export enum EmitFlags {
67+
DTS = 1 << 0,
68+
JS = 1 << 1,
69+
70+
Default = DTS | JS
71+
}
72+
73+
export interface CustomTransformers {
74+
beforeTs?: ts.TransformerFactory<ts.SourceFile>[];
75+
afterTs?: ts.TransformerFactory<ts.SourceFile>[];
76+
}
77+
78+
export interface TsEmitArguments {
79+
program: ts.Program;
80+
host: CompilerHost;
81+
options: CompilerOptions;
82+
targetSourceFile?: ts.SourceFile;
83+
writeFile?: ts.WriteFileCallback;
84+
cancellationToken?: ts.CancellationToken;
85+
emitOnlyDtsFiles?: boolean;
86+
customTransformers?: ts.CustomTransformers;
87+
}
88+
89+
export interface TsEmitCallback { (args: TsEmitArguments): ts.EmitResult; }
90+
91+
export interface Program {
92+
getTsProgram(): ts.Program;
93+
getTsOptionDiagnostics(cancellationToken?: ts.CancellationToken): ts.Diagnostic[];
94+
getNgOptionDiagnostics(cancellationToken?: ts.CancellationToken): Diagnostic[];
95+
getTsSyntacticDiagnostics(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken):
96+
ts.Diagnostic[];
97+
getNgStructuralDiagnostics(cancellationToken?: ts.CancellationToken): Diagnostic[];
98+
getTsSemanticDiagnostics(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken):
99+
ts.Diagnostic[];
100+
getNgSemanticDiagnostics(fileName?: string, cancellationToken?: ts.CancellationToken):
101+
Diagnostic[];
102+
loadNgStructureAsync(): Promise<void>;
103+
emit({emitFlags, cancellationToken, customTransformers, emitCallback}: {
104+
emitFlags?: EmitFlags,
105+
cancellationToken?: ts.CancellationToken,
106+
customTransformers?: CustomTransformers,
107+
emitCallback?: TsEmitCallback
108+
}): ts.EmitResult;
109+
}
110+
111+
// Wrapper for createProgram.
112+
export function createProgram(
113+
{rootNames, options, host, oldProgram}:
114+
{rootNames: string[], options: CompilerOptions, host: CompilerHost, oldProgram?: Program}):
115+
Program {
116+
return createProgramOrig({rootNames, options, host, oldProgram});
117+
}
118+
119+
// Wrapper for createCompilerHost.
120+
export function createCompilerHost(
121+
{options, tsHost = ts.createCompilerHost(options, true)}:
122+
{options: CompilerOptions, tsHost?: ts.CompilerHost}): CompilerHost {
123+
return createCompilerOrig({options, tsHost});
124+
}
125+
126+
// Wrapper for formatDiagnostics.
127+
export type Diagnostics = Array<ts.Diagnostic|Diagnostic>;
128+
export function formatDiagnostics(options: CompilerOptions, diags: Diagnostics): string {
129+
return formatDiagnosticsOrig(options, diags);
130+
}

packages/compiler-cli/src/transformers/api.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,6 @@ export interface Program {
250250
*/
251251
loadNgStructureAsync(): Promise<void>;
252252

253-
/**
254-
* Retrieve the lazy route references in the program.
255-
*
256-
* Angular structural information is required to produce these routes.
257-
*/
258-
getLazyRoutes(cancellationToken?: ts.CancellationToken): {[route: string]: string};
259-
260253
/**
261254
* Emit the files requested by emitFlags implied by the program.
262255
*

packages/compiler-cli/src/transformers/program.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ class AngularCompilerProgram implements Program {
134134
});
135135
}
136136

137-
getLazyRoutes(cancellationToken?: ts.CancellationToken): {[route: string]: string} { return {}; }
138-
139137
emit({emitFlags = EmitFlags.Default, cancellationToken, customTransformers,
140138
emitCallback = defaultEmitCallback}: {
141139
emitFlags?: EmitFlags,

packages/compiler-cli/tsconfig-build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
],
2727
"files": [
2828
"index.ts",
29+
"ngtools2.ts",
2930
"src/main.ts",
3031
"src/extract_i18n.ts",
3132
"src/language_services.ts",

0 commit comments

Comments
 (0)