@@ -5,40 +5,44 @@ const exts = ['.js', '.ts', '.tsx'];
55const targetDirs = [ 'api' , 'ui' , 'upload-api' ] ;
66
77function resolveImport ( importPath , fileDir ) {
8+ // Absolute or relative path
89 let basePath = path . resolve ( fileDir , importPath ) ;
910
10- // 1. Direct file with extension
11- if ( fs . existsSync ( basePath ) && fs . statSync ( basePath ) . isFile ( ) ) {
12- return true ;
13- }
14-
15- // 2. Try with extensions
16- for ( const ext of exts ) {
17- if ( fs . existsSync ( basePath + ext ) ) {
11+ // 1. If import has extension, check directly
12+ if ( / \. [ j t ] s x ? $ / . test ( importPath ) ) {
13+ if ( fs . existsSync ( basePath ) && fs . statSync ( basePath ) . isFile ( ) ) {
1814 return true ;
1915 }
20- }
21-
22- // 3. Directory with index file
23- if ( fs . existsSync ( basePath ) && fs . statSync ( basePath ) . isDirectory ( ) ) {
16+ } else {
17+ // 2. Try with extensions
2418 for ( const ext of exts ) {
25- if ( fs . existsSync ( path . join ( basePath , 'index' + ext ) ) ) {
19+ if ( fs . existsSync ( basePath + ext ) && fs . statSync ( basePath + ext ) . isFile ( ) ) {
2620 return true ;
2721 }
2822 }
23+ // 3. Directory with index file
24+ if ( fs . existsSync ( basePath ) && fs . statSync ( basePath ) . isDirectory ( ) ) {
25+ for ( const ext of exts ) {
26+ const idx = path . join ( basePath , 'index' + ext ) ;
27+ if ( fs . existsSync ( idx ) && fs . statSync ( idx ) . isFile ( ) ) {
28+ return true ;
29+ }
30+ }
31+ }
2932 }
30-
3133 return false ;
3234}
3335
3436function cleanImportsInFile ( filePath ) {
3537 let code = fs . readFileSync ( filePath , 'utf-8' ) ;
3638 const fileDir = path . dirname ( filePath ) ;
3739
40+ // Handles both regular and type-only imports
3841 const importRegex = / ^ i m p o r t \s + ( t y p e \s + ) ? [ \s \S ] * ?f r o m \s + [ ' " ] ( .* ?) [ ' " ] ; ? .* $ / gm;
3942
4043 let changed = false ;
4144 code = code . replace ( importRegex , ( match , typeKeyword , importPath ) => {
45+ // Ignore node_modules and built-in modules
4246 if ( ! importPath . startsWith ( '.' ) && ! importPath . startsWith ( '/' ) ) return match ;
4347
4448 if ( ! resolveImport ( importPath , fileDir ) ) {
0 commit comments