2016-11-29 15:36:33 -08:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
2016-12-15 09:12:40 -08:00
|
|
|
import {CompileSummaryKind, CompileTypeSummary} from '../compile_metadata';
|
|
|
|
|
import {Summary, SummaryResolver} from '../summary_resolver';
|
2016-11-29 15:36:33 -08:00
|
|
|
|
2016-12-15 09:12:40 -08:00
|
|
|
import {StaticSymbol, StaticSymbolCache} from './static_symbol';
|
|
|
|
|
import {ResolvedStaticSymbol} from './static_symbol_resolver';
|
|
|
|
|
import {deserializeSummaries, summaryFileName} from './summary_serializer';
|
2016-11-29 15:36:33 -08:00
|
|
|
|
|
|
|
|
const STRIP_SRC_FILE_SUFFIXES = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
|
|
|
|
|
|
|
|
|
|
export interface AotSummaryResolverHost {
|
|
|
|
|
/**
|
|
|
|
|
* Loads an NgModule/Directive/Pipe summary file
|
|
|
|
|
*/
|
2016-12-15 09:12:40 -08:00
|
|
|
loadSummary(filePath: string): string /*|null*/;
|
2016-11-29 15:36:33 -08:00
|
|
|
|
|
|
|
|
/**
|
2016-12-15 09:12:40 -08:00
|
|
|
* Returns whether a file is a source file or not.
|
2016-11-29 15:36:33 -08:00
|
|
|
*/
|
2016-12-15 09:12:40 -08:00
|
|
|
isSourceFile(sourceFilePath: string): boolean;
|
2016-11-29 15:36:33 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-15 09:12:40 -08:00
|
|
|
export class AotSummaryResolver implements SummaryResolver<StaticSymbol> {
|
|
|
|
|
private summaryCache = new Map<StaticSymbol, Summary<StaticSymbol>>();
|
|
|
|
|
private loadedFilePaths = new Set<string>();
|
2016-11-29 15:36:33 -08:00
|
|
|
|
2016-12-15 09:12:40 -08:00
|
|
|
constructor(private host: AotSummaryResolverHost, private staticSymbolCache: StaticSymbolCache) {}
|
2016-11-29 15:36:33 -08:00
|
|
|
|
2016-12-15 09:12:40 -08:00
|
|
|
private _assertNoMembers(symbol: StaticSymbol) {
|
|
|
|
|
if (symbol.members.length) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Internal state: StaticSymbols in summaries can't have members! ${JSON.stringify(symbol)}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-29 15:36:33 -08:00
|
|
|
|
2016-12-15 09:12:40 -08:00
|
|
|
resolveSummary(staticSymbol: StaticSymbol): Summary<StaticSymbol> {
|
|
|
|
|
this._assertNoMembers(staticSymbol);
|
|
|
|
|
let summary = this.summaryCache.get(staticSymbol);
|
|
|
|
|
if (!summary) {
|
|
|
|
|
this._loadSummaryFile(staticSymbol.filePath);
|
|
|
|
|
summary = this.summaryCache.get(staticSymbol);
|
|
|
|
|
}
|
|
|
|
|
return summary;
|
2016-11-29 15:36:33 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-15 09:12:40 -08:00
|
|
|
getSymbolsOf(filePath: string): StaticSymbol[] {
|
|
|
|
|
this._loadSummaryFile(filePath);
|
|
|
|
|
return Array.from(this.summaryCache.keys()).filter((symbol) => symbol.filePath === filePath);
|
|
|
|
|
}
|
2016-12-02 10:08:46 -08:00
|
|
|
|
2016-12-15 09:12:40 -08:00
|
|
|
private _loadSummaryFile(filePath: string) {
|
|
|
|
|
if (this.loadedFilePaths.has(filePath)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.loadedFilePaths.add(filePath);
|
|
|
|
|
if (!this.host.isSourceFile(filePath)) {
|
2016-11-29 15:36:33 -08:00
|
|
|
const summaryFilePath = summaryFileName(filePath);
|
2016-12-15 09:12:40 -08:00
|
|
|
let json: string;
|
|
|
|
|
try {
|
|
|
|
|
json = this.host.loadSummary(summaryFilePath);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`Error loading summary file ${summaryFilePath}`);
|
|
|
|
|
throw e;
|
2016-11-29 15:36:33 -08:00
|
|
|
}
|
2016-12-15 09:12:40 -08:00
|
|
|
if (json) {
|
|
|
|
|
const readSummaries = deserializeSummaries(this.staticSymbolCache, json);
|
|
|
|
|
readSummaries.forEach((summary) => { this.summaryCache.set(summary.symbol, summary); });
|
2016-11-29 15:36:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|