From 9280412823ec4f41c66b9abed342090b29ddca24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20=C3=81vila=20de=20Esp=C3=ADndola?= Date: Thu, 7 Jul 2022 13:11:01 +0000 Subject: [PATCH] Use host.resolveModuleNames when available TSC allows users to configure module resolution by implementing host.resolveModuleNames. This is used both by Deno and ChiselStrike. Unfortunately, I couldn't find an exported TSC function that uses host.resolveModuleNames or the builtin resolution, so I implemented the logic in the type-compiler. --- packages/type-compiler/src/resolver.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/type-compiler/src/resolver.ts b/packages/type-compiler/src/resolver.ts index 3559ce714..9e032fe97 100644 --- a/packages/type-compiler/src/resolver.ts +++ b/packages/type-compiler/src/resolver.ts @@ -7,6 +7,7 @@ import { Expression, ImportDeclaration, resolveModuleName, + ResolvedModule, ScriptTarget, SourceFile, StringLiteral, @@ -32,6 +33,14 @@ export class Resolver { return this.resolveSourceFile(from.fileName, (moduleSpecifier as StringLiteral).text); } + resolveImpl(modulePath: string, fromPath: string): ResolvedModule | undefined { + if (this.host.resolveModuleNames !== undefined) { + return this.host.resolveModuleNames([modulePath], fromPath, /*reusedNames*/ undefined, /*redirectedReference*/ undefined, this.compilerOptions)[0]; + } + const result = resolveModuleName(modulePath, fromPath, this.compilerOptions, this.host); + return result.resolvedModule; + } + /** * Tries to resolve the d.ts file path for a given module path. * Scans relative paths. Looks into package.json "types" and "exports" (with new 4.7 support) @@ -40,13 +49,13 @@ export class Resolver { * @param modulePath the x in 'from x'. */ resolveSourceFile(fromPath: string, modulePath: string): SourceFile | undefined { - const result = resolveModuleName(modulePath, fromPath, this.compilerOptions, this.host); - if (!result.resolvedModule) return; + const result = this.resolveImpl(modulePath, fromPath); + if (!result) return; - const fileName = result.resolvedModule.resolvedFileName; + const fileName = result.resolvedFileName; if (this.sourceFiles[fileName]) return this.sourceFiles[fileName]; - const source = this.host.readFile(result.resolvedModule.resolvedFileName); + const source = this.host.readFile(result.resolvedFileName); if (!source) return; const sourceFile = this.sourceFiles[fileName] = createSourceFile(fileName, source, this.compilerOptions.target || ScriptTarget.ES2018, true);