@@ -9,8 +9,7 @@ import { normalizePath, Plugin, searchForWorkspaceRoot } from 'vite'
9
9
import { resolvePathMappings } from './mappings'
10
10
import { basename , dirname , isAbsolute , join , relative } from './path'
11
11
import { PluginOptions } from './types'
12
-
13
- const debug = _debug ( 'vite-tsconfig-paths' )
12
+ import { debug , debugResolve } from './debug'
14
13
15
14
const noMatch = [ undefined , false ] as [ undefined , false ]
16
15
@@ -161,36 +160,52 @@ export default (opts: PluginOptions = {}): Plugin => {
161
160
} )
162
161
} ,
163
162
async resolveId ( id , importer , options ) {
164
- if ( importer && ! relativeImportRE . test ( id ) && ! isAbsolute ( id ) ) {
165
- // For Vite 4 and under, skipSelf needs to be set.
166
- const resolveOptions = { ...options , skipSelf : true }
167
- const viteResolve : ViteResolve = async ( id , importer ) =>
168
- ( await this . resolve ( id , importer , resolveOptions ) ) ?. id
169
-
170
- let prevProjectDir : string | undefined
171
- let projectDir = normalizePath ( dirname ( importer ) )
172
-
173
- // Find the nearest directory with a matching tsconfig file.
174
- loop: while ( projectDir && projectDir != prevProjectDir ) {
175
- const resolvers = resolversByDir [ projectDir ]
176
- if ( resolvers )
177
- for ( const resolve of resolvers ) {
178
- const [ resolved , matched ] = await resolve (
179
- viteResolve ,
180
- id ,
181
- importer
182
- )
183
- if ( resolved ) {
184
- return resolved
185
- }
186
- if ( matched ) {
187
- // Once a matching resolver is found, stop looking.
188
- break loop
189
- }
163
+ if ( debugResolve . enabled ) {
164
+ debugResolve ( 'resolving:' , { id, importer } )
165
+ }
166
+
167
+ if ( ! importer ) {
168
+ debugResolve ( 'importer is empty or undefined. skipping...' )
169
+ return
170
+ }
171
+ if ( relativeImportRE . test ( id ) ) {
172
+ debugResolve ( 'id is a relative import. skipping...' )
173
+ return
174
+ }
175
+ if ( isAbsolute ( id ) ) {
176
+ debugResolve ( 'id is an absolute path. skipping...' )
177
+ return
178
+ }
179
+ if ( id . includes ( '\0' ) ) {
180
+ debugResolve ( 'id is a virtual module. skipping...' )
181
+ return
182
+ }
183
+
184
+ // For Vite 4 and under, skipSelf needs to be set.
185
+ const resolveOptions = { ...options , skipSelf : true }
186
+ const viteResolve : ViteResolve = async ( id , importer ) =>
187
+ ( await this . resolve ( id , importer , resolveOptions ) ) ?. id
188
+
189
+ let prevProjectDir : string | undefined
190
+ let projectDir = normalizePath ( dirname ( importer ) )
191
+
192
+ // Find the nearest directory with a matching tsconfig file.
193
+ loop: while ( projectDir && projectDir != prevProjectDir ) {
194
+ const resolvers = resolversByDir [ projectDir ]
195
+ if ( resolvers ) {
196
+ for ( const resolve of resolvers ) {
197
+ const [ resolved , matched ] = await resolve ( viteResolve , id , importer )
198
+ if ( resolved ) {
199
+ return resolved
190
200
}
191
- prevProjectDir = projectDir
192
- projectDir = dirname ( prevProjectDir )
201
+ if ( matched ) {
202
+ // Once a matching resolver is found, stop looking.
203
+ break loop
204
+ }
205
+ }
193
206
}
207
+ prevProjectDir = projectDir
208
+ projectDir = dirname ( prevProjectDir )
194
209
}
195
210
} ,
196
211
}
@@ -231,7 +246,11 @@ export default (opts: PluginOptions = {}): Plugin => {
231
246
) => Promise < string | undefined >
232
247
233
248
const resolveWithBaseUrl : InternalResolver | undefined = baseUrl
234
- ? ( viteResolve , id , importer ) => viteResolve ( join ( baseUrl , id ) , importer )
249
+ ? ( viteResolve , id , importer ) => {
250
+ const absoluteId = join ( baseUrl , id )
251
+ debugResolve ( 'trying with baseUrl:' , absoluteId )
252
+ return viteResolve ( absoluteId , importer )
253
+ }
235
254
: undefined
236
255
237
256
let resolveId : InternalResolver
@@ -259,6 +278,7 @@ export default (opts: PluginOptions = {}): Plugin => {
259
278
const matchIndex = Math . min ( ++ starCount , match . length - 1 )
260
279
return match [ matchIndex ]
261
280
} )
281
+ debugResolve ( 'found match, trying to resolve:' , mappedId )
262
282
const resolved = await viteResolve ( mappedId , importer )
263
283
if ( resolved ) {
264
284
return resolved
@@ -301,23 +321,24 @@ export default (opts: PluginOptions = {}): Plugin => {
301
321
: / \. [ m c ] ? t s x ? $ /
302
322
303
323
const resolutionCache = new Map < string , string > ( )
304
- return async ( viteResolve , id , importer ) => {
305
- // Skip virtual modules.
306
- if ( id . includes ( '\0' ) ) {
307
- return noMatch
308
- }
309
324
325
+ return async ( viteResolve , id , importer ) => {
326
+ // Ideally, Vite would normalize the importer path for us.
310
327
importer = normalizePath ( importer )
328
+
329
+ // Remove query and hash parameters from the importer path.
311
330
const importerFile = importer . replace ( / [ # ? ] .+ $ / , '' )
312
331
313
332
// Ignore importers with unsupported extensions.
314
333
if ( ! importerExtRE . test ( importerFile ) ) {
334
+ debugResolve ( 'importer has unsupported extension. skipping...' )
315
335
return noMatch
316
336
}
317
337
318
338
// Respect the include/exclude properties.
319
339
const relativeImporterFile = relative ( configDir , importerFile )
320
340
if ( ! isIncludedRelative ( relativeImporterFile ) ) {
341
+ debugResolve ( 'importer is not included. skipping...' )
321
342
return noMatch
322
343
}
323
344
@@ -328,21 +349,29 @@ export default (opts: PluginOptions = {}): Plugin => {
328
349
id = id . slice ( 0 , - suffix . length )
329
350
}
330
351
331
- let path = resolutionCache . get ( id )
332
- if ( ! path ) {
333
- path = await resolveId ( viteResolve , id , importer )
334
- if ( ! path ) {
352
+ let resolvedId = resolutionCache . get ( id )
353
+ if ( ! resolvedId ) {
354
+ resolvedId = await resolveId ( viteResolve , id , importer )
355
+ if ( ! resolvedId ) {
335
356
return noMatch
336
357
}
337
- resolutionCache . set ( id , path )
338
- debug ( `resolved:` , {
339
- id,
340
- importer,
341
- resolvedId : path ,
342
- configPath,
343
- } )
358
+ resolutionCache . set ( id , resolvedId )
359
+ if ( debugResolve . enabled ) {
360
+ debugResolve ( 'resolved without error:' , {
361
+ id,
362
+ importer,
363
+ resolvedId,
364
+ configPath,
365
+ } )
366
+ }
344
367
}
345
- return [ path && suffix ? path + suffix : path , true ]
368
+
369
+ // Restore the suffix if one was removed earlier.
370
+ if ( suffix ) {
371
+ resolvedId += suffix
372
+ }
373
+
374
+ return [ resolvedId , true ]
346
375
}
347
376
}
348
377
}
0 commit comments