@@ -35,10 +35,9 @@ module.exports = class TsconfigPathsPlugin {
35
35
apply ( resolver ) {
36
36
if ( ! this . configFile ) return ;
37
37
38
- const fs = resolver . fileSystem ;
39
38
const aliasTarget = resolver . ensureHook ( "internal-resolve" ) ;
40
39
const moduleTarget = resolver . ensureHook ( "module" ) ;
41
- const aliasOptionsPromise = this . loadAndConvertPaths ( fs ) ;
40
+ const aliasOptionsPromise = this . loadAndConvertPaths ( resolver ) ;
42
41
43
42
resolver
44
43
. getHook ( "raw-resolve" )
@@ -90,25 +89,30 @@ module.exports = class TsconfigPathsPlugin {
90
89
91
90
/**
92
91
* Load tsconfig.json (and referenced tsconfigs) and convert paths to AliasPlugin format
93
- * @param {import("./ Resolver").FileSystem } fs the file system
92
+ * @param {Resolver } resolver the file system
94
93
* @returns {Promise<AliasOption[]> } Array of alias options for AliasPlugin
95
94
*/
96
- async loadAndConvertPaths ( fs ) {
95
+ async loadAndConvertPaths ( resolver ) {
97
96
try {
97
+ const fs = resolver . fileSystem ;
98
98
const configPath = path . isAbsolute ( this . configFile )
99
99
? this . configFile
100
- : path . resolve ( process . cwd ( ) , this . configFile ) ;
100
+ : resolver . join ( process . cwd ( ) , this . configFile ) ;
101
101
102
102
const mainOptions = await this . readTsconfigCompilerOptions (
103
- fs ,
103
+ resolver ,
104
104
configPath ,
105
105
) ;
106
106
if ( ! mainOptions ) return [ ] ;
107
107
108
108
/** @type {AliasOption[] } */
109
109
const aliases = [ ] ;
110
110
aliases . push (
111
- ...this . convertPathsToAliases ( mainOptions . paths , mainOptions . baseUrl ) ,
111
+ ...this . convertPathsToAliases (
112
+ resolver ,
113
+ mainOptions . paths ,
114
+ mainOptions . baseUrl ,
115
+ ) ,
112
116
) ;
113
117
114
118
// Collect references from the main tsconfig.json
@@ -129,7 +133,7 @@ module.exports = class TsconfigPathsPlugin {
129
133
if ( ! refPathLike ) continue ;
130
134
let refPath = path . isAbsolute ( refPathLike )
131
135
? refPathLike
132
- : path . resolve ( path . dirname ( configPath ) , refPathLike ) ;
136
+ : resolver . join ( path . dirname ( configPath ) , refPathLike ) ;
133
137
// If reference points to a directory, append tsconfig.json
134
138
try {
135
139
const stat = await new Promise ( ( resolve , reject ) => {
@@ -139,19 +143,23 @@ module.exports = class TsconfigPathsPlugin {
139
143
} ) ;
140
144
} ) ;
141
145
if ( stat . isDirectory ( ) ) {
142
- refPath = path . join ( refPath , "tsconfig.json" ) ;
146
+ refPath = resolver . join ( refPath , "tsconfig.json" ) ;
143
147
}
144
148
} catch ( _e ) {
145
149
// if it doesn't exist as directory/file, try adding tsconfig.json
146
150
if ( ! / \. j s o n $ / i. test ( refPath ) ) {
147
- refPath = path . join ( refPath , "tsconfig.json" ) ;
151
+ refPath = resolver . join ( refPath , "tsconfig.json" ) ;
148
152
}
149
153
}
150
154
151
155
const refOptions = await this . readTsconfigCompilerOptions ( fs , refPath ) ;
152
156
if ( ! refOptions ) continue ;
153
157
aliases . push (
154
- ...this . convertPathsToAliases ( refOptions . paths , refOptions . baseUrl ) ,
158
+ ...this . convertPathsToAliases (
159
+ resolver ,
160
+ refOptions . paths ,
161
+ refOptions . baseUrl ,
162
+ ) ,
155
163
) ;
156
164
}
157
165
@@ -163,12 +171,13 @@ module.exports = class TsconfigPathsPlugin {
163
171
164
172
/**
165
173
* Read tsconfig.json and return normalized compiler options
166
- * @param {import("./ Resolver").FileSystem } fs the file system
174
+ * @param {Resolver } resolver the resolver
167
175
* @param {string } absTsconfigPath absolute path to tsconfig.json
168
176
* @returns {Promise<{ baseUrl: string, paths: {[key: string]: string[]} } | null> } the normalized compiler options
169
177
*/
170
- async readTsconfigCompilerOptions ( fs , absTsconfigPath ) {
178
+ async readTsconfigCompilerOptions ( resolver , absTsconfigPath ) {
171
179
try {
180
+ const fs = resolver . fileSystem ;
172
181
const json = await new Promise ( ( resolve , reject ) => {
173
182
fs . readFile ( absTsconfigPath , "utf8" , ( err , data ) => {
174
183
if ( err ) reject ( err ) ;
@@ -181,7 +190,7 @@ module.exports = class TsconfigPathsPlugin {
181
190
if ( ! baseUrl ) {
182
191
baseUrl = path . dirname ( absTsconfigPath ) ;
183
192
} else if ( ! path . isAbsolute ( baseUrl ) ) {
184
- baseUrl = path . resolve ( path . dirname ( absTsconfigPath ) , baseUrl ) ;
193
+ baseUrl = resolver . join ( path . dirname ( absTsconfigPath ) , baseUrl ) ;
185
194
}
186
195
const paths = compilerOptions . paths || { } ;
187
196
return { baseUrl, paths } ;
@@ -192,27 +201,28 @@ module.exports = class TsconfigPathsPlugin {
192
201
193
202
/**
194
203
* Convert TypeScript paths configuration to AliasPlugin aliases
204
+ * @param {Resolver } resolver the resolver
195
205
* @param {{[key: string]: string[]} } paths TypeScript paths mapping
196
206
* @param {string } baseUrl Base URL for resolving paths
197
207
* @returns {AliasOption[] } Array of alias options
198
208
*/
199
- convertPathsToAliases ( paths , baseUrl ) {
209
+ convertPathsToAliases ( resolver , paths , baseUrl ) {
200
210
/** @type {AliasOption[] } */
201
211
const aliases = [ ] ;
202
212
203
213
for ( const [ pattern , mappings ] of Object . entries ( paths ) ) {
204
214
// Handle exact matches (no wildcards)
205
215
if ( ! pattern . includes ( "*" ) ) {
206
216
if ( mappings . length > 0 ) {
207
- const targetPath = path . resolve ( baseUrl , mappings [ 0 ] ) ;
217
+ const targetPath = resolver . join ( baseUrl , mappings [ 0 ] ) ;
208
218
aliases . push ( { name : pattern , alias : targetPath } ) ;
209
219
}
210
220
} else {
211
221
// Handle wildcard patterns by mapping the directory
212
222
const aliasName = pattern . replace ( / \/ \* $ / , "" ) ;
213
223
// Convert targets like "dir/*" -> "dir"
214
224
const aliasTargets = mappings . map ( ( mapping ) =>
215
- path . resolve ( baseUrl , mapping . replace ( / \/ \* $ / , "" ) ) ,
225
+ resolver . join ( baseUrl , mapping . replace ( / \/ \* $ / , "" ) ) ,
216
226
) ;
217
227
if ( aliasTargets . length > 0 ) {
218
228
aliases . push ( { name : aliasName , alias : aliasTargets [ 0 ] } ) ;
0 commit comments