-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve-path.mts
78 lines (69 loc) · 2.01 KB
/
resolve-path.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* @file resolvePath
* @module tsconfig-utils/lib/resolvePath
*/
import withTrailingSlash from '#internal/with-trailing-slash'
import isResolvedTsconfig from '#lib/is-resolved-tsconfig'
import * as mlly from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
import type { Tsconfig } from '@flex-development/tsconfig-types'
import type { ResolvePathOptions } from '@flex-development/tsconfig-utils'
/**
* Resolve an aliased `specifier`.
*
* @see {@linkcode ResolvePathOptions}
*
* @this {void}
*
* @param {string} specifier
* The module specifier to resolve
* @param {ResolvePathOptions | null | undefined} [options]
* Alias resolution options
* @return {string | null}
* Resolved specifier or `null` if path alias match is not found
*/
function resolvePath(
this: void,
specifier: string,
options?: ResolvePathOptions | null | undefined
): string | null {
/**
* URL of directory to resolve non-absolute modules from.
*
* @var {mlly.ModuleId | null | undefined} baseUrl
*/
let baseUrl: mlly.ModuleId | null | undefined
/**
* URL of parent module.
*
* @var {mlly.ModuleId | null | undefined} parent
*/
let parent: mlly.ModuleId | null | undefined
/**
* Tsconfig object.
*
* @var {Tsconfig | null | undefined} tsconfig
*/
let tsconfig: Tsconfig | null | undefined
if (isResolvedTsconfig(options?.tsconfig)) {
tsconfig = options.tsconfig.tsconfig
baseUrl = new URL(pathe.dot, options.tsconfig.url)
parent = options.tsconfig.url
} else if (options) {
tsconfig = options.tsconfig
baseUrl = options.cwd
}
if (typeof tsconfig?.compilerOptions?.baseUrl === 'string') {
baseUrl = tsconfig.compilerOptions.baseUrl
}
if (baseUrl !== null && baseUrl !== undefined && baseUrl !== parent) {
baseUrl = withTrailingSlash(mlly.toUrl(baseUrl))
}
return mlly.resolveAlias(specifier, {
...options,
aliases: tsconfig?.compilerOptions?.paths,
cwd: baseUrl,
parent: parent ?? baseUrl
})
}
export default resolvePath