-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.ts
30 lines (26 loc) · 1012 Bytes
/
utils.ts
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
import { basename } from 'pathe'
export function resolveAliases (_aliases: Record<string, string>) {
// Sort aliases from specific to general (ie. fs/promises before fs)
const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) =>
(b.split('/').length - a.split('/').length) || (b.length - a.length)
))
// Resolve alias values in relation to each other
for (const key in aliases) {
for (const alias in aliases) {
if (!['~', '@', '#'].includes(alias[0])) { continue }
if (alias === '@' && !aliases[key].startsWith('@/')) { continue } // Don't resolve @foo/bar
if (aliases[key].startsWith(alias)) {
aliases[key] = aliases[alias] + aliases[key].slice(alias.length)
}
}
}
return aliases
}
export function sortPaths (paths: string[]) {
return paths.sort((a, b) =>
(b.split('/').length - a.split('/').length) || (b.length - a.length)
)
}
export function filename (path: string) {
return basename(path).replace(/\.[^.]+$/, '')
}