…he TypeScript 7.1 module resolver API
The 7.1 API accepts a custom module resolver (api.createModuleResolver)
on synthetic programs only. When `resolveModuleName` is set, every file
now compiles in ts-loader's existing synthetic ("orphan") program with
the resolver attached, adapting classic ts-loader's
(moduleName, containingFile, compilerOptions, compilerHost, parentResolver)
signature. `parentResolver` is backed by a second, default resolver.
Limitations: `containingFile` is a placeholder in the importing file's
directory (the API only reports the directory), `compilerHost` is
undefined, and only resolvedFileName/originalPath/packageId are passed
back to the API.
Adds a resolveModuleName comparison test that fails (TS2307) without
this change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Note
Experimental - we do not actively plan to merge this. It's raised to record what the TypeScript 7.1 API makes possible for the
resolveModuleNameloader option, and what the trade-offs are.Summary
Since moving to the TypeScript 7 API, the
resolveModuleNameloader option has been accepted but ignored. TypeScript 7.1 addsapi.createModuleResolver(compilerOptions, { resolveModuleName }), which takes a custom resolver callback. This PR uses it to make the option work again.How it works
createPrograms/reconfigurePrograms), not on configured projects opened from a tsconfig. WhenresolveModuleNameis set, every file compiles in ts-loader's existing synthetic ("orphan") program with the resolver attached. The end-of-build recheck uses that program too.(moduleName, containingFile, compilerOptions, compilerHost, parentResolver).parentResolverworks. It's backed by a second, plain resolver that does TypeScript's default resolution. It's needed because returning nothing from the API's callback means "unresolved", not "use the default".resolveTypeReferenceDirectivehas no API equivalent.Limitations
containingFileis a placeholder. The API only reports the importing file's directory, socontainingFileis a made-up name in that directory. Resolvers that usepath.dirname(containingFile)work; ones that inspect the importing file's name don't.compilerHost. It'sundefined.resolvedFileName,originalPathandpackageIdreach the API. Fields likeisExternalLibraryImportare dropped.Test plan
resolveModuleNamecomparison test: the only reported error is a deliberateTS2322, which can only appear iffoo/bazresolved through the custom resolver. Without this change it reportsTS2307: Cannot find moduleinstead (checked by temporarily removing thesrc/changes).npm run comparison-tests: all 58 passnpm run lint/npm run build3.0.1_resolveModuleNameexecution test passes (it passes without this change too, because Karma doesn't fail on type errors)🤖 Generated with Claude Code