@@ -39,28 +39,24 @@ export default function (
3939 extMappings . set ( base , addition ) ;
4040 }
4141
42- function shouldMutateModuleSpecifier ( node : Node ) : string | false {
43- if ( ! ts . isImportDeclaration ( node ) && ! ts . isExportDeclaration ( node ) ) return false ;
44- if ( node . moduleSpecifier === undefined ) return false ;
45- // only when module specifier is valid
46- if ( ! ts . isStringLiteral ( node . moduleSpecifier ) ) return false ;
42+ function shouldMutateImportSource ( node : Node | undefined ) : string | false {
43+ if ( ! node || ! ts . isStringLiteral ( node ) ) return false ;
4744 // only when path is relative
48- if ( ! node . moduleSpecifier . text . startsWith ( './' ) && ! node . moduleSpecifier . text . startsWith ( '../' ) )
49- return false ;
45+ if ( ! node . text . startsWith ( './' ) && ! node . text . startsWith ( '../' ) ) return false ;
5046 // only when module specifier has accepted extension
51- const ext = path . extname ( node . moduleSpecifier . text ) ;
47+ const ext = path . extname ( node . text ) ;
5248 if ( ! extMappings . has ( ext ) ) return false ;
53- return node . moduleSpecifier . text + extMappings . get ( ext ) ;
49+ return node . text + extMappings . get ( ext ) ;
5450 }
5551
5652 return ( ctx : TransformationContext ) => {
5753 const { factory} = ctx ;
5854
5955 return ( sourceFile : SourceFile ) => {
6056 function visit ( node : Node ) : Node {
61- const newImportSource = shouldMutateModuleSpecifier ( node ) ;
62- if ( newImportSource ) {
63- if ( ts . isImportDeclaration ( node ) ) {
57+ if ( ts . isImportDeclaration ( node ) ) {
58+ const newImportSource = shouldMutateImportSource ( node . moduleSpecifier ) ;
59+ if ( newImportSource ) {
6460 const newModuleSpecifier = factory . createStringLiteral ( newImportSource ) ;
6561 node = factory . updateImportDeclaration (
6662 node ,
@@ -69,7 +65,10 @@ export default function (
6965 newModuleSpecifier ,
7066 node . assertClause
7167 ) ;
72- } else if ( ts . isExportDeclaration ( node ) ) {
68+ }
69+ } else if ( ts . isExportDeclaration ( node ) ) {
70+ const newImportSource = shouldMutateImportSource ( node . moduleSpecifier ) ;
71+ if ( newImportSource ) {
7372 const newModuleSpecifier = factory . createStringLiteral ( newImportSource ) ;
7473 node = factory . updateExportDeclaration (
7574 node ,
@@ -80,6 +79,16 @@ export default function (
8079 node . assertClause
8180 ) ;
8281 }
82+ } else if (
83+ ts . isCallExpression ( node ) &&
84+ node . expression . kind === ts . SyntaxKind . ImportKeyword
85+ ) {
86+ const newImportSource = shouldMutateImportSource ( node . arguments [ 0 ] ) ;
87+ if ( newImportSource ) {
88+ const newArgs = node . arguments . slice ( ) ;
89+ newArgs [ 0 ] = factory . createStringLiteral ( newImportSource ) ;
90+ node = factory . updateCallExpression ( node , node . expression , node . typeArguments , newArgs ) ;
91+ }
8392 }
8493
8594 return ts . visitEachChild ( node , visit , ctx ) ;
0 commit comments