feat(compiler): make .ngsummary.json
files portable
This also allows to customize the filePaths in `.ngsummary.json` file via the new methods `toSummaryFileName` and `fromSummaryFileName` on the `CompilerHost`.
This commit is contained in:
@ -207,10 +207,10 @@ export class AotCompiler {
|
||||
}
|
||||
|
||||
private _createSummary(
|
||||
srcFileUrl: string, directives: StaticSymbol[], pipes: StaticSymbol[],
|
||||
srcFileName: string, directives: StaticSymbol[], pipes: StaticSymbol[],
|
||||
ngModules: StaticSymbol[], injectables: StaticSymbol[],
|
||||
ngFactoryCtx: OutputContext): GeneratedFile[] {
|
||||
const symbolSummaries = this._symbolResolver.getSymbolsOf(srcFileUrl)
|
||||
const symbolSummaries = this._symbolResolver.getSymbolsOf(srcFileName)
|
||||
.map(symbol => this._symbolResolver.resolveSymbol(symbol));
|
||||
const typeData: {
|
||||
summary: CompileTypeSummary,
|
||||
@ -235,18 +235,19 @@ export class AotCompiler {
|
||||
metadata: this._metadataResolver.getInjectableSummary(ref) !.type
|
||||
}))
|
||||
];
|
||||
const forJitOutputCtx = this._createOutputContext(summaryForJitFileName(srcFileUrl, true));
|
||||
const forJitOutputCtx = this._createOutputContext(summaryForJitFileName(srcFileName, true));
|
||||
const {json, exportAs} = serializeSummaries(
|
||||
forJitOutputCtx, this._summaryResolver, this._symbolResolver, symbolSummaries, typeData);
|
||||
srcFileName, forJitOutputCtx, this._summaryResolver, this._symbolResolver, symbolSummaries,
|
||||
typeData);
|
||||
exportAs.forEach((entry) => {
|
||||
ngFactoryCtx.statements.push(
|
||||
o.variable(entry.exportAs).set(ngFactoryCtx.importExpr(entry.symbol)).toDeclStmt(null, [
|
||||
o.StmtModifier.Exported
|
||||
]));
|
||||
});
|
||||
const summaryJson = new GeneratedFile(srcFileUrl, summaryFileName(srcFileUrl), json);
|
||||
const summaryJson = new GeneratedFile(srcFileName, summaryFileName(srcFileName), json);
|
||||
if (this._enableSummariesForJit) {
|
||||
return [summaryJson, this._codegenSourceModule(srcFileUrl, forJitOutputCtx)];
|
||||
return [summaryJson, this._codegenSourceModule(srcFileName, forJitOutputCtx)];
|
||||
};
|
||||
|
||||
return [summaryJson];
|
||||
|
@ -23,11 +23,20 @@ export interface AotSummaryResolverHost {
|
||||
*/
|
||||
isSourceFile(sourceFilePath: string): boolean;
|
||||
/**
|
||||
* Returns the output file path of a source file.
|
||||
* Converts a file name into a representation that should be stored in a summary file.
|
||||
* This has to include changing the suffix as well.
|
||||
* E.g.
|
||||
* `some_file.ts` -> `some_file.d.ts`
|
||||
*
|
||||
* @param referringSrcFileName the soure file that refers to fileName
|
||||
*/
|
||||
getOutputFileName(sourceFilePath: string): string;
|
||||
toSummaryFileName(fileName: string, referringSrcFileName: string): string;
|
||||
|
||||
/**
|
||||
* Converts a fileName that was processed by `toSummaryFileName` back into a real fileName
|
||||
* given the fileName of the library that is referrig to it.
|
||||
*/
|
||||
fromSummaryFileName(fileName: string, referringLibFileName: string): string;
|
||||
}
|
||||
|
||||
export class AotSummaryResolver implements SummaryResolver<StaticSymbol> {
|
||||
@ -46,7 +55,13 @@ export class AotSummaryResolver implements SummaryResolver<StaticSymbol> {
|
||||
return !this.host.isSourceFile(stripGeneratedFileSuffix(filePath));
|
||||
}
|
||||
|
||||
getLibraryFileName(filePath: string) { return this.host.getOutputFileName(filePath); }
|
||||
toSummaryFileName(filePath: string, referringSrcFileName: string) {
|
||||
return this.host.toSummaryFileName(filePath, referringSrcFileName);
|
||||
}
|
||||
|
||||
fromSummaryFileName(fileName: string, referringLibFileName: string) {
|
||||
return this.host.fromSummaryFileName(fileName, referringLibFileName);
|
||||
}
|
||||
|
||||
resolveSummary(staticSymbol: StaticSymbol): Summary<StaticSymbol> {
|
||||
staticSymbol.assertNoMembers();
|
||||
@ -85,7 +100,8 @@ export class AotSummaryResolver implements SummaryResolver<StaticSymbol> {
|
||||
throw e;
|
||||
}
|
||||
if (json) {
|
||||
const {summaries, importAs} = deserializeSummaries(this.staticSymbolCache, json);
|
||||
const {summaries, importAs} =
|
||||
deserializeSummaries(this.staticSymbolCache, this, filePath, json);
|
||||
summaries.forEach((summary) => this.summaryCache.set(summary.symbol, summary));
|
||||
importAs.forEach((importAs) => {
|
||||
this.importAs.set(
|
||||
|
@ -15,7 +15,7 @@ import {ResolvedStaticSymbol, StaticSymbolResolver} from './static_symbol_resolv
|
||||
import {summaryForJitFileName, summaryForJitName} from './util';
|
||||
|
||||
export function serializeSummaries(
|
||||
forJitCtx: OutputContext, summaryResolver: SummaryResolver<StaticSymbol>,
|
||||
srcFileName: string, forJitCtx: OutputContext, summaryResolver: SummaryResolver<StaticSymbol>,
|
||||
symbolResolver: StaticSymbolResolver, symbols: ResolvedStaticSymbol[], types: {
|
||||
summary: CompileTypeSummary,
|
||||
metadata: CompileNgModuleMetadata | CompileDirectiveMetadata | CompilePipeMetadata |
|
||||
@ -76,15 +76,17 @@ export function serializeSummaries(
|
||||
});
|
||||
}
|
||||
});
|
||||
const {json, exportAs} = toJsonSerializer.serialize();
|
||||
const {json, exportAs} = toJsonSerializer.serialize(srcFileName);
|
||||
forJitSerializer.serialize(exportAs);
|
||||
return {json, exportAs};
|
||||
}
|
||||
|
||||
export function deserializeSummaries(symbolCache: StaticSymbolCache, json: string):
|
||||
export function deserializeSummaries(
|
||||
symbolCache: StaticSymbolCache, summaryResolver: SummaryResolver<StaticSymbol>,
|
||||
libraryFileName: string, json: string):
|
||||
{summaries: Summary<StaticSymbol>[], importAs: {symbol: StaticSymbol, importAs: string}[]} {
|
||||
const deserializer = new FromJsonDeserializer(symbolCache);
|
||||
return deserializer.deserialize(json);
|
||||
const deserializer = new FromJsonDeserializer(symbolCache, summaryResolver);
|
||||
return deserializer.deserialize(libraryFileName, json);
|
||||
}
|
||||
|
||||
export function createForJitStub(outputCtx: OutputContext, reference: StaticSymbol) {
|
||||
@ -151,7 +153,8 @@ class ToJsonSerializer extends ValueTransformer {
|
||||
}
|
||||
}
|
||||
|
||||
serialize(): {json: string, exportAs: {symbol: StaticSymbol, exportAs: string}[]} {
|
||||
serialize(srcFileName: string):
|
||||
{json: string, exportAs: {symbol: StaticSymbol, exportAs: string}[]} {
|
||||
const exportAs: {symbol: StaticSymbol, exportAs: string}[] = [];
|
||||
const json = JSON.stringify({
|
||||
summaries: this.processedSummaries,
|
||||
@ -165,10 +168,7 @@ class ToJsonSerializer extends ValueTransformer {
|
||||
return {
|
||||
__symbol: index,
|
||||
name: symbol.name,
|
||||
// We convert the source filenames tinto output filenames,
|
||||
// as the generated summary file will be used when the current
|
||||
// compilation unit is used as a library
|
||||
filePath: this.summaryResolver.getLibraryFileName(symbol.filePath),
|
||||
filePath: this.summaryResolver.toSummaryFileName(symbol.filePath, srcFileName),
|
||||
importAs: importAs
|
||||
};
|
||||
})
|
||||
@ -317,15 +317,21 @@ class ForJitSerializer {
|
||||
class FromJsonDeserializer extends ValueTransformer {
|
||||
private symbols: StaticSymbol[];
|
||||
|
||||
constructor(private symbolCache: StaticSymbolCache) { super(); }
|
||||
constructor(
|
||||
private symbolCache: StaticSymbolCache,
|
||||
private summaryResolver: SummaryResolver<StaticSymbol>) {
|
||||
super();
|
||||
}
|
||||
|
||||
deserialize(json: string):
|
||||
deserialize(libraryFileName: string, json: string):
|
||||
{summaries: Summary<StaticSymbol>[], importAs: {symbol: StaticSymbol, importAs: string}[]} {
|
||||
const data: {summaries: any[], symbols: any[]} = JSON.parse(json);
|
||||
const importAs: {symbol: StaticSymbol, importAs: string}[] = [];
|
||||
this.symbols = [];
|
||||
data.symbols.forEach((serializedSymbol) => {
|
||||
const symbol = this.symbolCache.get(serializedSymbol.filePath, serializedSymbol.name);
|
||||
const symbol = this.symbolCache.get(
|
||||
this.summaryResolver.fromSummaryFileName(serializedSymbol.filePath, libraryFileName),
|
||||
serializedSymbol.name);
|
||||
this.symbols.push(symbol);
|
||||
if (serializedSymbol.importAs) {
|
||||
importAs.push({symbol: symbol, importAs: serializedSymbol.importAs});
|
||||
|
Reference in New Issue
Block a user