1
1
// @flow
2
2
import type { TSModule , Export } from './TSModule' ;
3
- import typeof TypeScriptModule from 'typescript' ; // eslint-disable-line import/no-extraneous-dependencies
3
+
4
4
import nullthrows from 'nullthrows' ;
5
5
import invariant from 'assert' ;
6
+ import ts from 'typescript' ;
6
7
7
8
export class TSModuleGraph {
8
- ts : TypeScriptModule ;
9
9
modules : Map < string , TSModule > ;
10
10
mainModuleName: string ;
11
11
mainModule: ?TSModule ;
12
12
13
- constructor ( ts : TypeScriptModule , mainModuleName : string ) {
14
- this . ts = ts ;
13
+ constructor ( mainModuleName : string ) {
15
14
this . modules = new Map ( ) ;
16
15
this . mainModuleName = mainModuleName ;
17
16
this . mainModule = null ;
@@ -29,18 +28,16 @@ export class TSModuleGraph {
29
28
}
30
29
31
30
markUsed ( module : TSModule , name : string , context : any ) : void {
32
- let { ts} = this ;
33
-
34
31
// If name is imported, mark used in the original module
35
32
if ( module . imports . has ( name ) ) {
36
33
module . used . add ( name ) ;
37
- let { specifier , imported } = nullthrows ( module . imports . get ( name ) ) ;
38
- let m = this . getModule ( specifier ) ;
39
- if ( ! m ) {
34
+ let resolved = this . resolveImport ( module , name ) ;
35
+ // Missing or external
36
+ if ( ! resolved || resolved . module === module ) {
40
37
return ;
41
38
}
42
39
43
- return this . markUsed ( m , imported , context ) ;
40
+ return this . markUsed ( resolved . module , resolved . imported , context ) ;
44
41
}
45
42
46
43
if ( module . used . has ( name ) ) {
@@ -112,8 +109,8 @@ export class TSModuleGraph {
112
109
// Named export
113
110
return {
114
111
module : m ,
115
- name : m . getName ( exportName ) ,
116
- imported : e . imported || exportName ,
112
+ name : exportName ,
113
+ imported : e . imported != null ? m . getName ( e . imported ) : exportName ,
117
114
} ;
118
115
}
119
116
@@ -205,14 +202,22 @@ export class TSModuleGraph {
205
202
exportedNames . set ( e . name , e . module ) ;
206
203
}
207
204
205
+ let importedSymbolsToUpdate = [ ] ;
206
+
208
207
// Assign unique names across all modules
209
208
for ( let m of this . modules . values ( ) ) {
210
209
for ( let [ orig , name ] of m . names ) {
211
210
if ( exportedNames . has ( name ) && exportedNames . get ( name ) === m ) {
212
211
continue ;
213
212
}
214
213
215
- if ( ! m . used . has ( orig ) || m . imports . get ( orig ) ) {
214
+ if ( ! m . used . has ( orig ) ) {
215
+ continue ;
216
+ }
217
+
218
+ if ( m . imports . has ( orig ) ) {
219
+ // Update imports after all modules's local variables have been renamed
220
+ importedSymbolsToUpdate . push ( [ m , orig ] ) ;
216
221
continue ;
217
222
}
218
223
@@ -224,6 +229,11 @@ export class TSModuleGraph {
224
229
}
225
230
}
226
231
232
+ for ( let [ m , orig ] of importedSymbolsToUpdate ) {
233
+ let imported = nullthrows ( this . resolveImport ( m , orig ) ) ;
234
+ m . names . set ( orig , imported . imported ) ;
235
+ }
236
+
227
237
return exportedNames ;
228
238
}
229
239
}
0 commit comments