@@ -5,86 +5,90 @@ export const hasStrictlyNsReferences = (
55 graph : ModuleGraph ,
66 filePath : string ,
77 importsForExport : ImportMaps | undefined ,
8- identifier : string ,
9- seenFiles = new Set < string > ( )
8+ identifier : string
109) : [ boolean , string ?] => {
11- if ( ! importsForExport ) return [ false ] ;
10+ const seen = new Set < string > ( ) ;
1211
13- if ( seenFiles . has ( filePath ) ) return [ false ] ;
14- seenFiles . add ( filePath ) ;
12+ const walkDown = ( path : string , importMaps : ImportMaps | undefined , id : string ) : [ boolean , string ? ] => {
13+ if ( ! importMaps ) return [ false ] ;
1514
16- let namespace : string | undefined ;
15+ if ( seen . has ( path ) ) return [ false ] ;
16+ seen . add ( path ) ;
1717
18- const followReExports = ( sources : Set < string > , nextId : string ) : [ boolean , string ?] | undefined => {
19- for ( const filePath of sources ) {
20- const file = graph . get ( filePath ) ;
21- if ( ! file ?. importedBy ) continue ;
22- const result = hasStrictlyNsReferences ( graph , filePath , file . importedBy , nextId , seenFiles ) ;
23- if ( result [ 0 ] === false && result [ 1 ] ) return result ;
24- if ( result [ 1 ] && ! namespace ) namespace = result [ 1 ] ;
25- }
26- return undefined ;
27- } ;
18+ let namespace : string | undefined ;
19+
20+ const follow = ( sources : Set < string > , nextId : string ) : [ boolean , string ?] | undefined => {
21+ for ( const filePath of sources ) {
22+ const file = graph . get ( filePath ) ;
23+ if ( ! file ?. importedBy ) continue ;
24+ const result = walkDown ( filePath , file . importedBy , nextId ) ;
25+ if ( result [ 0 ] === false && result [ 1 ] ) return result ;
26+ if ( result [ 1 ] && ! namespace ) namespace = result [ 1 ] ;
27+ }
28+ return undefined ;
29+ } ;
30+
31+ for ( const ns of importMaps . importNs . keys ( ) ) {
32+ if ( ! importMaps . refs . has ( ns ) ) return [ false , ns ] ;
2833
29- for ( const ns of importsForExport . importNs . keys ( ) ) {
30- const hasNsRef = importsForExport . refs . has ( ns ) ;
31- if ( ! hasNsRef ) return [ false , ns ] ;
34+ for ( const ref of importMaps . refs ) {
35+ if ( ref . startsWith ( ` ${ ns } .` ) ) return [ false , ns ] ;
36+ }
3237
33- for ( const ref of importsForExport . refs ) {
34- if ( ref . startsWith ( `${ ns } .` ) ) return [ false , ns ] ;
38+ namespace = ns ;
39+
40+ const nsAliases = getAliasReExportMap ( importMaps , ns ) ;
41+ if ( nsAliases ) {
42+ for ( const [ alias , sources ] of nsAliases ) {
43+ const result = follow ( sources , alias ) ;
44+ if ( result ) return result ;
45+ }
46+ }
3547 }
3648
37- namespace = ns ;
49+ const directSources = getPassThroughReExportSources ( importMaps , id ) ;
50+ if ( directSources ) {
51+ const result = follow ( directSources , id ) ;
52+ if ( result ) return result ;
53+ }
3854
39- const nsAliases = getAliasReExportMap ( importsForExport , ns ) ;
40- if ( nsAliases ) {
41- for ( const [ alias , sources ] of nsAliases ) {
42- const result = followReExports ( sources , alias ) ;
55+ const starSources = getStarReExportSources ( importMaps ) ;
56+ if ( starSources ) {
57+ const result = follow ( starSources , id ) ;
58+ if ( result ) return result ;
59+ }
60+
61+ const [ _id , ...rest ] = id . split ( '.' ) ;
62+ const aliasEntries = getAliasReExportMap ( importMaps , _id ) ;
63+ if ( aliasEntries ) {
64+ for ( const [ alias , sources ] of aliasEntries ) {
65+ const result = follow ( sources , [ alias , ...rest ] . join ( '.' ) ) ;
4366 if ( result ) return result ;
4467 }
4568 }
46- }
47-
48- const directSources = getPassThroughReExportSources ( importsForExport , identifier ) ;
49- if ( directSources ) {
50- const result = followReExports ( directSources , identifier ) ;
51- if ( result ) return result ;
52- }
53-
54- const starSources = getStarReExportSources ( importsForExport ) ;
55- if ( starSources ) {
56- const result = followReExports ( starSources , identifier ) ;
57- if ( result ) return result ;
58- }
59-
60- const [ id , ...rest ] = identifier . split ( '.' ) ;
61- const aliasEntries = getAliasReExportMap ( importsForExport , id ) ;
62- if ( aliasEntries ) {
63- for ( const [ alias , sources ] of aliasEntries ) {
64- const result = followReExports ( sources , [ alias , ...rest ] . join ( '.' ) ) ;
69+
70+ for ( const [ ns , sources ] of importMaps . reExportNs ) {
71+ const result = follow ( sources , `${ ns } .${ id } ` ) ;
6572 if ( result ) return result ;
6673 }
67- }
68-
69- for ( const [ ns , sources ] of importsForExport . reExportNs ) {
70- const result = followReExports ( sources , `${ ns } .${ identifier } ` ) ;
71- if ( result ) return result ;
72- }
73-
74- const importedSources = importsForExport . import . get ( identifier ) ;
75- if ( importedSources ) {
76- const result = followReExports ( importedSources , identifier ) ;
77- if ( result ) return result ;
78- }
79-
80- const importAsMap = importsForExport . importAs . get ( identifier ) ;
81- if ( importAsMap ) {
82- for ( const [ alias , sources ] of importAsMap ) {
83- const result = followReExports ( sources , alias ) ;
74+
75+ const importedSources = importMaps . import . get ( id ) ;
76+ if ( importedSources ) {
77+ const result = follow ( importedSources , id ) ;
8478 if ( result ) return result ;
8579 }
86- }
8780
88- if ( namespace ) return [ true , namespace ] ;
89- return [ false ] ;
81+ const importAsMap = importMaps . importAs . get ( id ) ;
82+ if ( importAsMap ) {
83+ for ( const [ alias , sources ] of importAsMap ) {
84+ const result = follow ( sources , alias ) ;
85+ if ( result ) return result ;
86+ }
87+ }
88+
89+ if ( namespace ) return [ true , namespace ] ;
90+ return [ false ] ;
91+ } ;
92+
93+ return walkDown ( filePath , importsForExport , identifier ) ;
9094} ;
0 commit comments