-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve-alias.mts
122 lines (109 loc) · 2.91 KB
/
resolve-alias.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @file resolveAlias
* @module mlly/lib/resolveAlias
*/
import chars from '#internal/chars'
import cwd from '#lib/cwd'
import isRelativeSpecifier from '#lib/is-relative-specifier'
import patternMatch from '#lib/pattern-match'
import toRelativeSpecifier from '#lib/to-relative-specifier'
import type { PatternMatch, ResolveAliasOptions } from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
export default resolveAlias
/**
* Resolve an aliased `specifier`.
*
* @see {@linkcode ResolveAliasOptions}
*
* @param {string} specifier
* The module specifier to resolve
* @param {ResolveAliasOptions | null | undefined} [options]
* Alias resolution options
* @return {string | null }
* Specifier of aliased module or `null` if path alias match is not found
*/
function resolveAlias(
specifier: string,
options?: ResolveAliasOptions | null | undefined
): string | null {
/**
* Resolved alias.
*
* @var {string | null} aliased
*/
let aliased: string | null = null
if (options) {
if (typeof options.aliases === 'object' && options.aliases) {
/**
* Tuple containing expansion key and pattern match.
*
* @const {PatternMatch | null} match
*/
const match: PatternMatch | null = patternMatch(
specifier,
options.aliases
)
if (match) {
aliased = resolveAliasTarget(options.aliases[match[0]], match[1])
if (typeof aliased === 'string') {
/**
* URL of aliased module.
*
* @const {URL} url
*/
const url: URL = new URL(aliased, options.cwd ?? cwd())
if (options.absolute) {
aliased = url.href
} else if (options.parent) {
aliased = toRelativeSpecifier(url, options.parent)
}
}
}
}
}
return aliased
}
/**
* Resolve a path alias target.
*
* @internal
*
* @param {unknown} target
* The path alias target to resolve
* @param {string | null | undefined} [patternMatch]
* Pattern match, replaces `*` in `target`
* @return {string | null}
* Matched alias
*/
function resolveAliasTarget(
target: unknown,
patternMatch?: string | null | undefined
): string | null {
if (typeof target === 'string') {
/**
* Matched alias.
*
* @var {string} match
*/
let match: string = typeof patternMatch === 'string'
? target.replace(chars.asterisk, patternMatch)
: target
if (!isRelativeSpecifier(match)) match = pathe.dot + pathe.sep + match
return match
}
if (Array.isArray<string | null | undefined>(target)) {
for (const targetValue of target) {
/**
* Resolved path alias.
*
* @const {string | null} resolved
*/
const resolved: string | null = resolveAliasTarget(
targetValue,
patternMatch
)
if (typeof resolved === 'string') return resolved
}
}
return null
}