-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathfilepath.ts
More file actions
74 lines (66 loc) · 1.8 KB
/
Copy pathfilepath.ts
File metadata and controls
74 lines (66 loc) · 1.8 KB
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
import { relative } from 'pathe'
import { makeCachedFunction } from './cache'
export function isNodeModulePath(path: string) {
return !!path.match(/[/\\]node_modules[/\\]/) || isPackageName(path)
}
export function isPackageName(name: string) {
return name[0] === '#' || !!name.match(/^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/)
}
export function getModuleNameFromPath(path: string) {
if (isPackageName(path))
return path
const match = path.replace(/\\/g, '/').match(/.*\/node_modules\/(.*)$/)?.[1]
if (!match)
return undefined
if (match.startsWith('@'))
return match.split('/').slice(0, 2).join('/')
return match.split('/')[0]
}
function getModuleSubpathFromPath(path: string) {
const match = path.match(/.*\/node_modules\/(.*)$/)?.[1]
if (!match)
return undefined
return match
}
export function isBuiltInModule(name: string | undefined) {
if (!name)
return
return ['nuxt', '#app', '#head', 'vue'].includes(name)
}
export const parseReadablePath = makeCachedFunction((path: string, root: string) => {
path = path
.replace(/%2F/g, '/')
.replace(/\\/g, '/')
if (isPackageName(path)) {
return {
moduleName: path,
path,
}
}
if (path.match(/^\w+:/)) {
return {
moduleName: path,
path,
}
}
const moduleName = getModuleNameFromPath(path)
const subpath = getModuleSubpathFromPath(path)
if (moduleName && subpath) {
return {
moduleName,
path: subpath,
}
}
// Workaround https://github.com/unjs/pathe/issues/113
try {
let result = relative(root, path)
if (!result.startsWith('./') && !result.startsWith('../'))
result = `./${result}`
if (result.startsWith('./.nuxt/'))
result = `#build${result.slice(7)}`
return { path: result }
}
catch {
return { path }
}
})