Просмотр исходного кода

fix(hmr): force full diff on HMR

Evan You 6 лет назад
Родитель
Сommit
d6acb9c073
1 измененных файлов с 20 добавлено и 6 удалено
  1. 20 6
      packages/runtime-core/src/renderer.ts

+ 20 - 6
packages/runtime-core/src/renderer.ts

@@ -55,6 +55,8 @@ import { ErrorCodes, callWithErrorHandling } from './errorHandling'
 import { KeepAliveSink, isKeepAlive } from './components/KeepAlive'
 import { KeepAliveSink, isKeepAlive } from './components/KeepAlive'
 import { registerHMR, unregisterHMR } from './hmr'
 import { registerHMR, unregisterHMR } from './hmr'
 
 
+const __HMR__ = __BUNDLER__ && __DEV__
+
 export interface RendererOptions<HostNode = any, HostElement = any> {
 export interface RendererOptions<HostNode = any, HostElement = any> {
   patchProp(
   patchProp(
     el: HostElement,
     el: HostElement,
@@ -442,7 +444,7 @@ export function createRenderer<
     optimized: boolean
     optimized: boolean
   ) {
   ) {
     const el = (n2.el = n1.el) as HostElement
     const el = (n2.el = n1.el) as HostElement
-    const { patchFlag, dynamicChildren } = n2
+    let { patchFlag, dynamicChildren } = n2
     const oldProps = (n1 && n1.props) || EMPTY_OBJ
     const oldProps = (n1 && n1.props) || EMPTY_OBJ
     const newProps = n2.props || EMPTY_OBJ
     const newProps = n2.props || EMPTY_OBJ
 
 
@@ -450,6 +452,12 @@ export function createRenderer<
       invokeDirectiveHook(newProps.onVnodeBeforeUpdate, parentComponent, n2, n1)
       invokeDirectiveHook(newProps.onVnodeBeforeUpdate, parentComponent, n2, n1)
     }
     }
 
 
+    if (__HMR__ && parentComponent && parentComponent.renderUpdated) {
+      // HMR updated, force full diff
+      patchFlag = 0
+      optimized = false
+    }
+
     if (patchFlag > 0) {
     if (patchFlag > 0) {
       // the presence of a patchFlag means this element's render code was
       // the presence of a patchFlag means this element's render code was
       // generated by the compiler and can take the fast path.
       // generated by the compiler and can take the fast path.
@@ -654,10 +662,18 @@ export function createRenderer<
     const fragmentEndAnchor = (n2.anchor = n1
     const fragmentEndAnchor = (n2.anchor = n1
       ? n1.anchor
       ? n1.anchor
       : hostCreateComment(showID ? `fragment-${devFragmentID}-end` : ''))!
       : hostCreateComment(showID ? `fragment-${devFragmentID}-end` : ''))!
-    const { patchFlag } = n2
+
+    let { patchFlag } = n2
     if (patchFlag > 0) {
     if (patchFlag > 0) {
       optimized = true
       optimized = true
     }
     }
+
+    if (__HMR__ && parentComponent && parentComponent.renderUpdated) {
+      // HMR updated, force full diff
+      patchFlag = 0
+      optimized = false
+    }
+
     if (n1 == null) {
     if (n1 == null) {
       if (showID) {
       if (showID) {
         devFragmentID++
         devFragmentID++
@@ -879,8 +895,7 @@ export function createRenderer<
       parentComponent
       parentComponent
     ))
     ))
 
 
-    // HMR
-    if (__BUNDLER__ && __DEV__ && instance.type.__hmrId != null) {
+    if (__HMR__ && instance.type.__hmrId != null) {
       registerHMR(instance)
       registerHMR(instance)
     }
     }
 
 
@@ -1572,8 +1587,7 @@ export function createRenderer<
     parentSuspense: HostSuspenseBoundary | null,
     parentSuspense: HostSuspenseBoundary | null,
     doRemove?: boolean
     doRemove?: boolean
   ) {
   ) {
-    // HMR
-    if (__BUNDLER__ && __DEV__ && instance.type.__hmrId != null) {
+    if (__HMR__ && instance.type.__hmrId != null) {
       unregisterHMR(instance)
       unregisterHMR(instance)
     }
     }