@@ -41,7 +41,25 @@ function resolveTSAlias(tsconfigpath: string, to: string, cwd: string) {
4141 return to ;
4242}
4343
44- const codeExt = [ ".ts" , ".js" , ".tsx" , ".jsx" ] ;
44+ const codeExt = [ ".ts" , ".js" , ".tsx" , ".jsx" , ".mjs" , ".mjsx" , ".mts" , ".mtsx" , ".cjs" , ".cjsx" , ".cts" , ".ctsx" ] ;
45+ const extAlts = {
46+ // Base family
47+ ".js" : [ ".jsx" , ".ts" , ".tsx" ] ,
48+ ".jsx" : [ ".js" , ".tsx" , ".ts" ] ,
49+ ".ts" : [ ".tsx" , ".js" , ".jsx" ] ,
50+ ".tsx" : [ ".ts" , ".jsx" , ".js" ] ,
51+ // ESM-explicit family (m*)
52+ ".mjs" : [ ".mts" , ".mjsx" , ".mtsx" ] ,
53+ ".mjsx" : [ ".mjs" , ".mtsx" , ".mts" ] ,
54+ ".mts" : [ ".mtsx" , ".mjs" , ".mjsx" ] ,
55+ ".mtsx" : [ ".mts" , ".mjsx" , ".mjs" ] ,
56+ // CJS-explicit family (c*)
57+ ".cjs" : [ ".cts" , ".cjsx" , ".ctsx" ] ,
58+ ".cjsx" : [ ".cjs" , ".ctsx" , ".cts" ] ,
59+ ".cts" : [ ".ctsx" , ".cjs" , ".cjsx" ] ,
60+ ".ctsx" : [ ".cts" , ".cjsx" , ".cjs" ] ,
61+ } ;
62+
4563
4664function endsWithAny ( str : string , arr : string [ ] ) {
4765 return arr . some ( ( ext ) => str . endsWith ( ext ) ) ;
@@ -92,14 +110,11 @@ export function getImportDeclaration(
92110 }
93111
94112 if ( ! existsSync ( res ) ) {
95- if ( res . endsWith ( ".js" ) ) {
96- res = findFileExtension ( res . replace ( ".js" , "" ) , [ ".jsx" , ".ts" , ".tsx" ] ) ;
97- } else if ( res . endsWith ( ".jsx" ) ) {
98- res = findFileExtension ( res . replace ( ".jsx" , "" ) , [ ".js" , ".tsx" , ".ts" ] ) ;
99- } else if ( res . endsWith ( ".ts" ) ) {
100- res = findFileExtension ( res . replace ( ".ts" , "" ) , [ ".tsx" , ".js" , ".jsx" ] ) ;
101- } else if ( res . endsWith ( ".tsx" ) ) {
102- res = findFileExtension ( res . replace ( ".tsx" , "" ) , [ ".ts" , ".jsx" , ".js" ] ) ;
113+ for ( const [ ext , alternatives ] of Object . entries ( extAlts ) ) {
114+ if ( res . endsWith ( ext ) ) {
115+ res = findFileExtension ( res . slice ( 0 , - ext . length ) , alternatives ) ;
116+ break ;
117+ }
103118 }
104119 }
105120
0 commit comments