summary_serializer.js
8.3 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { CompileSummaryKind } from '../compile_metadata';
import { ValueTransformer, visitValue } from '../util';
import { StaticSymbol } from './static_symbol';
var /** @type {?} */ STRIP_SRC_FILE_SUFFIXES = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
/**
* @param {?} host
* @param {?} summaryResolver
* @param {?} symbolResolver
* @param {?} symbols
* @param {?} types
* @return {?}
*/
export function serializeSummaries(host, summaryResolver, symbolResolver, symbols, types) {
var /** @type {?} */ serializer = new Serializer(host);
// for symbols, we use everything except for the class metadata itself
// (we keep the statics though), as the class metadata is contained in the
// CompileTypeSummary.
symbols.forEach(function (resolvedSymbol) { return serializer.addOrMergeSummary({ symbol: resolvedSymbol.symbol, metadata: resolvedSymbol.metadata }); });
// Add summaries that are referenced by the given symbols (transitively)
// Note: the serializer.symbols array might be growing while
// we execute the loop!
for (var /** @type {?} */ processedIndex = 0; processedIndex < serializer.symbols.length; processedIndex++) {
var /** @type {?} */ symbol = serializer.symbols[processedIndex];
if (!host.isSourceFile(symbol.filePath)) {
var /** @type {?} */ summary = summaryResolver.resolveSummary(symbol);
if (!summary) {
// some symbols might originate from a plain typescript library
// that just exported .d.ts and .metadata.json files, i.e. where no summary
// files were created.
var /** @type {?} */ resolvedSymbol = symbolResolver.resolveSymbol(symbol);
if (resolvedSymbol) {
summary = { symbol: resolvedSymbol.symbol, metadata: resolvedSymbol.metadata };
}
}
if (summary) {
serializer.addOrMergeSummary(summary);
}
}
}
// Add type summaries.
// Note: We don't add the summaries of all referenced symbols as for the ResolvedSymbols,
// as the type summaries already contain the transitive data that they require
// (in a minimal way).
types.forEach(function (typeSummary) {
serializer.addOrMergeSummary({ symbol: typeSummary.type.reference, metadata: { __symbolic: 'class' }, type: typeSummary });
if (typeSummary.summaryKind === CompileSummaryKind.NgModule) {
var /** @type {?} */ ngModuleSummary = (typeSummary);
ngModuleSummary.exportedDirectives.concat(ngModuleSummary.exportedPipes).forEach(function (id) {
var /** @type {?} */ symbol = id.reference;
if (!host.isSourceFile(symbol.filePath)) {
serializer.addOrMergeSummary(summaryResolver.resolveSummary(symbol));
}
});
}
});
return serializer.serialize();
}
/**
* @param {?} symbolCache
* @param {?} json
* @return {?}
*/
export function deserializeSummaries(symbolCache, json) {
var /** @type {?} */ deserializer = new Deserializer(symbolCache);
return deserializer.deserialize(json);
}
/**
* @param {?} fileName
* @return {?}
*/
export function summaryFileName(fileName) {
var /** @type {?} */ fileNameWithoutSuffix = fileName.replace(STRIP_SRC_FILE_SUFFIXES, '');
return fileNameWithoutSuffix + ".ngsummary.json";
}
var Serializer = (function (_super) {
__extends(Serializer, _super);
/**
* @param {?} host
*/
function Serializer(host) {
_super.call(this);
this.host = host;
this.symbols = [];
this.indexBySymbol = new Map();
this.processedSummaryBySymbol = new Map();
this.processedSummaries = [];
}
/**
* @param {?} summary
* @return {?}
*/
Serializer.prototype.addOrMergeSummary = function (summary) {
var /** @type {?} */ symbolMeta = summary.metadata;
if (symbolMeta && symbolMeta.__symbolic === 'class') {
// For classes, we only keep their statics, but not the metadata
// of the class itself as that has been captured already via other summaries
// (e.g. DirectiveSummary, ...).
symbolMeta = { __symbolic: 'class', statics: symbolMeta.statics };
}
var /** @type {?} */ processedSummary = this.processedSummaryBySymbol.get(summary.symbol);
if (!processedSummary) {
processedSummary = this.processValue({ symbol: summary.symbol });
this.processedSummaries.push(processedSummary);
this.processedSummaryBySymbol.set(summary.symbol, processedSummary);
}
// Note: == by purpose to compare with undefined!
if (processedSummary.metadata == null && symbolMeta != null) {
processedSummary.metadata = this.processValue(symbolMeta);
}
// Note: == by purpose to compare with undefined!
if (processedSummary.type == null && summary.type != null) {
processedSummary.type = this.processValue(summary.type);
}
};
/**
* @return {?}
*/
Serializer.prototype.serialize = function () {
var _this = this;
return JSON.stringify({
summaries: this.processedSummaries,
symbols: this.symbols.map(function (symbol, index) {
return {
__symbol: index,
name: symbol.name,
// We convert the source filenames tinto output filenames,
// as the generated summary file will be used when teh current
// compilation unit is used as a library
filePath: _this.host.getOutputFileName(symbol.filePath)
};
})
});
};
/**
* @param {?} value
* @return {?}
*/
Serializer.prototype.processValue = function (value) { return visitValue(value, this, null); };
/**
* @param {?} value
* @param {?} context
* @return {?}
*/
Serializer.prototype.visitOther = function (value, context) {
if (value instanceof StaticSymbol) {
var /** @type {?} */ index = this.indexBySymbol.get(value);
// Note: == by purpose to compare with undefined!
if (index == null) {
index = this.indexBySymbol.size;
this.indexBySymbol.set(value, index);
this.symbols.push(value);
}
return { __symbol: index };
}
};
return Serializer;
}(ValueTransformer));
function Serializer_tsickle_Closure_declarations() {
/** @type {?} */
Serializer.prototype.symbols;
/** @type {?} */
Serializer.prototype.indexBySymbol;
/** @type {?} */
Serializer.prototype.processedSummaryBySymbol;
/** @type {?} */
Serializer.prototype.processedSummaries;
/** @type {?} */
Serializer.prototype.host;
}
var Deserializer = (function (_super) {
__extends(Deserializer, _super);
/**
* @param {?} symbolCache
*/
function Deserializer(symbolCache) {
_super.call(this);
this.symbolCache = symbolCache;
}
/**
* @param {?} json
* @return {?}
*/
Deserializer.prototype.deserialize = function (json) {
var _this = this;
var /** @type {?} */ data = JSON.parse(json);
this.symbols = data.symbols.map(function (serializedSymbol) { return _this.symbolCache.get(serializedSymbol.filePath, serializedSymbol.name); });
return visitValue(data.summaries, this, null);
};
/**
* @param {?} map
* @param {?} context
* @return {?}
*/
Deserializer.prototype.visitStringMap = function (map, context) {
if ('__symbol' in map) {
return this.symbols[map['__symbol']];
}
else {
return _super.prototype.visitStringMap.call(this, map, context);
}
};
return Deserializer;
}(ValueTransformer));
function Deserializer_tsickle_Closure_declarations() {
/** @type {?} */
Deserializer.prototype.symbols;
/** @type {?} */
Deserializer.prototype.symbolCache;
}
//# sourceMappingURL=summary_serializer.js.map