Skip to content

Commit

Permalink
feat(compiler): make .ngsummary.json files portable
Browse files Browse the repository at this point in the history
This also allows to customize the filePaths in `.ngsummary.json` file
via the new methods `toSummaryFileName` and `fromSummaryFileName`
on the `CompilerHost`.
  • Loading branch information
tbosch authored and hansl committed Aug 16, 2017
1 parent 6a1ab61 commit 2572bf5
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 53 deletions.
14 changes: 10 additions & 4 deletions packages/compiler-cli/src/compiler_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export abstract class BaseAotCompilerHost<C extends BaseAotCompilerHostContext>

abstract fileNameToModuleName(importedFile: string, containingFile: string): string|null;

abstract toSummaryFileName(fileName: string, referringSrcFileName: string): string;

abstract fromSummaryFileName(fileName: string, referringLibFileName: string): string;

protected getSourceFile(filePath: string): ts.SourceFile {
const sf = this.program.getSourceFile(filePath);
if (!sf) {
Expand Down Expand Up @@ -144,10 +148,6 @@ export abstract class BaseAotCompilerHost<C extends BaseAotCompilerHostContext>
return null;
}

getOutputFileName(sourceFilePath: string): string {
return sourceFilePath.replace(EXT, '') + '.d.ts';
}

isSourceFile(filePath: string): boolean {
const excludeRegex =
this.options.generateCodeForLibraries === false ? GENERATED_OR_DTS_FILES : GENERATED_FILES;
Expand Down Expand Up @@ -268,6 +268,12 @@ export class CompilerHost extends BaseAotCompilerHost<CompilerHostContext> {
};
}

toSummaryFileName(fileName: string, referringSrcFileName: string): string {
return fileName.replace(EXT, '') + '.d.ts';
}

fromSummaryFileName(fileName: string, referringLibFileName: string): string { return fileName; }

calculateEmitPath(filePath: string): string {
// Write codegen in a directory structure matching the sources.
let root = this.options.basePath !;
Expand Down
14 changes: 14 additions & 0 deletions packages/compiler-cli/src/transformers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ export interface CompilerHost extends ts.CompilerHost {
* See ImportResolver.
*/
fileNameToModuleName(importedFilePath: string, containingFilePath: string): string|null;
/**
* 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
*/
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;
/**
* Load a referenced resource either statically or asynchronously. If the host returns a
* `Promise<string>` it is assumed the user of the corresponding `Program` will call
Expand Down
31 changes: 28 additions & 3 deletions packages/compiler-cli/src/transformers/compiler_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function createCompilerHost(

host.moduleNameToFileName = mixin.moduleNameToFileName.bind(mixin);
host.fileNameToModuleName = mixin.fileNameToModuleName.bind(mixin);
host.toSummaryFileName = mixin.toSummaryFileName.bind(mixin);
host.fromSummaryFileName = mixin.fromSummaryFileName.bind(mixin);

// Make sure we do not `host.realpath()` from TS as we do not want to resolve symlinks.
// https://github.com/Microsoft/TypeScript/issues/9552
Expand Down Expand Up @@ -109,9 +111,15 @@ class CompilerHostMixin {

let moduleName: string;
if (importedFilePackagName === containingFilePackageName) {
moduleName = dotRelative(
path.dirname(stripRootDir(this.rootDirs, containingFile)),
stripRootDir(this.rootDirs, importedFile));
const rootedContainingFile = stripRootDir(this.rootDirs, containingFile);
const rootedImportedFile = stripRootDir(this.rootDirs, importedFile);

if (rootedContainingFile !== containingFile && rootedImportedFile !== importedFile) {
// if both files are contained in the `rootDirs`, then strip the rootDirs
containingFile = rootedContainingFile;
importedFile = rootedImportedFile;
}
moduleName = dotRelative(path.dirname(containingFile), importedFile);
} else if (importedFilePackagName) {
moduleName = stripNodeModulesPrefix(importedFile);
} else {
Expand All @@ -120,6 +128,18 @@ class CompilerHostMixin {
}
return moduleName;
}

toSummaryFileName(fileName: string, referringSrcFileName: string): string {
return this.fileNameToModuleName(fileName, referringSrcFileName);
}

fromSummaryFileName(fileName: string, referringLibFileName: string): string {
const resolved = this.moduleNameToFileName(fileName, referringLibFileName);
if (!resolved) {
throw new Error(`Could not resolve ${fileName} from ${referringLibFileName}`);
}
return resolved;
}
}

interface ModuleFilenameResolutionHost extends ts.ModuleResolutionHost {
Expand Down Expand Up @@ -189,6 +209,11 @@ function stripNodeModulesPrefix(filePath: string): string {
return filePath.replace(/.*node_modules\//, '');
}

function getNodeModulesPrefix(filePath: string): string|null {
const match = /.*node_modules\//.exec(filePath);
return match ? match[1] : null;
}

function normalizePath(p: string): string {
return path.normalize(path.join(p, '.')).replace(/\\/g, '/');
}
8 changes: 8 additions & 0 deletions packages/compiler-cli/src/transformers/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ class AotCompilerHostImpl extends BaseAotCompilerHost<CompilerHost> {
fileNameToModuleName(importedFile: string, containingFile: string): string|null {
return this.context.fileNameToModuleName(importedFile, containingFile);
}

toSummaryFileName(fileName: string, referringSrcFileName: string): string {
return this.context.toSummaryFileName(fileName, referringSrcFileName);
}

fromSummaryFileName(fileName: string, referringLibFileName: string): string {
return this.context.fromSummaryFileName(fileName, referringLibFileName);
}
}

export function createProgram(
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-cli/test/diagnostics/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ const summaryResolver = new AotSummaryResolver(
{
loadSummary(filePath: string) { return null; },
isSourceFile(sourceFilePath: string) { return true; },
getOutputFileName(sourceFilePath: string) { return sourceFilePath; }
toSummaryFileName(sourceFilePath: string) { return sourceFilePath; },
fromSummaryFileName(filePath: string): string{return filePath;},
},
staticSymbolCache);

Expand Down
5 changes: 5 additions & 0 deletions packages/compiler-cli/test/transformers/compiler_host_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ describe('NgCompilerHost', () => {
]
}
});
// both files are in the rootDirs
expect(ngHostWithMultipleRoots.fileNameToModuleName('/tmp/src/b/b.ts', '/tmp/src/a/a.ts'))
.toBe('./b');

// one file is not in the rootDirs
expect(ngHostWithMultipleRoots.fileNameToModuleName('/tmp/src/c/c.ts', '/tmp/src/a/a.ts'))
.toBe('../c/c');
});

it('should error if accessing a source file from a package', () => {
Expand Down
13 changes: 7 additions & 6 deletions packages/compiler/src/aot/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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];
Expand Down
24 changes: 20 additions & 4 deletions packages/compiler/src/aot/summary_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
32 changes: 19 additions & 13 deletions packages/compiler/src/aot/summary_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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
};
})
Expand Down Expand Up @@ -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});
Expand Down
10 changes: 6 additions & 4 deletions packages/compiler/src/summary_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export interface Summary<T> {

export abstract class SummaryResolver<T> {
abstract isLibraryFile(fileName: string): boolean;
abstract getLibraryFileName(fileName: string): string|null;
abstract toSummaryFileName(fileName: string, referringSrcFileName: string): string;
abstract fromSummaryFileName(fileName: string, referringLibFileName: string): string;
abstract resolveSummary(reference: T): Summary<T>|null;
abstract getSymbolsOf(filePath: string): T[];
abstract getImportAs(reference: T): T;
Expand All @@ -28,12 +29,13 @@ export abstract class SummaryResolver<T> {
export class JitSummaryResolver implements SummaryResolver<Type<any>> {
private _summaries = new Map<Type<any>, Summary<Type<any>>>();

isLibraryFile(fileName: string): boolean { return false; };
getLibraryFileName(fileName: string): string|null { return null; }
isLibraryFile(): boolean { return false; };
toSummaryFileName(fileName: string): string { return fileName; }
fromSummaryFileName(fileName: string): string { return fileName; }
resolveSummary(reference: Type<any>): Summary<Type<any>>|null {
return this._summaries.get(reference) || null;
};
getSymbolsOf(filePath: string): Type<any>[] { return []; }
getSymbolsOf(): Type<any>[] { return []; }
getImportAs(reference: Type<any>): Type<any> { return reference; }
addSummary(summary: Summary<Type<any>>) { this._summaries.set(summary.symbol, summary); };
}
3 changes: 2 additions & 1 deletion packages/compiler/test/aot/static_symbol_resolver_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ export class MockSummaryResolver implements SummaryResolver<StaticSymbol> {
}

isLibraryFile(filePath: string): boolean { return filePath.endsWith('.d.ts'); }
getLibraryFileName(filePath: string): string { return filePath.replace(/(\.d)?\.ts$/, '.d.ts'); }
toSummaryFileName(filePath: string): string { return filePath.replace(/(\.d)?\.ts$/, '.d.ts'); }
fromSummaryFileName(filePath: string): string { return filePath; }
}

export class MockStaticSymbolResolverHost implements StaticSymbolResolverHost {
Expand Down
7 changes: 5 additions & 2 deletions packages/compiler/test/aot/summary_resolver_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function main() {
const symbolResolver = new StaticSymbolResolver(
new MockStaticSymbolResolverHost({}), symbolCache, mockSummaryResolver);
return serializeSummaries(
createMockOutputContext(), mockSummaryResolver, symbolResolver, symbols, [])
'someFile.ts', createMockOutputContext(), mockSummaryResolver, symbolResolver,
symbols, [])
.json;
}

Expand Down Expand Up @@ -105,10 +106,12 @@ export class MockAotSummaryResolverHost implements AotSummaryResolverHost {
return './' + path.basename(fileName).replace(EXT, '');
}

getOutputFileName(sourceFileName: string): string {
toSummaryFileName(sourceFileName: string): string {
return sourceFileName.replace(EXT, '') + '.d.ts';
}

fromSummaryFileName(filePath: string): string { return filePath; }

isSourceFile(filePath: string) { return !filePath.endsWith('.d.ts'); }

loadSummary(filePath: string): string { return this.summaries[filePath]; }
Expand Down
Loading

0 comments on commit 2572bf5

Please sign in to comment.