@@ -26,6 +26,20 @@ const isDirectory = (filename: string): boolean => {
26
26
return exists ;
27
27
} ;
28
28
29
+ const isModule = ( filename : string , ext : string ) : boolean => {
30
+ const exts = [ 'ts' , 'tsx' , ext ] ;
31
+
32
+ // Metro won't resolve these extensions if explicit extension is provided
33
+ // So we can't add extension to these files
34
+ const additional = [ 'native' , 'android' , 'ios' , 'web' ] ;
35
+
36
+ return exts . some (
37
+ ( ext ) =>
38
+ isFile ( `${ filename } .${ ext } ` ) &&
39
+ additional . every ( ( add ) => ! isFile ( `${ filename } .${ add } .${ ext } ` ) )
40
+ ) ;
41
+ } ;
42
+
29
43
const isTypeImport = (
30
44
node : ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration
31
45
) =>
@@ -110,27 +124,22 @@ export default function (
110
124
node . source . value
111
125
) ;
112
126
113
- // Add extension if .ts file or file with extension exists
114
- if (
115
- isFile ( `${ filename } .ts` ) ||
116
- isFile ( `${ filename } .tsx` ) ||
117
- isFile ( `${ filename } .${ extension } ` )
118
- ) {
119
- node . source . value += `.${ extension } ` ;
127
+ // Replace .ts extension with .js if file with extension is explicitly imported
128
+ if ( isFile ( filename ) ) {
129
+ node . source . value = node . source . value . replace ( / \. t s x ? $ / , `.${ extension } ` ) ;
120
130
return ;
121
131
}
122
132
123
- // Replace .ts extension with .js if .ts file exists
124
- if ( isFile ( filename ) ) {
125
- node . source . value = node . source . value . replace ( / \. t s x ? $ / , `.${ extension } ` ) ;
133
+ // Add extension if .ts file or file with extension exists
134
+ if ( isModule ( filename , extension ) ) {
135
+ node . source . value += `.${ extension } ` ;
126
136
return ;
127
137
}
128
138
139
+ // Expand folder imports to index and add extension
129
140
if (
130
141
isDirectory ( filename ) &&
131
- ( isFile ( path . join ( filename , 'index.ts' ) ) ||
132
- isFile ( path . join ( filename , 'index.tsx' ) ) ||
133
- isFile ( path . join ( filename , `index.${ extension } ` ) ) )
142
+ isModule ( path . join ( filename , 'index' ) , extension )
134
143
) {
135
144
node . source . value = node . source . value . replace (
136
145
/ \/ ? $ / ,
0 commit comments