Skip to content

Commit 9bfa405

Browse files
clydinfilipesilva
authored andcommitted
refactor(@ngtools/webpack): cleanup ngtools type imports/exports
1 parent 04a3607 commit 9bfa405

File tree

2 files changed

+27
-132
lines changed

2 files changed

+27
-132
lines changed

packages/@ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import {
4141
CompilerHost,
4242
Diagnostic,
4343
EmitFlags,
44-
LazyRoute,
4544
createProgram,
4645
createCompilerHost,
4746
formatDiagnostics,
@@ -430,7 +429,7 @@ export class AngularCompilerPlugin {
430429
const lazyRoutes = ngProgram.listLazyRoutes();
431430

432431
return lazyRoutes.reduce(
433-
(acc: LazyRouteMap, curr: LazyRoute) => {
432+
(acc, curr) => {
434433
const ref = curr.route;
435434
if (ref in acc && acc[ref] !== curr.referencedModule.filePath) {
436435
throw new Error(

packages/@ngtools/webpack/src/ngtools_api.ts

Lines changed: 26 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -7,121 +7,18 @@
77
*/
88
import * as path from 'path';
99
import * as ts from 'typescript';
10+
import * as ngc from '@angular/compiler-cli';
11+
import * as ngtools from '@angular/compiler-cli/ngtools2';
1012

1113
export const DEFAULT_ERROR_CODE = 100;
1214
export const UNKNOWN_ERROR_CODE = 500;
1315
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-
}
8816

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>;
12522

12623
// Manually check for Compiler CLI availability and supported version.
12724
// This is needed because @ngtools/webpack does not depend directly on @angular/compiler-cli, since
@@ -132,7 +29,7 @@ export function CompilerCliIsSupported() {
13229

13330
// Check that Angular is available.
13431
try {
135-
version = require('@angular/compiler-cli').VERSION;
32+
version = (require('@angular/compiler-cli') as typeof ngc).VERSION;
13633
} catch (e) {
13734
throw new Error('The "@angular/compiler-cli" package was not properly installed. Error: ' + e);
13835
}
@@ -144,45 +41,44 @@ export function CompilerCliIsSupported() {
14441
+ 'Please clean your node_modules and reinstall.');
14542
}
14643

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. '
15547
+ `Current version is "${version.full}".`);
15648
}
15749
}
15850

15951
// These imports do not exist on a global install for Angular CLI, so we cannot use a static ES6
16052
// import.
161-
let compilerCli: any = {};
53+
let compilerCli: typeof ngc;
16254
try {
16355
compilerCli = require('@angular/compiler-cli');
164-
} catch (e) {
56+
} catch {
16557
// Don't throw an error if the private API does not exist.
16658
// Instead, the `CompilerCliIsSupported` method should return throw and indicate the
16759
// plugin cannot be used.
16860
}
16961

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;
17267

17368
// These imports do not exist on Angular versions lower than 5, so we cannot use a static ES6
17469
// import.
175-
let ngtools2: any = {};
70+
let ngtools2: typeof ngtools;
17671
try {
17772
ngtools2 = require('@angular/compiler-cli/ngtools2');
178-
} catch (e) {
73+
} catch {
17974
// Don't throw an error if the private API does not exist.
18075
// Instead, the `AngularCompilerPlugin.isSupported` method should return false and indicate the
18176
// plugin cannot be used.
18277
}
18378

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

Comments
 (0)