1 | import { ViewEncapsulation, ɵConsole } from '@angular/core';
|
2 | import { CompileMetadataResolver, NgModuleResolver, DirectiveResolver, DirectiveNormalizer, HtmlParser, CompilerConfig, PipeResolver, AotSummaryResolver, DomElementSchemaRegistry, StaticSymbolResolver, StaticSymbolCache, StaticReflector, createOfflineCompileUrlResolver, analyzeNgModules, GeneratedFile } from '@angular/compiler';
|
3 | import { MetadataCollector, readConfiguration } from '@angular/compiler-cli';
|
4 | import { createCompilerHost, createProgram } from '@angular/compiler-cli/ngtools2';
|
5 | import { PipeSymbol } from './pipe-symbol';
|
6 | import { DirectiveSymbol } from './directive-symbol';
|
7 | import { ModuleSymbol } from './module-symbol';
|
8 | import { TsCompilerAotCompilerTypeCheckHostAdapter } from '@angular/compiler-cli/src/transformers/compiler_host';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var ProjectSymbols = (function () {
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function ProjectSymbols(tsconfigPath, resourceResolver, errorReporter) {
|
26 | this.tsconfigPath = tsconfigPath;
|
27 | this.resourceResolver = resourceResolver;
|
28 | this.errorReporter = errorReporter;
|
29 | var config = readConfiguration(this.tsconfigPath);
|
30 | this.options = config.options;
|
31 | this.compilerHost = createCompilerHost({ options: config.options });
|
32 | this.program = createProgram({ rootNames: config.rootNames, options: config.options, host: this.compilerHost });
|
33 | this.init();
|
34 | }
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | ProjectSymbols.prototype.getModules = function () {
|
43 | var _this = this;
|
44 | this.clearCaches();
|
45 | this.init();
|
46 | var resultMap = new Map();
|
47 | this.getAnalyzedModules().ngModules.forEach(function (m, s) {
|
48 | resultMap.set(m.type.reference, m);
|
49 | });
|
50 | var result = [];
|
51 | resultMap.forEach(function (v) {
|
52 | return result.push(new ModuleSymbol(_this.program.getTsProgram(), v.type.reference, _this.metadataResolver, _this.directiveNormalizer, _this.directiveResolver, _this.pipeResolver, _this.reflector, _this.resourceResolver, _this));
|
53 | });
|
54 | return result;
|
55 | };
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | ProjectSymbols.prototype.getDirectives = function () {
|
64 | var _this = this;
|
65 | return this.extractProgramSymbols()
|
66 | .filter(function (symbol) { return _this.metadataResolver.isDirective(symbol); })
|
67 | .map(function (symbol) {
|
68 | return new DirectiveSymbol(_this.program.getTsProgram(), symbol, _this.metadataResolver, _this.directiveNormalizer, _this.directiveResolver, _this.reflector, _this.resourceResolver, _this);
|
69 | });
|
70 | };
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | ProjectSymbols.prototype.getPipes = function () {
|
79 | var _this = this;
|
80 | return this.extractProgramSymbols()
|
81 | .filter(function (v) { return _this.metadataResolver.isPipe(v); })
|
82 | .map(function (p) { return new PipeSymbol(_this.program.getTsProgram(), p, _this.pipeResolver, _this.metadataResolver, _this); });
|
83 | };
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | ProjectSymbols.prototype.getProviders = function () {
|
92 | var resultSet = new Map();
|
93 | this.getModules().forEach(function (m) {
|
94 | m.getProviders().forEach(function (p) { return resultSet.set(p.getMetadata(), p); });
|
95 | });
|
96 | this.getDirectives().forEach(function (d) {
|
97 | d.getProviders().forEach(function (p) { return resultSet.set(p.getMetadata(), p); });
|
98 | d.getViewProviders().forEach(function (p) { return resultSet.set(p.getMetadata(), p); });
|
99 | });
|
100 | var finalResult = [];
|
101 | resultSet.forEach(function (v) { return finalResult.push(v); });
|
102 | return finalResult;
|
103 | };
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | ProjectSymbols.prototype.getDirectiveFromNode = function (declaration, fileName) {
|
113 | var sourceFile = this.program.getTsProgram().getSourceFile(fileName);
|
114 | var identifier = declaration.name;
|
115 | if (identifier) {
|
116 | return new DirectiveSymbol(this.program.getTsProgram(), this.reflector.getStaticSymbol(sourceFile.fileName, identifier.text), this.metadataResolver, this.directiveNormalizer, this.directiveResolver, this.reflector, this.resourceResolver, this);
|
117 | }
|
118 | else {
|
119 | return null;
|
120 | }
|
121 | };
|
122 |
|
123 | ProjectSymbols.prototype.getAnalyzedModules = function () {
|
124 | var analyzedModules = this.analyzedModules;
|
125 | if (!analyzedModules) {
|
126 | var analyzeHost = {
|
127 | isSourceFile: function (filePath) {
|
128 | return true;
|
129 | }
|
130 | };
|
131 | analyzedModules = this.analyzedModules = analyzeNgModules(this.program.getTsProgram().getRootFileNames(), analyzeHost, this.staticSymbolResolver, this.metadataResolver);
|
132 | }
|
133 | return analyzedModules;
|
134 | };
|
135 | ProjectSymbols.prototype.extractProgramSymbols = function () {
|
136 | var _this = this;
|
137 | return [].concat.apply([], this.program
|
138 | .getTsProgram()
|
139 | .getSourceFiles()
|
140 | .map(function (f) { return _this.staticSymbolResolver.getSymbolsOf(f.fileName); }));
|
141 | };
|
142 | ProjectSymbols.prototype.clearCaches = function () {
|
143 | this.metadataResolver.clearCache();
|
144 | this.directiveNormalizer.clearCache();
|
145 | };
|
146 | ProjectSymbols.prototype.init = function () {
|
147 | var staticSymbolCache = new StaticSymbolCache();
|
148 | var summaryResolver = new AotSummaryResolver({
|
149 | loadSummary: function (filePath) {
|
150 | return '';
|
151 | },
|
152 | isSourceFile: function (sourceFilePath) {
|
153 | return true;
|
154 | },
|
155 | toSummaryFileName: function (host) {
|
156 | return '';
|
157 | },
|
158 | fromSummaryFileName: function (host) {
|
159 | return '';
|
160 | }
|
161 | }, staticSymbolCache);
|
162 | var parser = new HtmlParser();
|
163 | var config = new CompilerConfig({
|
164 | defaultEncapsulation: ViewEncapsulation.Emulated,
|
165 | useJit: false
|
166 | });
|
167 | var defaultDir = this.program.getTsProgram().getCurrentDirectory();
|
168 | this.options.baseUrl = this.options.baseUrl || defaultDir;
|
169 | this.options.basePath = this.options.basePath || defaultDir;
|
170 | this.options.genDir = this.options.genDir || defaultDir;
|
171 | this.staticResolverHost = new TsCompilerAotCompilerTypeCheckHostAdapter(this.program.getTsProgram().getRootFileNames(), this.options, this.compilerHost, new MetadataCollector(), {
|
172 | generateFile: function (genFileName, baseFileName) { return new GeneratedFile('', '', ''); },
|
173 | findGeneratedFileNames: function (fileName) { return []; }
|
174 | });
|
175 | this.staticSymbolResolver = new StaticSymbolResolver(this.staticResolverHost, staticSymbolCache, summaryResolver, this.errorReporter);
|
176 | this.summaryResolver = new AotSummaryResolver(this.staticResolverHost, staticSymbolCache);
|
177 | this.reflector = new StaticReflector(this.summaryResolver, this.staticSymbolResolver, [], [], this.errorReporter);
|
178 | var ngModuleResolver = new NgModuleResolver(this.reflector);
|
179 | this.directiveResolver = new DirectiveResolver(this.reflector);
|
180 | this.pipeResolver = new PipeResolver(this.reflector);
|
181 | this.urlResolver = createOfflineCompileUrlResolver();
|
182 | this.directiveNormalizer = new DirectiveNormalizer(this.resourceResolver, this.urlResolver, parser, config);
|
183 | this.metadataResolver = new CompileMetadataResolver(new CompilerConfig(), parser, ngModuleResolver, this.directiveResolver, this.pipeResolver, summaryResolver, new DomElementSchemaRegistry(), this.directiveNormalizer, new ɵConsole(), staticSymbolCache, this.reflector);
|
184 | };
|
185 | return ProjectSymbols;
|
186 | }());
|
187 | export { ProjectSymbols };
|
188 |
|
\ | No newline at end of file |