11import { ModuleNode , HmrContext } from 'vite' ;
2- import { CompileData } from './utils/compile' ;
2+ import { Code , CompileData } from './utils/compile' ;
33import { log } from './utils/log' ;
44import { SvelteRequest } from './utils/id' ;
55import { VitePluginSvelteCache } from './utils/VitePluginSvelteCache' ;
6+ import { ResolvedOptions } from './utils/options' ;
67
78/**
89 * Vite-specific HMR handling
@@ -11,34 +12,33 @@ export async function handleHotUpdate(
1112 compileSvelte : Function ,
1213 ctx : HmrContext ,
1314 svelteRequest : SvelteRequest ,
14- cache : VitePluginSvelteCache
15+ cache : VitePluginSvelteCache ,
16+ options : Partial < ResolvedOptions >
1517) : Promise < ModuleNode [ ] | void > {
1618 const { read, server } = ctx ;
17- const cachedCompileData = cache . getCompileData ( svelteRequest , false ) ;
18- if ( ! cachedCompileData ) {
19+
20+ const cachedJS = cache . getJS ( svelteRequest ) ;
21+ if ( ! cachedJS ) {
1922 // file hasn't been requested yet (e.g. async component)
2023 log . debug ( `handleHotUpdate first call ${ svelteRequest . id } ` ) ;
2124 return ;
2225 }
26+ const cachedCss = cache . getCSS ( svelteRequest ) ;
2327
2428 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 ) ;
3131
3232 const affectedModules = new Set < ModuleNode | undefined > ( ) ;
3333
3434 const cssModule = server . moduleGraph . getModuleById ( svelteRequest . cssId ) ;
3535 const mainModule = server . moduleGraph . getModuleById ( svelteRequest . id ) ;
36- if ( cssModule && cssChanged ( cachedCompileData , compileData ) ) {
36+ if ( cssModule && cssChanged ( cachedCss , compileData . compiled . css ) ) {
3737 log . debug ( 'handleHotUpdate css changed' ) ;
3838 affectedModules . add ( cssModule ) ;
3939 }
4040
41- if ( mainModule && jsChanged ( cachedCompileData , compileData ) ) {
41+ if ( mainModule && jsChanged ( cachedJS , compileData . compiled . js , svelteRequest . filename ) ) {
4242 log . debug ( 'handleHotUpdate js changed' ) ;
4343 affectedModules . add ( mainModule ) ;
4444 }
@@ -56,27 +56,27 @@ export async function handleHotUpdate(
5656 return result ;
5757}
5858
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 ) ;
6161}
6262
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 ;
6666 const isStrictEqual = isCodeEqual ( prevJs , nextJs ) ;
6767 if ( isStrictEqual ) {
6868 return false ;
6969 }
7070 const isLooseEqual = isCodeEqual ( normalizeJsCode ( prevJs ) , normalizeJsCode ( nextJs ) ) ;
7171 if ( ! isStrictEqual && isLooseEqual ) {
7272 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`
7474 ) ;
7575 }
7676 return ! isLooseEqual ;
7777}
7878
79- function isCodeEqual ( prev : string , next : string ) : boolean {
79+ function isCodeEqual ( prev ? : string , next ? : string ) : boolean {
8080 if ( ! prev && ! next ) {
8181 return true ;
8282 }
@@ -93,7 +93,7 @@ function isCodeEqual(prev: string, next: string): boolean {
9393 * 2) ... maybe more (or less) in the future
9494 * @param code
9595 */
96- function normalizeJsCode ( code : string ) : string {
96+ function normalizeJsCode ( code ? : string ) : string | undefined {
9797 if ( ! code ) {
9898 return code ;
9999 }
0 commit comments