/
transformer.ts
176 lines (155 loc) Β· 7.68 KB
/
transformer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {FileSystem} from '../../../src/ngtsc/file_system';
import {DecorationAnalyzer} from '../analysis/decoration_analyzer';
import {ModuleWithProvidersAnalyses, ModuleWithProvidersAnalyzer} from '../analysis/module_with_providers_analyzer';
import {NgccReferencesRegistry} from '../analysis/ngcc_references_registry';
import {ExportInfo, PrivateDeclarationsAnalyzer} from '../analysis/private_declarations_analyzer';
import {SwitchMarkerAnalyses, SwitchMarkerAnalyzer} from '../analysis/switch_marker_analyzer';
import {CompiledFile} from '../analysis/types';
import {CommonJsReflectionHost} from '../host/commonjs_host';
import {Esm2015ReflectionHost} from '../host/esm2015_host';
import {Esm5ReflectionHost} from '../host/esm5_host';
import {NgccReflectionHost} from '../host/ngcc_host';
import {UmdReflectionHost} from '../host/umd_host';
import {Logger} from '../logging/logger';
import {CommonJsRenderingFormatter} from '../rendering/commonjs_rendering_formatter';
import {DtsRenderer} from '../rendering/dts_renderer';
import {Esm5RenderingFormatter} from '../rendering/esm5_rendering_formatter';
import {EsmRenderingFormatter} from '../rendering/esm_rendering_formatter';
import {Renderer} from '../rendering/renderer';
import {RenderingFormatter} from '../rendering/rendering_formatter';
import {UmdRenderingFormatter} from '../rendering/umd_rendering_formatter';
import {FileToWrite} from '../rendering/utils';
import {EntryPointBundle} from './entry_point_bundle';
export type TransformResult = {
success: true; diagnostics: ts.Diagnostic[]; transformedFiles: FileToWrite[];
} |
{
success: false;
diagnostics: ts.Diagnostic[];
};
/**
* A Package is stored in a directory on disk and that directory can contain one or more package
* formats - e.g. fesm2015, UMD, etc. Additionally, each package provides typings (`.d.ts` files).
*
* Each of these formats exposes one or more entry points, which are source files that need to be
* parsed to identify the decorated exported classes that need to be analyzed and compiled by one or
* more `DecoratorHandler` objects.
*
* Each entry point to a package is identified by a `package.json` which contains properties that
* indicate what formatted bundles are accessible via this end-point.
*
* Each bundle is identified by a root `SourceFile` that can be parsed and analyzed to
* identify classes that need to be transformed; and then finally rendered and written to disk.
*
* Along with the source files, the corresponding source maps (either inline or external) and
* `.d.ts` files are transformed accordingly.
*
* - Flat file packages have all the classes in a single file.
* - Other packages may re-export classes from other non-entry point files.
* - Some formats may contain multiple "modules" in a single file.
*/
export class Transformer {
constructor(private fs: FileSystem, private logger: Logger) {}
/**
* Transform the source (and typings) files of a bundle.
* @param bundle the bundle to transform.
* @returns information about the files that were transformed.
*/
transform(bundle: EntryPointBundle): TransformResult {
const reflectionHost = this.getHost(bundle);
// Parse and analyze the files.
const {decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses,
moduleWithProvidersAnalyses, diagnostics} = this.analyzeProgram(reflectionHost, bundle);
// Bail if the analysis produced any errors.
if (hasErrors(diagnostics)) {
return {success: false, diagnostics};
}
// Transform the source files and source maps.
const srcFormatter = this.getRenderingFormatter(reflectionHost, bundle);
const renderer = new Renderer(reflectionHost, srcFormatter, this.fs, this.logger, bundle);
let renderedFiles = renderer.renderProgram(
decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses);
if (bundle.dts) {
const dtsFormatter = new EsmRenderingFormatter(reflectionHost, bundle.isCore);
const dtsRenderer =
new DtsRenderer(dtsFormatter, this.fs, this.logger, reflectionHost, bundle);
const renderedDtsFiles = dtsRenderer.renderProgram(
decorationAnalyses, privateDeclarationsAnalyses, moduleWithProvidersAnalyses);
renderedFiles = renderedFiles.concat(renderedDtsFiles);
}
return {success: true, diagnostics, transformedFiles: renderedFiles};
}
getHost(bundle: EntryPointBundle): NgccReflectionHost {
const typeChecker = bundle.src.program.getTypeChecker();
switch (bundle.format) {
case 'esm2015':
return new Esm2015ReflectionHost(this.logger, bundle.isCore, typeChecker, bundle.dts);
case 'esm5':
return new Esm5ReflectionHost(this.logger, bundle.isCore, typeChecker, bundle.dts);
case 'umd':
return new UmdReflectionHost(
this.logger, bundle.isCore, bundle.src.program, bundle.src.host, bundle.dts);
case 'commonjs':
return new CommonJsReflectionHost(
this.logger, bundle.isCore, bundle.src.program, bundle.src.host, bundle.dts);
default:
throw new Error(`Reflection host for "${bundle.format}" not yet implemented.`);
}
}
getRenderingFormatter(host: NgccReflectionHost, bundle: EntryPointBundle): RenderingFormatter {
switch (bundle.format) {
case 'esm2015':
return new EsmRenderingFormatter(host, bundle.isCore);
case 'esm5':
return new Esm5RenderingFormatter(host, bundle.isCore);
case 'umd':
if (!(host instanceof UmdReflectionHost)) {
throw new Error('UmdRenderer requires a UmdReflectionHost');
}
return new UmdRenderingFormatter(host, bundle.isCore);
case 'commonjs':
return new CommonJsRenderingFormatter(host, bundle.isCore);
default:
throw new Error(`Renderer for "${bundle.format}" not yet implemented.`);
}
}
analyzeProgram(reflectionHost: NgccReflectionHost, bundle: EntryPointBundle): ProgramAnalyses {
const referencesRegistry = new NgccReferencesRegistry(reflectionHost);
const switchMarkerAnalyzer =
new SwitchMarkerAnalyzer(reflectionHost, bundle.entryPoint.package);
const switchMarkerAnalyses = switchMarkerAnalyzer.analyzeProgram(bundle.src.program);
const diagnostics: ts.Diagnostic[] = [];
const decorationAnalyzer = new DecorationAnalyzer(
this.fs, bundle, reflectionHost, referencesRegistry,
diagnostic => diagnostics.push(diagnostic));
const decorationAnalyses = decorationAnalyzer.analyzeProgram();
const moduleWithProvidersAnalyzer =
bundle.dts && new ModuleWithProvidersAnalyzer(reflectionHost, referencesRegistry);
const moduleWithProvidersAnalyses = moduleWithProvidersAnalyzer &&
moduleWithProvidersAnalyzer.analyzeProgram(bundle.src.program);
const privateDeclarationsAnalyzer =
new PrivateDeclarationsAnalyzer(reflectionHost, referencesRegistry);
const privateDeclarationsAnalyses =
privateDeclarationsAnalyzer.analyzeProgram(bundle.src.program);
return {decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses,
moduleWithProvidersAnalyses, diagnostics};
}
}
export function hasErrors(diagnostics: ts.Diagnostic[]) {
return diagnostics.some(d => d.category === ts.DiagnosticCategory.Error);
}
interface ProgramAnalyses {
decorationAnalyses: Map<ts.SourceFile, CompiledFile>;
switchMarkerAnalyses: SwitchMarkerAnalyses;
privateDeclarationsAnalyses: ExportInfo[];
moduleWithProvidersAnalyses: ModuleWithProvidersAnalyses|null;
diagnostics: ts.Diagnostic[];
}