/
descriptorCache.ts
51 lines (43 loc) · 1.42 KB
/
descriptorCache.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import path from 'path'
import slash from 'slash'
import hash from 'hash-sum'
import { parse, SFCDescriptor } from '@vue/compiler-sfc'
const cache = new Map<string, SFCDescriptor>()
const prevCache = new Map<string, SFCDescriptor | undefined>()
export function createDescriptor(
filename: string,
source: string,
root: string,
isProduction: boolean | undefined
) {
const { descriptor, errors } = parse(source, {
filename,
sourceMap: true
})
// ensure the path is normalized in a way that is consistent inside
// project (relative to root) and on different systems.
const normalizedPath = slash(path.normalize(path.relative(root, filename)))
descriptor.id = hash(normalizedPath + (isProduction ? source : ''))
cache.set(filename, descriptor)
return { descriptor, errors }
}
export function getPrevDescriptor(filename: string) {
return prevCache.get(filename)
}
export function setPrevDescriptor(filename: string, entry: SFCDescriptor) {
prevCache.set(filename, entry)
}
export function getDescriptor(filename: string, errorOnMissing = true) {
if (cache.has(filename)) {
return cache.get(filename)!
}
if (errorOnMissing) {
throw new Error(
`${filename} has no corresponding SFC entry in the cache. ` +
`This is a @vitejs/plugin-vue internal error, please open an issue.`
)
}
}
export function setDescriptor(filename: string, entry: SFCDescriptor) {
cache.set(filename, entry)
}