feat(compiler): use typescript for resolving resource paths

This can also be customized via the new method `resourceNameToFileName` in the
`CompilerHost`.
This commit is contained in:
Tobias Bosch
2017-08-15 17:06:09 -07:00
committed by Hans
parent 2572bf508f
commit 43226cb93d
11 changed files with 142 additions and 37 deletions

View File

@ -330,8 +330,15 @@ export class MockAotCompilerHost implements AotCompilerHost {
private metadataCollector = new MetadataCollector();
private metadataVisible: boolean = true;
private dtsAreSource: boolean = true;
private resolveModuleNameHost: ts.ModuleResolutionHost;
constructor(private tsHost: MockCompilerHost) {}
constructor(private tsHost: MockCompilerHost) {
this.resolveModuleNameHost = Object.create(tsHost);
this.resolveModuleNameHost.fileExists = (fileName) => {
fileName = stripNgResourceSuffix(fileName);
return tsHost.fileExists(fileName);
};
}
hideMetadata() { this.metadataVisible = false; }
@ -369,11 +376,22 @@ export class MockAotCompilerHost implements AotCompilerHost {
moduleName = moduleName.replace(EXT, '');
const resolved = ts.resolveModuleName(
moduleName, containingFile.replace(/\\/g, '/'),
{baseDir: '/', genDir: '/'}, this.tsHost)
{baseDir: '/', genDir: '/'}, this.resolveModuleNameHost)
.resolvedModule;
return resolved ? resolved.resolvedFileName : null;
}
resourceNameToFileName(resourceName: string, containingFile: string) {
// Note: we convert package paths into relative paths to be compatible with the the
// previous implementation of UrlResolver.
if (resourceName && resourceName.charAt(0) !== '.' && !path.isAbsolute(resourceName)) {
resourceName = `./${resourceName}`;
}
const filePathWithNgResource =
this.moduleNameToFileName(addNgResourceSuffix(resourceName), containingFile);
return filePathWithNgResource ? stripNgResourceSuffix(filePathWithNgResource) : null;
}
// AotSummaryResolverHost
loadSummary(filePath: string): string|null { return this.tsHost.readFile(filePath); }
@ -647,3 +665,11 @@ export function compile(
}
return {genFiles, outDir};
}
function stripNgResourceSuffix(fileName: string): string {
return fileName.replace(/\.\$ngresource\$.*/, '');
}
function addNgResourceSuffix(fileName: string): string {
return `${fileName}.$ngresource$`;
}