1
1
import { ModuleNode , HmrContext } from 'vite' ;
2
- import { CompileData } from './utils/compile' ;
2
+ import { Code , CompileData } from './utils/compile' ;
3
3
import { log } from './utils/log' ;
4
4
import { SvelteRequest } from './utils/id' ;
5
5
import { VitePluginSvelteCache } from './utils/VitePluginSvelteCache' ;
6
+ import { ResolvedOptions } from './utils/options' ;
6
7
7
8
/**
8
9
* Vite-specific HMR handling
@@ -11,34 +12,33 @@ export async function handleHotUpdate(
11
12
compileSvelte : Function ,
12
13
ctx : HmrContext ,
13
14
svelteRequest : SvelteRequest ,
14
- cache : VitePluginSvelteCache
15
+ cache : VitePluginSvelteCache ,
16
+ options : Partial < ResolvedOptions >
15
17
) : Promise < ModuleNode [ ] | void > {
16
18
const { read, server } = ctx ;
17
- const cachedCompileData = cache . getCompileData ( svelteRequest , false ) ;
18
- if ( ! cachedCompileData ) {
19
+
20
+ const cachedJS = cache . getJS ( svelteRequest ) ;
21
+ if ( ! cachedJS ) {
19
22
// file hasn't been requested yet (e.g. async component)
20
23
log . debug ( `handleHotUpdate first call ${ svelteRequest . id } ` ) ;
21
24
return ;
22
25
}
26
+ const cachedCss = cache . getCSS ( svelteRequest ) ;
23
27
24
28
const content = await read ( ) ;
25
- const compileData : CompileData = await compileSvelte (
26
- svelteRequest ,
27
- content ,
28
- cachedCompileData . options
29
- ) ;
30
- cache . setCompileData ( compileData ) ;
29
+ const compileData : CompileData = await compileSvelte ( svelteRequest , content , options ) ;
30
+ cache . update ( compileData ) ;
31
31
32
32
const affectedModules = new Set < ModuleNode | undefined > ( ) ;
33
33
34
34
const cssModule = server . moduleGraph . getModuleById ( svelteRequest . cssId ) ;
35
35
const mainModule = server . moduleGraph . getModuleById ( svelteRequest . id ) ;
36
- if ( cssModule && cssChanged ( cachedCompileData , compileData ) ) {
36
+ if ( cssModule && cssChanged ( cachedCss , compileData . compiled . css ) ) {
37
37
log . debug ( 'handleHotUpdate css changed' ) ;
38
38
affectedModules . add ( cssModule ) ;
39
39
}
40
40
41
- if ( mainModule && jsChanged ( cachedCompileData , compileData ) ) {
41
+ if ( mainModule && jsChanged ( cachedJS , compileData . compiled . js , svelteRequest . filename ) ) {
42
42
log . debug ( 'handleHotUpdate js changed' ) ;
43
43
affectedModules . add ( mainModule ) ;
44
44
}
@@ -56,27 +56,27 @@ export async function handleHotUpdate(
56
56
return result ;
57
57
}
58
58
59
- function cssChanged ( prev : CompileData , next : CompileData ) : boolean {
60
- return ! isCodeEqual ( prev . compiled . css ?. code , next . compiled . css ?. code ) ;
59
+ function cssChanged ( prev ?: Code , next ?: Code ) : boolean {
60
+ return ! isCodeEqual ( prev ?. code , next ?. code ) ;
61
61
}
62
62
63
- function jsChanged ( prev : CompileData , next : CompileData ) : boolean {
64
- const prevJs = prev . compiled . js . code ;
65
- const nextJs = next . compiled . js . code ;
63
+ function jsChanged ( prev ?: Code , next ?: Code , filename ?: string ) : boolean {
64
+ const prevJs = prev ? .code ;
65
+ const nextJs = next ? .code ;
66
66
const isStrictEqual = isCodeEqual ( prevJs , nextJs ) ;
67
67
if ( isStrictEqual ) {
68
68
return false ;
69
69
}
70
70
const isLooseEqual = isCodeEqual ( normalizeJsCode ( prevJs ) , normalizeJsCode ( nextJs ) ) ;
71
71
if ( ! isStrictEqual && isLooseEqual ) {
72
72
log . warn (
73
- `ignoring compiler output js change for ${ next . filename } as it is equal to previous output after normalization`
73
+ `ignoring compiler output js change for ${ filename } as it is equal to previous output after normalization`
74
74
) ;
75
75
}
76
76
return ! isLooseEqual ;
77
77
}
78
78
79
- function isCodeEqual ( prev : string , next : string ) : boolean {
79
+ function isCodeEqual ( prev ? : string , next ? : string ) : boolean {
80
80
if ( ! prev && ! next ) {
81
81
return true ;
82
82
}
@@ -93,7 +93,7 @@ function isCodeEqual(prev: string, next: string): boolean {
93
93
* 2) ... maybe more (or less) in the future
94
94
* @param code
95
95
*/
96
- function normalizeJsCode ( code : string ) : string {
96
+ function normalizeJsCode ( code ? : string ) : string | undefined {
97
97
if ( ! code ) {
98
98
return code ;
99
99
}
0 commit comments