@@ -708,13 +708,14 @@ function resolveGlobalScope(ctx: TypeResolveContext): TypeScope[] | undefined {
708
708
}
709
709
}
710
710
711
- let ts : typeof TS
711
+ let ts : typeof TS | undefined
712
+ let loadTS : ( ( ) => typeof TS ) | undefined
712
713
713
714
/**
714
715
* @private
715
716
*/
716
- export function registerTS ( _ts : any ) {
717
- ts = _ts
717
+ export function registerTS ( _loadTS : ( ) => typeof TS ) {
718
+ loadTS = _loadTS
718
719
}
719
720
720
721
type FS = NonNullable < SFCScriptCompileOptions [ 'fs' ] >
@@ -723,7 +724,10 @@ function resolveFS(ctx: TypeResolveContext): FS | undefined {
723
724
if ( ctx . fs ) {
724
725
return ctx . fs
725
726
}
726
- const fs = ctx . options . fs || ts . sys
727
+ if ( ! ts && loadTS ) {
728
+ ts = loadTS ( )
729
+ }
730
+ const fs = ctx . options . fs || ts ?. sys
727
731
if ( ! fs ) {
728
732
return
729
733
}
@@ -779,22 +783,25 @@ function importSourceToScope(
779
783
} else {
780
784
// module or aliased import - use full TS resolution, only supported in Node
781
785
if ( ! __NODE_JS__ ) {
782
- ctx . error (
786
+ return ctx . error (
783
787
`Type import from non-relative sources is not supported in the browser build.` ,
784
788
node ,
785
789
scope
786
790
)
787
791
}
788
792
if ( ! ts ) {
789
- ctx . error (
790
- `Failed to resolve import source ${ JSON . stringify ( source ) } . ` +
791
- `typescript is required as a peer dep for vue in order ` +
792
- `to support resolving types from module imports.` ,
793
- node ,
794
- scope
795
- )
793
+ if ( loadTS ) ts = loadTS ( )
794
+ if ( ! ts ) {
795
+ return ctx . error (
796
+ `Failed to resolve import source ${ JSON . stringify ( source ) } . ` +
797
+ `typescript is required as a peer dep for vue in order ` +
798
+ `to support resolving types from module imports.` ,
799
+ node ,
800
+ scope
801
+ )
802
+ }
796
803
}
797
- resolved = resolveWithTS ( scope . filename , source , fs )
804
+ resolved = resolveWithTS ( scope . filename , source , ts , fs )
798
805
}
799
806
if ( resolved ) {
800
807
resolved = scope . resolvedImportSources [ source ] = normalizePath ( resolved )
@@ -839,6 +846,7 @@ const tsConfigRefMap = new Map<string, string>()
839
846
function resolveWithTS (
840
847
containingFile : string ,
841
848
source : string ,
849
+ ts : typeof TS ,
842
850
fs : FS
843
851
) : string | undefined {
844
852
if ( ! __NODE_JS__ ) return
@@ -853,7 +861,7 @@ function resolveWithTS(
853
861
const normalizedConfigPath = normalizePath ( configPath )
854
862
const cached = tsConfigCache . get ( normalizedConfigPath )
855
863
if ( ! cached ) {
856
- configs = loadTSConfig ( configPath , fs ) . map ( config => ( { config } ) )
864
+ configs = loadTSConfig ( configPath , ts , fs ) . map ( config => ( { config } ) )
857
865
tsConfigCache . set ( normalizedConfigPath , configs )
858
866
} else {
859
867
configs = cached
@@ -918,7 +926,11 @@ function resolveWithTS(
918
926
}
919
927
}
920
928
921
- function loadTSConfig ( configPath : string , fs : FS ) : TS . ParsedCommandLine [ ] {
929
+ function loadTSConfig (
930
+ configPath : string ,
931
+ ts : typeof TS ,
932
+ fs : FS
933
+ ) : TS . ParsedCommandLine [ ] {
922
934
// The only case where `fs` is NOT `ts.sys` is during tests.
923
935
// parse config host requires an extra `readDirectory` method
924
936
// during tests, which is stubbed.
@@ -940,7 +952,7 @@ function loadTSConfig(configPath: string, fs: FS): TS.ParsedCommandLine[] {
940
952
if ( config . projectReferences ) {
941
953
for ( const ref of config . projectReferences ) {
942
954
tsConfigRefMap . set ( ref . path , configPath )
943
- res . unshift ( ...loadTSConfig ( ref . path , fs ) )
955
+ res . unshift ( ...loadTSConfig ( ref . path , ts , fs ) )
944
956
}
945
957
}
946
958
return res
0 commit comments