fix(ivy): use CompilerHost.resolveModuleNames() if available (#30017)

Sometimes we need to override module resolution behaviour.
We do this by implementing the optional method `resolveModuleNames()`
on `CompilerHost`.

This commit ensures that we always try this method first before falling
back to the standard `ts.resolveModuleName`

PR Close #30017
This commit is contained in:
Pete Bacon Darwin
2019-05-01 14:03:39 +01:00
committed by Kara Erickson
parent 638ba4a2cf
commit 5b80ab372d
4 changed files with 27 additions and 10 deletions

View File

@ -91,3 +91,20 @@ export function nodeDebugInfo(node: ts.Node): string {
const {line, character} = ts.getLineAndCharacterOfPosition(sf, node.pos);
return `[${sf.fileName}: ${ts.SyntaxKind[node.kind]} @ ${line}:${character}]`;
}
/**
* Resolve the specified `moduleName` using the given `compilerOptions` and `compilerHost`.
*
* This helper will attempt to use the `CompilerHost.resolveModuleNames()` method if available.
* Otherwise it will fallback on the `ts.ResolveModuleName()` function.
*/
export function resolveModuleName(
moduleName: string, containingFile: string, compilerOptions: ts.CompilerOptions,
compilerHost: ts.CompilerHost): ts.ResolvedModule|undefined {
if (compilerHost.resolveModuleNames) {
return compilerHost.resolveModuleNames([moduleName], containingFile)[0];
} else {
return ts.resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost)
.resolvedModule;
}
}