@@ -3,6 +3,7 @@ import path from 'path';
3
3
import JSON5 from 'json5' ;
4
4
5
5
import { lilconfigSync } from 'lilconfig' ;
6
+ import { resolveJson5File } from './resolveJson5File' ;
6
7
7
8
const validate = {
8
9
string : ( x : unknown ) : x is string => typeof x === 'string' ,
@@ -54,22 +55,46 @@ export const resolveAliasedImport = ({
54
55
'.json' : ( _ , content ) => JSON5 . parse ( content ) ,
55
56
} ,
56
57
} ) ;
57
- const config = searcher . search ( location ) ;
58
+ let config = searcher . search ( location ) ;
58
59
59
60
if ( config == null ) {
60
61
return null ;
61
62
}
62
63
63
- const paths : unknown = config . config ?. compilerOptions ?. paths ;
64
+ let configLocation = path . dirname ( config . filepath ) ;
64
65
65
- const potentialBaseUrl : unknown = config . config ?. compilerOptions ?. baseUrl ;
66
+ let paths : unknown = config . config ?. compilerOptions ?. paths ;
67
+ let pathsBase = configLocation ;
66
68
67
- const configLocation = path . dirname ( config . filepath ) ;
69
+ let potentialBaseUrl : unknown = config . config ?. compilerOptions ?. baseUrl ;
70
+ let baseUrl = validate . string ( potentialBaseUrl )
71
+ ? path . resolve ( configLocation , potentialBaseUrl )
72
+ : null ;
73
+
74
+ let depth = 0 ;
75
+ while ( ( ! paths || ! baseUrl ) && config . config ?. extends && depth ++ < 10 ) {
76
+ config = resolveJson5File ( {
77
+ path : config . config . extends ,
78
+ base : configLocation ,
79
+ } ) ;
80
+ if ( config == null ) {
81
+ return null ;
82
+ }
83
+ configLocation = path . dirname ( config . filepath ) ;
84
+ if ( ! paths && config . config ?. compilerOptions ?. paths ) {
85
+ paths = config . config . compilerOptions . paths ;
86
+ pathsBase = configLocation ;
87
+ }
88
+ if ( ! baseUrl && config . config ?. compilerOptions ?. baseUrl ) {
89
+ potentialBaseUrl = config . config . compilerOptions . baseUrl ;
90
+ baseUrl = validate . string ( potentialBaseUrl )
91
+ ? path . resolve ( configLocation , potentialBaseUrl )
92
+ : null ;
93
+ }
94
+ }
68
95
69
96
if ( validate . tsconfigPaths ( paths ) ) {
70
- const baseUrl = validate . string ( potentialBaseUrl )
71
- ? potentialBaseUrl
72
- : '.' ;
97
+ baseUrl = baseUrl || pathsBase ;
73
98
74
99
for ( const alias in paths ) {
75
100
const aliasRe = new RegExp ( alias . replace ( '*' , '(.+)' ) , '' ) ;
@@ -80,7 +105,6 @@ export const resolveAliasedImport = ({
80
105
81
106
for ( const potentialAliasLocation of paths [ alias ] ) {
82
107
const resolvedFileLocation = path . resolve (
83
- configLocation ,
84
108
baseUrl ,
85
109
potentialAliasLocation
86
110
// "./utils/*" -> "./utils/style.module.css"
@@ -100,12 +124,8 @@ export const resolveAliasedImport = ({
100
124
// so here we only try and resolve the file if baseUrl is explcitly set and valid
101
125
// i.e. if no baseUrl is provided
102
126
// then no imports relative to baseUrl on its own are allowed, only relative to paths
103
- if ( validate . string ( potentialBaseUrl ) ) {
104
- const resolvedFileLocation = path . resolve (
105
- configLocation ,
106
- potentialBaseUrl ,
107
- importFilepath ,
108
- ) ;
127
+ if ( baseUrl ) {
128
+ const resolvedFileLocation = path . resolve ( baseUrl , importFilepath ) ;
109
129
110
130
if ( fs . existsSync ( resolvedFileLocation ) ) {
111
131
return resolvedFileLocation ;
0 commit comments