11import fs from 'fs'
2+ import _debug from 'debug'
23import { parse , SFCBlock } from '@vue/compiler-sfc'
34import { getDescriptor , setDescriptor } from './utils/descriptorCache'
45
6+ const debug = _debug ( 'vite:hmr' )
7+
58/**
69 * Vite-specific HMR handling
710 */
@@ -33,8 +36,10 @@ export async function handleHotUpdate(file: string, modules: any[]) {
3336 const filteredModules = [ ]
3437
3538 const reload = ( ) => {
36- console . log ( `[vue:reload] ${ file } ` )
37- return modules . filter ( ( m ) => / t y p e = s c r i p t / . test ( m . id ) )
39+ debug ( `[vue:reload] ${ file } ` )
40+ return modules . filter (
41+ ( m ) => ! / t y p e = / . test ( m . id ) || / t y p e = s c r i p t / . test ( m . id )
42+ )
3843 }
3944
4045 if (
@@ -52,16 +57,6 @@ export async function handleHotUpdate(file: string, modules: any[]) {
5257 const prevStyles = prevDescriptor . styles || [ ]
5358 const nextStyles = descriptor . styles || [ ]
5459
55- // css modules update causes a reload because the $style object is changed
56- // and it may be used in JS. It also needs to trigger a vue-style-update
57- // event so the client busts the sw cache.
58- if (
59- prevStyles . some ( ( s ) => s . module != null ) ||
60- nextStyles . some ( ( s ) => s . module != null )
61- ) {
62- return reload ( )
63- }
64-
6560 // force reload if CSS vars injection changed
6661 if ( descriptor . cssVars ) {
6762 if ( prevDescriptor . cssVars . join ( '' ) !== descriptor . cssVars . join ( '' ) ) {
@@ -76,12 +71,21 @@ export async function handleHotUpdate(file: string, modules: any[]) {
7671
7772 // only need to update styles if not reloading, since reload forces
7873 // style updates as well.
79- nextStyles . forEach ( ( _ , i ) => {
80- if ( ! prevStyles [ i ] || ! isEqualBlock ( prevStyles [ i ] , nextStyles [ i ] ) ) {
74+ for ( let i = 0 ; i < nextStyles . length ; i ++ ) {
75+ const prev = prevStyles [ i ]
76+ const next = nextStyles [ i ]
77+ if ( ! prev || ! isEqualBlock ( prev , next ) ) {
78+ // css modules update causes a reload because the $style object is changed
79+ // and it may be used in JS.
80+ // if (prev.module != null || next.module != null) {
81+ // return modules.filter(
82+ // (m) => !/type=/.test(m.id) || /type=script/.test(m.id)
83+ // )
84+ // }
8185 didUpdateStyle = true
8286 filteredModules . push ( modules . find ( ( m ) => m . id . includes ( `index=${ i } ` ) ) )
8387 }
84- } )
88+ }
8589
8690 const prevCustoms = prevDescriptor . customBlocks || [ ]
8791 const nextCustoms = descriptor . customBlocks || [ ]
@@ -108,7 +112,7 @@ export async function handleHotUpdate(file: string, modules: any[]) {
108112 updateType . push ( `style` )
109113 }
110114 if ( updateType . length ) {
111- console . log ( `[vue:update(${ updateType . join ( '&' ) } )] ${ file } ` )
115+ debug ( `[vue:update(${ updateType . join ( '&' ) } )] ${ file } ` )
112116 }
113117 return filteredModules
114118}
0 commit comments