7
7
*/
8
8
import * as path from 'path' ;
9
9
import * as ts from 'typescript' ;
10
+ import * as ngc from '@angular/compiler-cli' ;
11
+ import * as ngtools from '@angular/compiler-cli/ngtools2' ;
10
12
11
13
export const DEFAULT_ERROR_CODE = 100 ;
12
14
export const UNKNOWN_ERROR_CODE = 500 ;
13
15
export const SOURCE = 'angular' as 'angular' ;
14
- export interface Diagnostic {
15
- messageText : string ;
16
- span ?: any ;
17
- category : ts . DiagnosticCategory ;
18
- code : number ;
19
- source : 'angular' ;
20
- }
21
- export interface CompilerOptions extends ts . CompilerOptions {
22
- basePath ?: string ;
23
- skipMetadataEmit ?: boolean ;
24
- strictMetadataEmit ?: boolean ;
25
- skipTemplateCodegen ?: boolean ;
26
- flatModuleOutFile ?: string ;
27
- flatModuleId ?: string ;
28
- generateCodeForLibraries ?: boolean ;
29
- annotateForClosureCompiler ?: boolean ;
30
- annotationsAs ?: 'decorators' | 'static fields' ;
31
- trace ?: boolean ;
32
- enableLegacyTemplate ?: boolean ;
33
- disableExpressionLowering ?: boolean ;
34
- i18nOutLocale ?: string ;
35
- i18nOutFormat ?: string ;
36
- i18nOutFile ?: string ;
37
- i18nInFormat ?: string ;
38
- i18nInLocale ?: string ;
39
- i18nInFile ?: string ;
40
- i18nInMissingTranslations ?: 'error' | 'warning' | 'ignore' ;
41
- preserveWhitespaces ?: boolean ;
42
- }
43
- export interface CompilerHost extends ts . CompilerHost {
44
- moduleNameToFileName ( moduleName : string , containingFile ?: string ) : string | null ;
45
- fileNameToModuleName ( importedFilePath : string , containingFilePath : string ) : string | null ;
46
- resourceNameToFileName ( resourceName : string , containingFilePath : string ) : string | null ;
47
- toSummaryFileName ( fileName : string , referringSrcFileName : string ) : string ;
48
- fromSummaryFileName ( fileName : string , referringLibFileName : string ) : string ;
49
- readResource ?( fileName : string ) : Promise < string > | string ;
50
- }
51
- export interface CustomTransformers {
52
- beforeTs ?: ts . TransformerFactory < ts . SourceFile > [ ] ;
53
- afterTs ?: ts . TransformerFactory < ts . SourceFile > [ ] ;
54
- }
55
- export interface TsEmitArguments {
56
- program : ts . Program ;
57
- host : CompilerHost ;
58
- options : CompilerOptions ;
59
- targetSourceFile ?: ts . SourceFile ;
60
- writeFile ?: ts . WriteFileCallback ;
61
- cancellationToken ?: ts . CancellationToken ;
62
- emitOnlyDtsFiles ?: boolean ;
63
- customTransformers ?: ts . CustomTransformers ;
64
- }
65
- export interface TsEmitCallback {
66
- ( args : TsEmitArguments ) : ts . EmitResult ;
67
- }
68
- export interface Program {
69
- getTsProgram ( ) : ts . Program ;
70
- getTsOptionDiagnostics ( cancellationToken ?: ts . CancellationToken ) : ts . Diagnostic [ ] ;
71
- getNgOptionDiagnostics ( cancellationToken ?: ts . CancellationToken ) : Diagnostic [ ] ;
72
- getTsSyntacticDiagnostics ( sourceFile ?: ts . SourceFile , cancellationToken ?: ts . CancellationToken ) :
73
- ts . Diagnostic [ ] ;
74
- getNgStructuralDiagnostics ( cancellationToken ?: ts . CancellationToken ) : Diagnostic [ ] ;
75
- getTsSemanticDiagnostics ( sourceFile ?: ts . SourceFile , cancellationToken ?: ts . CancellationToken ) :
76
- ts . Diagnostic [ ] ;
77
- getNgSemanticDiagnostics ( fileName ?: string , cancellationToken ?: ts . CancellationToken ) :
78
- Diagnostic [ ] ;
79
- loadNgStructureAsync ( ) : Promise < void > ;
80
- listLazyRoutes ?( ) : LazyRoute [ ] ;
81
- emit ( { emitFlags, cancellationToken, customTransformers, emitCallback } : {
82
- emitFlags ?: any ;
83
- cancellationToken ?: ts . CancellationToken ;
84
- customTransformers ?: CustomTransformers ;
85
- emitCallback ?: TsEmitCallback ;
86
- } ) : ts . EmitResult ;
87
- }
88
16
89
- export interface LazyRoute {
90
- route : string ;
91
- module : { name : string , filePath : string } ;
92
- referencedModule : { name : string , filePath : string } ;
93
- }
94
-
95
- export declare type Diagnostics = ReadonlyArray < ts . Diagnostic | Diagnostic > ;
96
-
97
- // Interfaces for the function declarations.
98
- export interface CreateProgramInterface {
99
- ( { rootNames, options, host, oldProgram } : {
100
- rootNames : string [ ] ;
101
- options : CompilerOptions ;
102
- host : CompilerHost ;
103
- oldProgram ?: Program ;
104
- } ) : Program ;
105
- }
106
- export interface CreateCompilerHostInterface {
107
- ( { options, tsHost } : {
108
- options : CompilerOptions ;
109
- tsHost ?: ts . CompilerHost ;
110
- } ) : CompilerHost ;
111
- }
112
- export interface FormatDiagnosticsInterface {
113
- ( diags : Diagnostics ) : string ;
114
- }
115
- export interface ParsedConfiguration {
116
- project : string ;
117
- options : CompilerOptions ;
118
- rootNames : string [ ] ;
119
- emitFlags : any ;
120
- errors : Diagnostics ;
121
- }
122
- export interface ReadConfigurationInterface {
123
- ( project : string , existingOptions ?: ts . CompilerOptions ) : ParsedConfiguration ;
124
- }
17
+ export type CompilerOptions = ngc . CompilerOptions ;
18
+ export type CompilerHost = ngtools . CompilerHost ;
19
+ export type Program = ngtools . Program ;
20
+ export type Diagnostic = ngtools . Diagnostic ;
21
+ export type Diagnostics = ReadonlyArray < ts . Diagnostic | Diagnostic > ;
125
22
126
23
// Manually check for Compiler CLI availability and supported version.
127
24
// This is needed because @ngtools /webpack does not depend directly on @angular /compiler-cli, since
@@ -132,7 +29,7 @@ export function CompilerCliIsSupported() {
132
29
133
30
// Check that Angular is available.
134
31
try {
135
- version = require ( '@angular/compiler-cli' ) . VERSION ;
32
+ version = ( require ( '@angular/compiler-cli' ) as typeof ngc ) . VERSION ;
136
33
} catch ( e ) {
137
34
throw new Error ( 'The "@angular/compiler-cli" package was not properly installed. Error: ' + e ) ;
138
35
}
@@ -144,45 +41,44 @@ export function CompilerCliIsSupported() {
144
41
+ 'Please clean your node_modules and reinstall.' ) ;
145
42
}
146
43
147
- // Throw if we're neither 2.3.1 or more, nor 4.x.y, nor 5.x.y, nor 6.x.y.
148
- if ( ! ( version . major == '6'
149
- || version . major == '5'
150
- || version . major == '4'
151
- || ( version . major == '2'
152
- && ( version . minor == '4'
153
- || version . minor == '3' && version . patch == '1' ) ) ) ) {
154
- throw new Error ( 'Version of @angular/compiler-cli needs to be 2.3.1 or greater. '
44
+ // Throw if we're less than 5.x
45
+ if ( Number ( version . major ) < 5 ) {
46
+ throw new Error ( 'Version of @angular/compiler-cli needs to be 5.0.0 or greater. '
155
47
+ `Current version is "${ version . full } ".` ) ;
156
48
}
157
49
}
158
50
159
51
// These imports do not exist on a global install for Angular CLI, so we cannot use a static ES6
160
52
// import.
161
- let compilerCli : any = { } ;
53
+ let compilerCli : typeof ngc ;
162
54
try {
163
55
compilerCli = require ( '@angular/compiler-cli' ) ;
164
- } catch ( e ) {
56
+ } catch {
165
57
// Don't throw an error if the private API does not exist.
166
58
// Instead, the `CompilerCliIsSupported` method should return throw and indicate the
167
59
// plugin cannot be used.
168
60
}
169
61
170
- export const VERSION = compilerCli . VERSION ;
171
- export const __NGTOOLS_PRIVATE_API_2 = compilerCli . __NGTOOLS_PRIVATE_API_2 ;
62
+ export const VERSION : typeof ngc . VERSION = compilerCli && compilerCli . VERSION ;
63
+ export const __NGTOOLS_PRIVATE_API_2 : typeof ngc . __NGTOOLS_PRIVATE_API_2 =
64
+ compilerCli && compilerCli . __NGTOOLS_PRIVATE_API_2 ;
65
+ export const readConfiguration : typeof ngc . readConfiguration =
66
+ compilerCli && compilerCli . readConfiguration ;
172
67
173
68
// These imports do not exist on Angular versions lower than 5, so we cannot use a static ES6
174
69
// import.
175
- let ngtools2 : any = { } ;
70
+ let ngtools2 : typeof ngtools ;
176
71
try {
177
72
ngtools2 = require ( '@angular/compiler-cli/ngtools2' ) ;
178
- } catch ( e ) {
73
+ } catch {
179
74
// Don't throw an error if the private API does not exist.
180
75
// Instead, the `AngularCompilerPlugin.isSupported` method should return false and indicate the
181
76
// plugin cannot be used.
182
77
}
183
78
184
- export const createProgram : CreateProgramInterface = ngtools2 . createProgram ;
185
- export const createCompilerHost : CreateCompilerHostInterface = ngtools2 . createCompilerHost ;
186
- export const formatDiagnostics : FormatDiagnosticsInterface = ngtools2 . formatDiagnostics ;
187
- export const readConfiguration : ReadConfigurationInterface = compilerCli . readConfiguration ;
188
- export const EmitFlags = ngtools2 . EmitFlags ;
79
+ export const createProgram : typeof ngtools . createProgram = ngtools2 && ngtools2 . createProgram ;
80
+ export const createCompilerHost : typeof ngtools . createCompilerHost =
81
+ ngtools2 && ngtools2 . createCompilerHost ;
82
+ export const formatDiagnostics : typeof ngtools . formatDiagnostics =
83
+ ngtools2 && ngtools2 . formatDiagnostics ;
84
+ export const EmitFlags : typeof ngtools . EmitFlags = ngtools2 && ngtools2 . EmitFlags ;
0 commit comments