@@ -65,6 +65,7 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
65
65
// if it's relative path do not transform
66
66
return moduleName ;
67
67
}
68
+
68
69
for ( const { regexp, path } of binds ) {
69
70
const match = regexp . exec ( moduleName ) ;
70
71
if ( match ) {
@@ -81,6 +82,19 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
81
82
return undefined ;
82
83
}
83
84
85
+ const isRequire = ( node : ts . Node ) : node is ts . CallExpression =>
86
+ ts . isCallExpression ( node ) &&
87
+ ts . isIdentifier ( node . expression ) &&
88
+ node . expression . text === "require" &&
89
+ ts . isStringLiteral ( node . arguments [ 0 ] ) &&
90
+ node . arguments . length === 1 ;
91
+
92
+ const isAsyncImport = ( node : ts . Node ) : node is ts . CallExpression =>
93
+ ts . isCallExpression ( node ) &&
94
+ node . expression . kind === ts . SyntaxKind . ImportKeyword &&
95
+ ts . isStringLiteral ( node . arguments [ 0 ] ) &&
96
+ node . arguments . length === 1 ;
97
+
84
98
function visit ( node : ts . Node ) : ts . VisitResult < ts . Node > {
85
99
if (
86
100
! isDeclarationFile &&
@@ -92,36 +106,71 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
92
106
) {
93
107
return undefined ;
94
108
}
95
- if (
96
- ts . isCallExpression ( node ) &&
97
- ts . isIdentifier ( node . expression ) &&
98
- node . expression . text === "require" &&
99
- ts . isStringLiteral ( node . arguments [ 0 ] ) &&
100
- node . arguments . length === 1
101
- ) {
102
- const firstArg = node . arguments [ 0 ] as ts . StringLiteral ;
103
- const file = bindModuleToFile ( firstArg . text ) ;
104
- if ( ! file ) {
105
- return node ;
106
- }
107
- const fileLiteral = ts . createLiteral ( file ) ;
108
- return ts . updateCall ( node , node . expression , node . typeArguments , [
109
- fileLiteral
110
- ] ) ;
109
+
110
+ if ( isRequire ( node ) || isAsyncImport ( node ) ) {
111
+ return unpathRequireAndAsyncImport ( node ) ;
111
112
}
113
+
112
114
if ( ts . isExternalModuleReference ( node ) ) {
113
115
return unpathImportEqualsDeclaration ( node ) ;
114
116
}
117
+
115
118
if ( ts . isImportDeclaration ( node ) ) {
116
119
return unpathImportDeclaration ( node ) ;
117
120
}
121
+
118
122
if ( ts . isExportDeclaration ( node ) ) {
119
123
return unpathExportDeclaration ( node ) ;
120
124
}
121
125
126
+ if ( ts . isImportTypeNode ( node ) ) {
127
+ return unpathImportTypeNode ( node ) ;
128
+ }
129
+
122
130
return ts . visitEachChild ( node , visit , context ) ;
123
131
}
124
132
133
+ function unpathRequireAndAsyncImport ( node : ts . CallExpression ) {
134
+ const firstArg = node . arguments [ 0 ] as ts . StringLiteral ;
135
+ const file = bindModuleToFile ( firstArg . text ) ;
136
+
137
+ if ( ! file ) {
138
+ return node ;
139
+ }
140
+
141
+ const fileLiteral = ts . createLiteral ( file ) ;
142
+
143
+ return ts . updateCall ( node , node . expression , node . typeArguments , [
144
+ fileLiteral
145
+ ] ) ;
146
+ }
147
+
148
+ function unpathImportTypeNode ( node : ts . ImportTypeNode ) {
149
+ const argument = node . argument as ts . LiteralTypeNode ;
150
+ const literal = argument . literal ;
151
+
152
+ if ( ! ts . isStringLiteral ( literal ) ) {
153
+ return node ;
154
+ }
155
+
156
+ const file = bindModuleToFile ( literal . text ) ;
157
+
158
+ if ( ! file ) {
159
+ return node ;
160
+ }
161
+
162
+ const fileLiteral = ts . createLiteral ( file ) ;
163
+ const fileArgument = ts . updateLiteralTypeNode ( argument , fileLiteral ) ;
164
+
165
+ return ts . updateImportTypeNode (
166
+ node ,
167
+ fileArgument ,
168
+ node . qualifier ,
169
+ node . typeArguments ,
170
+ node . isTypeOf
171
+ ) ;
172
+ }
173
+
125
174
function unpathImportEqualsDeclaration ( node : ts . ExternalModuleReference ) {
126
175
if ( ! ts . isStringLiteral ( node . expression ) ) {
127
176
return node ;
0 commit comments