@@ -12,12 +12,14 @@ export interface SFCParseResult {
12
12
}
13
13
14
14
export const cache = new Map < string , SFCDescriptor > ( )
15
+ export const hmrCache = new Map < string , SFCDescriptor > ( )
15
16
const prevCache = new Map < string , SFCDescriptor | undefined > ( )
16
17
17
18
export function createDescriptor (
18
19
filename : string ,
19
20
source : string ,
20
21
{ root, isProduction, sourceMap, compiler } : ResolvedOptions ,
22
+ hmr = false ,
21
23
) : SFCParseResult {
22
24
const { descriptor, errors } = compiler . parse ( source , {
23
25
filename,
@@ -28,37 +30,41 @@ export function createDescriptor(
28
30
// project (relative to root) and on different systems.
29
31
const normalizedPath = slash ( path . normalize ( path . relative ( root , filename ) ) )
30
32
descriptor . id = getHash ( normalizedPath + ( isProduction ? source : '' ) )
31
-
32
- cache . set ( filename , descriptor )
33
+ ; ( hmr ? hmrCache : cache ) . set ( filename , descriptor )
33
34
return { descriptor, errors }
34
35
}
35
36
36
37
export function getPrevDescriptor ( filename : string ) : SFCDescriptor | undefined {
37
38
return prevCache . get ( filename )
38
39
}
39
40
40
- export function setPrevDescriptor (
41
- filename : string ,
42
- entry : SFCDescriptor ,
43
- ) : void {
44
- prevCache . set ( filename , entry )
41
+ export function invalidateDescriptor ( filename : string , hmr = false ) : void {
42
+ const _cache = hmr ? hmrCache : cache
43
+ const prev = _cache . get ( filename )
44
+ _cache . delete ( filename )
45
+ if ( prev ) {
46
+ prevCache . set ( filename , prev )
47
+ }
45
48
}
46
49
47
50
export function getDescriptor (
48
51
filename : string ,
49
52
options : ResolvedOptions ,
50
53
createIfNotFound = true ,
54
+ hmr = false ,
51
55
) : SFCDescriptor | undefined {
52
- if ( cache . has ( filename ) ) {
53
- return cache . get ( filename ) !
56
+ const _cache = hmr ? hmrCache : cache
57
+ if ( _cache . has ( filename ) ) {
58
+ return _cache . get ( filename ) !
54
59
}
55
60
if ( createIfNotFound ) {
56
61
const { descriptor, errors } = createDescriptor (
57
62
filename ,
58
63
fs . readFileSync ( filename , 'utf-8' ) ,
59
64
options ,
65
+ hmr ,
60
66
)
61
- if ( errors . length ) {
67
+ if ( errors . length && ! hmr ) {
62
68
throw errors [ 0 ]
63
69
}
64
70
return descriptor
0 commit comments