@@ -542,7 +542,7 @@ function innerResolveTypeReference(
542
542
ns = ns . _ns
543
543
}
544
544
if ( ns ) {
545
- const childScope = moduleDeclToScope ( ns , ns . _ownerScope || scope )
545
+ const childScope = moduleDeclToScope ( ctx , ns , ns . _ownerScope || scope )
546
546
return innerResolveTypeReference (
547
547
ctx ,
548
548
childScope ,
@@ -581,7 +581,7 @@ function resolveGlobalScope(ctx: TypeResolveContext): TypeScope[] | undefined {
581
581
throw new Error ( '[vue/compiler-sfc] globalTypeFiles requires fs access.' )
582
582
}
583
583
return ctx . options . globalTypeFiles . map ( file =>
584
- fileToScope ( normalizePath ( file ) , fs , ctx . options . babelParserPlugins , true )
584
+ fileToScope ( ctx , normalizePath ( file ) , true )
585
585
)
586
586
}
587
587
}
@@ -603,23 +603,36 @@ function resolveTypeFromImport(
603
603
name : string ,
604
604
scope : TypeScope
605
605
) : ScopeTypeNode | undefined {
606
+ const { source, imported } = scope . imports [ name ]
607
+ const resolved = resolveImportSource ( ctx , node , scope , source )
608
+ return resolveTypeReference (
609
+ ctx ,
610
+ node ,
611
+ fileToScope ( ctx , resolved ) ,
612
+ imported ,
613
+ true
614
+ )
615
+ }
616
+
617
+ function resolveImportSource (
618
+ ctx : TypeResolveContext ,
619
+ node : Node ,
620
+ scope : TypeScope ,
621
+ source : string
622
+ ) : string {
606
623
const fs : FS = ctx . options . fs || ts ?. sys
607
624
if ( ! fs ) {
608
625
ctx . error (
609
626
`No fs option provided to \`compileScript\` in non-Node environment. ` +
610
627
`File system access is required for resolving imported types.` ,
611
- node
628
+ node ,
629
+ scope
612
630
)
613
631
}
614
-
615
- const containingFile = scope . filename
616
- const { source, imported } = scope . imports [ name ]
617
-
618
- let resolved : string | undefined
619
-
632
+ let resolved
620
633
if ( source . startsWith ( '.' ) ) {
621
634
// relative import - fast path
622
- const filename = path . join ( containingFile , '..' , source )
635
+ const filename = path . join ( scope . filename , '..' , source )
623
636
resolved = resolveExt ( filename , fs )
624
637
} else {
625
638
// module or aliased import - use full TS resolution, only supported in Node
@@ -632,36 +645,22 @@ function resolveTypeFromImport(
632
645
}
633
646
if ( ! ts ) {
634
647
ctx . error (
635
- `Failed to resolve type ${ imported } from module ${ JSON . stringify (
636
- source
637
- ) } . ` +
648
+ `Failed to resolve import source ${ JSON . stringify ( source ) } . ` +
638
649
`typescript is required as a peer dep for vue in order ` +
639
650
`to support resolving types from module imports.` ,
640
651
node ,
641
652
scope
642
653
)
643
654
}
644
- resolved = resolveWithTS ( containingFile , source , fs )
655
+ resolved = resolveWithTS ( scope . filename , source , fs )
645
656
}
646
-
647
657
if ( resolved ) {
648
- resolved = normalizePath ( resolved )
649
-
650
658
// (hmr) register dependency file on ctx
651
659
; ( ctx . deps || ( ctx . deps = new Set ( ) ) ) . add ( resolved )
652
-
653
- return resolveTypeReference (
654
- ctx ,
655
- node ,
656
- fileToScope ( resolved , fs , ctx . options . babelParserPlugins ) ,
657
- imported ,
658
- true
659
- )
660
+ return normalizePath ( resolved )
660
661
} else {
661
- ctx . error (
662
- `Failed to resolve import source ${ JSON . stringify (
663
- source
664
- ) } for type ${ name } `,
662
+ return ctx . error (
663
+ `Failed to resolve import source ${ JSON . stringify ( source ) } .` ,
665
664
node ,
666
665
scope
667
666
)
@@ -753,18 +752,18 @@ export function invalidateTypeCache(filename: string) {
753
752
}
754
753
755
754
export function fileToScope (
755
+ ctx : TypeResolveContext ,
756
756
filename : string ,
757
- fs : FS ,
758
- parserPlugins : SFCScriptCompileOptions [ 'babelParserPlugins' ] ,
759
757
asGlobal = false
760
758
) : TypeScope {
761
759
const cached = fileToScopeCache . get ( filename )
762
760
if ( cached ) {
763
761
return cached
764
762
}
765
-
763
+ // fs should be guaranteed to exist here
764
+ const fs = ctx . options . fs || ts ?. sys
766
765
const source = fs . readFile ( filename ) || ''
767
- const body = parseFile ( filename , source , parserPlugins )
766
+ const body = parseFile ( filename , source , ctx . options . babelParserPlugins )
768
767
const scope : TypeScope = {
769
768
filename,
770
769
source,
@@ -773,7 +772,7 @@ export function fileToScope(
773
772
types : Object . create ( null ) ,
774
773
exportedTypes : Object . create ( null )
775
774
}
776
- recordTypes ( body , scope , asGlobal )
775
+ recordTypes ( ctx , body , scope , asGlobal )
777
776
fileToScopeCache . set ( filename , scope )
778
777
return scope
779
778
}
@@ -846,12 +845,13 @@ function ctxToScope(ctx: TypeResolveContext): TypeScope {
846
845
exportedTypes : Object . create ( null )
847
846
}
848
847
849
- recordTypes ( body , scope )
848
+ recordTypes ( ctx , body , scope )
850
849
851
850
return ( ctx . scope = scope )
852
851
}
853
852
854
853
function moduleDeclToScope (
854
+ ctx : TypeResolveContext ,
855
855
node : TSModuleDeclaration & { _resolvedChildScope ?: TypeScope } ,
856
856
parentScope : TypeScope
857
857
) : TypeScope {
@@ -872,15 +872,20 @@ function moduleDeclToScope(
872
872
const id = getId ( decl . id )
873
873
scope . types [ id ] = scope . exportedTypes [ id ] = decl
874
874
} else {
875
- recordTypes ( node . body . body , scope )
875
+ recordTypes ( ctx , node . body . body , scope )
876
876
}
877
877
878
878
return ( node . _resolvedChildScope = scope )
879
879
}
880
880
881
881
const importExportRE = / ^ I m p o r t | ^ E x p o r t /
882
882
883
- function recordTypes ( body : Statement [ ] , scope : TypeScope , asGlobal = false ) {
883
+ function recordTypes (
884
+ ctx : TypeResolveContext ,
885
+ body : Statement [ ] ,
886
+ scope : TypeScope ,
887
+ asGlobal = false
888
+ ) {
884
889
const { types, exportedTypes, imports } = scope
885
890
const isAmbient = asGlobal
886
891
? ! body . some ( s => importExportRE . test ( s . type ) )
@@ -932,6 +937,15 @@ function recordTypes(body: Statement[], scope: TypeScope, asGlobal = false) {
932
937
}
933
938
}
934
939
}
940
+ } else if ( stmt . type === 'ExportAllDeclaration' ) {
941
+ const targetFile = resolveImportSource (
942
+ ctx ,
943
+ stmt . source ,
944
+ scope ,
945
+ stmt . source . value
946
+ )
947
+ const targetScope = fileToScope ( ctx , targetFile )
948
+ Object . assign ( scope . exportedTypes , targetScope . exportedTypes )
935
949
}
936
950
}
937
951
}
0 commit comments