From 679073e33b820655696ed0bd1ee1bb424d2b7618 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Mon, 22 Apr 2024 06:44:59 +0900 Subject: [PATCH] fix(typescript): prioritize resolving `.js` virtual script to `.d.ts` file path --- packages/typescript/lib/resolveModuleName.ts | 46 ++++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/packages/typescript/lib/resolveModuleName.ts b/packages/typescript/lib/resolveModuleName.ts index 98e888be..0910a850 100644 --- a/packages/typescript/lib/resolveModuleName.ts +++ b/packages/typescript/lib/resolveModuleName.ts @@ -7,7 +7,10 @@ export function createResolveModuleName( languagePlugins: LanguagePlugin[], getSourceScript: (fileName: string) => SourceScript | undefined, ) { - const toPatchResults = new Map(); + const toSourceFileInfo = new Map(); const moduleResolutionHost: ts.ModuleResolutionHost = { readFile: host.readFile.bind(host), directoryExists: host.directoryExists?.bind(host), @@ -24,10 +27,28 @@ export function createResolveModuleName( } for (const { extension } of typescript.extraFileExtensions) { if (fileName.endsWith(`.d.${extension}.ts`)) { - const patchFileName = fileName.slice(0, -`.d.${extension}.ts`.length) + `.${extension}`; - if (fileExists(patchFileName)) { - toPatchResults.set(fileName, patchFileName); - return true; + const sourceFileName = fileName.slice(0, -`.d.${extension}.ts`.length) + `.${extension}`; + if (fileExists(sourceFileName)) { + const sourceScript = getSourceScript(sourceFileName); + if (sourceScript?.generated) { + const serviceScript = sourceScript.generated.languagePlugin.typescript?.getServiceScript(sourceScript.generated.root); + if (serviceScript) { + const dtsPath = sourceFileName + '.d.ts'; + if ((serviceScript.extension === '.js' || serviceScript.extension === '.jsx') && fileExists(dtsPath)) { + toSourceFileInfo.set(fileName, { + sourceFileName: dtsPath, + extension: '.ts', + }); + } + else { + toSourceFileInfo.set(fileName, { + sourceFileName, + extension: serviceScript.extension, + }); + } + return true; + } + } } } } @@ -52,17 +73,14 @@ export function createResolveModuleName( redirectedReference, resolutionMode ); - if (result.resolvedModule && toPatchResults.has(result.resolvedModule.resolvedFileName)) { - result.resolvedModule.resolvedFileName = toPatchResults.get(result.resolvedModule.resolvedFileName)!; - const sourceScript = getSourceScript(result.resolvedModule.resolvedFileName); - if (sourceScript?.generated) { - const serviceScript = sourceScript.generated.languagePlugin.typescript?.getServiceScript(sourceScript.generated.root); - if (serviceScript) { - result.resolvedModule.extension = serviceScript.extension; - } + if (result.resolvedModule) { + const sourceFileInfo = toSourceFileInfo.get(result.resolvedModule.resolvedFileName); + if (sourceFileInfo) { + result.resolvedModule.resolvedFileName = sourceFileInfo.sourceFileName; + result.resolvedModule.extension = sourceFileInfo.extension; } } - toPatchResults.clear(); + toSourceFileInfo.clear(); return result; };