2
2
/// <reference path="diagnosticInformationMap.generated.ts" />
3
3
4
4
namespace ts {
5
-
6
5
/* @internal */
7
6
export function trace ( host : ModuleResolutionHost , message : DiagnosticMessage , ...args : any [ ] ) : void ;
8
7
export function trace ( host : ModuleResolutionHost ) : void {
@@ -15,6 +14,7 @@ namespace ts {
15
14
}
16
15
17
16
/** Array that is only intended to be pushed to, never read. */
17
+ /* @internal */
18
18
export interface Push < T > {
19
19
push ( value : T ) : void ;
20
20
}
@@ -675,12 +675,25 @@ namespace ts {
675
675
}
676
676
677
677
export function nodeModuleNameResolver ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache ?: ModuleResolutionCache ) : ResolvedModuleWithFailedLookupLocations {
678
- return nodeModuleNameResolverWorker ( moduleName , containingFile , compilerOptions , host , cache , /*jsOnly*/ false ) ;
678
+ return nodeModuleNameResolverWorker ( moduleName , getDirectoryPath ( containingFile ) , compilerOptions , host , cache , /*jsOnly*/ false ) ;
679
679
}
680
680
681
+ /**
682
+ * Expose resolution logic to allow us to use Node module resolution logic from arbitrary locations.
683
+ * No way to do this with `require()`: https://github.com/nodejs/node/issues/5963
684
+ * Throws an error if the module can't be resolved.
685
+ */
681
686
/* @internal */
682
- export function nodeModuleNameResolverWorker ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache ?: ModuleResolutionCache , jsOnly = false ) : ResolvedModuleWithFailedLookupLocations {
683
- const containingDirectory = getDirectoryPath ( containingFile ) ;
687
+ export function resolveJavaScriptModule ( moduleName : string , initialDir : string , host : ModuleResolutionHost ) : string {
688
+ const { resolvedModule, failedLookupLocations } =
689
+ nodeModuleNameResolverWorker ( moduleName , initialDir , { moduleResolution : ts . ModuleResolutionKind . NodeJs , allowJs : true } , host , /*cache*/ undefined , /*jsOnly*/ true ) ;
690
+ if ( ! resolvedModule ) {
691
+ throw new Error ( `Could not resolve JS module ${ moduleName } starting at ${ initialDir } . Looked in: ${ failedLookupLocations . join ( ", " ) } ` ) ;
692
+ }
693
+ return resolvedModule . resolvedFileName ;
694
+ }
695
+
696
+ function nodeModuleNameResolverWorker ( moduleName : string , containingDirectory : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache : ModuleResolutionCache | undefined , jsOnly : boolean ) : ResolvedModuleWithFailedLookupLocations {
684
697
const traceEnabled = isTraceEnabled ( compilerOptions , host ) ;
685
698
686
699
const failedLookupLocations : string [ ] = [ ] ;
0 commit comments