@@ -62,7 +62,9 @@ export type SimpleTypeResolveContext = Pick<
62
62
// required
63
63
'source' | 'filename' | 'error' | 'options'
64
64
> &
65
- Partial < Pick < ScriptCompileContext , 'scope' | 'globalScopes' | 'deps' > > & {
65
+ Partial <
66
+ Pick < ScriptCompileContext , 'scope' | 'globalScopes' | 'deps' | 'fs' >
67
+ > & {
66
68
ast : Statement [ ]
67
69
}
68
70
@@ -693,7 +695,7 @@ function qualifiedNameToPath(node: Identifier | TSQualifiedName): string[] {
693
695
694
696
function resolveGlobalScope ( ctx : TypeResolveContext ) : TypeScope [ ] | undefined {
695
697
if ( ctx . options . globalTypeFiles ) {
696
- const fs : FS = ctx . options . fs || ts ?. sys
698
+ const fs = resolveFS ( ctx )
697
699
if ( ! fs ) {
698
700
throw new Error ( '[vue/compiler-sfc] globalTypeFiles requires fs access.' )
699
701
}
@@ -714,6 +716,30 @@ export function registerTS(_ts: any) {
714
716
715
717
type FS = NonNullable < SFCScriptCompileOptions [ 'fs' ] >
716
718
719
+ function resolveFS ( ctx : TypeResolveContext ) : FS | undefined {
720
+ if ( ctx . fs ) {
721
+ return ctx . fs
722
+ }
723
+ const fs = ctx . options . fs || ts . sys
724
+ if ( ! fs ) {
725
+ return
726
+ }
727
+ return ( ctx . fs = {
728
+ fileExists ( file ) {
729
+ if ( file . endsWith ( '.vue.ts' ) ) {
730
+ file = file . replace ( / \. t s $ / , '' )
731
+ }
732
+ return fs . fileExists ( file )
733
+ } ,
734
+ readFile ( file ) {
735
+ if ( file . endsWith ( '.vue.ts' ) ) {
736
+ file = file . replace ( / \. t s $ / , '' )
737
+ }
738
+ return fs . readFile ( file )
739
+ }
740
+ } )
741
+ }
742
+
717
743
function resolveTypeFromImport (
718
744
ctx : TypeResolveContext ,
719
745
node : ReferenceTypes ,
@@ -731,9 +757,9 @@ function importSourceToScope(
731
757
scope : TypeScope ,
732
758
source : string
733
759
) : TypeScope {
734
- const fs : FS = ctx . options . fs || ts ?. sys
760
+ const fs = resolveFS ( ctx )
735
761
if ( ! fs ) {
736
- ctx . error (
762
+ return ctx . error (
737
763
`No fs option provided to \`compileScript\` in non-Node environment. ` +
738
764
`File system access is required for resolving imported types.` ,
739
765
node ,
@@ -881,7 +907,11 @@ function resolveWithTS(
881
907
)
882
908
883
909
if ( res . resolvedModule ) {
884
- return res . resolvedModule . resolvedFileName
910
+ let filename = res . resolvedModule . resolvedFileName
911
+ if ( filename . endsWith ( '.vue.ts' ) ) {
912
+ filename = filename . replace ( / \. t s $ / , '' )
913
+ }
914
+ return filename
885
915
}
886
916
}
887
917
@@ -936,7 +966,7 @@ export function fileToScope(
936
966
return cached
937
967
}
938
968
// fs should be guaranteed to exist here
939
- const fs = ctx . options . fs || ts ?. sys
969
+ const fs = resolveFS ( ctx ) !
940
970
const source = fs . readFile ( filename ) || ''
941
971
const body = parseFile ( filename , source , ctx . options . babelParserPlugins )
942
972
const scope = new TypeScope ( filename , source , 0 , recordImports ( body ) )
0 commit comments