@@ -13,6 +13,17 @@ const transformer = <T extends ts.Node>(_: ts.Program) => {
13
13
resolve : paths [ key ] [ 0 ] // TODO should check if is not empty
14
14
} ) ) ;
15
15
let fileDir = "" ;
16
+ function findFileInPaths ( text : string ) {
17
+ for ( const path of regPaths ) {
18
+ const match = text . match ( path . regexp ) ;
19
+ if ( match ) {
20
+ const out = path . resolve . replace ( / \* / g, match [ 1 ] ) ;
21
+ const file = slash ( relative ( fileDir , resolve ( baseUrl , out ) ) ) ;
22
+ return file ;
23
+ }
24
+ }
25
+ return null ;
26
+ }
16
27
function visit ( node : ts . Node ) : ts . Node {
17
28
if ( ts . isSourceFile ( node ) ) {
18
29
fileDir = dirname ( node . fileName ) ;
@@ -22,11 +33,8 @@ const transformer = <T extends ts.Node>(_: ts.Program) => {
22
33
ts . isImportDeclaration ( node ) &&
23
34
ts . isStringLiteral ( node . moduleSpecifier )
24
35
) {
25
- for ( const path of regPaths ) {
26
- const match = node . moduleSpecifier . text . match ( path . regexp ) ;
27
- if ( match === null ) continue ;
28
- const out = path . resolve . replace ( / \* / g, match [ 1 ] ) ;
29
- const file = slash ( relative ( fileDir , resolve ( baseUrl , out ) ) ) ;
36
+ const file = findFileInPaths ( node . moduleSpecifier . text ) ;
37
+ if ( file ) {
30
38
return ts . updateImportDeclaration (
31
39
node ,
32
40
node . decorators ,
@@ -37,6 +45,23 @@ const transformer = <T extends ts.Node>(_: ts.Program) => {
37
45
) ;
38
46
}
39
47
}
48
+ if (
49
+ ts . isExportDeclaration ( node ) &&
50
+ node . moduleSpecifier &&
51
+ ts . isStringLiteral ( node . moduleSpecifier )
52
+ ) {
53
+ const file = findFileInPaths ( node . moduleSpecifier . text ) ;
54
+ if ( file ) {
55
+ return ts . updateExportDeclaration (
56
+ node ,
57
+ node . decorators ,
58
+ node . modifiers ,
59
+ node . exportClause ,
60
+ // If it's in the same level or below add the ./
61
+ ts . createLiteral ( file [ 0 ] === "." ? file : `./${ file } ` )
62
+ ) ;
63
+ }
64
+ }
40
65
return ts . visitEachChild ( node , visit , context ) ;
41
66
}
42
67
return ts . visitNode ( rootNode , visit ) ;
0 commit comments