UNPKG

8.84 kBJavaScriptView Raw
1import { ViewEncapsulation, ɵConsole } from '@angular/core';
2import { CompileMetadataResolver, NgModuleResolver, DirectiveResolver, DirectiveNormalizer, HtmlParser, CompilerConfig, PipeResolver, AotSummaryResolver, DomElementSchemaRegistry, StaticSymbolResolver, StaticSymbolCache, StaticReflector, createOfflineCompileUrlResolver, analyzeNgModules, GeneratedFile } from '@angular/compiler';
3import { MetadataCollector, readConfiguration } from '@angular/compiler-cli';
4import { createCompilerHost, createProgram } from '@angular/compiler-cli/ngtools2';
5import { PipeSymbol } from './pipe-symbol';
6import { DirectiveSymbol } from './directive-symbol';
7import { ModuleSymbol } from './module-symbol';
8import { TsCompilerAotCompilerTypeCheckHostAdapter } from '@angular/compiler-cli/src/transformers/compiler_host';
9/**
10 * Creates a proxy which provides us access to the symbols
11 * defined in given context (could be lazy loaded module or the root module).
12 *
13 * @export
14 * @class ProjectSymbols
15 */
16var ProjectSymbols = /** @class */ (function () {
17 /**
18 * Creates an instance of ProjectSymbols.
19 *
20 * @param {ts.Program} program
21 * @param {ResourceResolver} resourceResolver
22 *
23 * @memberOf ProjectSymbols
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 * Returns the metadata associated to this module.
37 *
38 * @returns {ModuleSymbol[]}
39 *
40 * @memberOf ProjectSymbols
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 * Returns all the directives available in the context.
58 *
59 * @returns {DirectiveSymbol[]}
60 *
61 * @memberOf ProjectSymbols
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 * Returns all the pipes available in this module.
73 *
74 * @returns {PipeSymbol[]}
75 *
76 * @memberOf ProjectSymbols
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 * Returns all the providers available in this module.
86 *
87 * @returns {ProviderSymbol[]}
88 *
89 * @memberOf ProjectSymbols
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 * Returns directive based on `ClassDeclaration` node and a filename.
106 *
107 * @param {ts.ClassDeclaration} declaration
108 * @param {string} fileName
109 *
110 * @memberOf DirectiveSymbol
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 /** @internal */
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}());
187export { ProjectSymbols };
188//# sourceMappingURL=project-symbols.js.map
\No newline at end of file