66import { Token } from 'vue-eslint-parser/ast/tokens' ;
77import { model } from './Model' ;
88import { existsSync } from 'fs' ;
9- import { resolve , extname , dirname } from 'path' ;
9+ import { resolve , extname , dirname , normalize } from 'path' ;
10+ import { type TsConfigJson } from 'get-tsconfig' ;
1011
1112/**
1213 * get only Import Declaration syntax.
@@ -94,11 +95,28 @@ export const resolveFile = (_filename: string, _currentFileName: string): string
9495 filename = resolve ( dirnameOfCurrentFile , _filename ) ;
9596 } else if ( _filename . startsWith ( './' ) ) {
9697 filename = `${ dirnameOfCurrentFile } /${ _filename . replace ( / \. \/ | / ug, '' ) } ` ;
98+ } else if ( model . tsconfigPathMapping . size > 0 ) {
99+ // `@@` should be processed before `@`
100+ const keys = Array . from ( model . tsconfigPathMapping . keys ( ) ) . sort ( ) . reverse ( ) ;
101+
102+ for ( let index = 0 ; index < keys . length ; index ++ ) {
103+ const key = keys [ index ] ;
104+ const replaceTo = model . tsconfigPathMapping . get ( key ) ;
105+
106+ if ( _filename . startsWith ( key ) && replaceTo ) {
107+ filename = _filename . replace ( key , replaceTo ) ;
108+
109+ break ;
110+ }
111+ }
97112 } else if ( _filename . startsWith ( '~' ) || _filename . startsWith ( '@' ) ) {
98113 filename = _filename . replace ( '~' , model . resourceRoot ) . replace ( '@' , model . resourceRoot ) ;
99114 }
100115
116+ // filename is empty when import third-party script
101117 if ( filename ) {
118+ filename = normalize ( filename ) ;
119+
102120 if ( extname ( filename ) === '' ) {
103121 if ( existsSync ( `${ filename } .vue` ) ) {
104122 return `${ filename } .vue` ;
@@ -112,3 +130,21 @@ export const resolveFile = (_filename: string, _currentFileName: string): string
112130
113131 return filename ;
114132} ;
133+
134+ export const getTsConfigPathMapping = ( compilerOptions : TsConfigJson . CompilerOptions ) : Map < string , string > => {
135+ const { paths, baseUrl} = compilerOptions ;
136+ const pathMaps = new Map < string , string > ( ) ;
137+
138+ if ( ! baseUrl || ! paths ) {
139+ return pathMaps ;
140+ }
141+
142+ for ( const key in paths ) {
143+ if ( Object . hasOwn ( paths , key ) ) {
144+ // only use the first path for now.
145+ pathMaps . set ( key . replace ( '/*' , '' ) , resolve ( model . resourceRoot , baseUrl , paths [ key ] [ 0 ] . replace ( '/*' , '' ) ) ) ;
146+ }
147+ }
148+
149+ return pathMaps ;
150+ } ;
0 commit comments