From 24041b7ac1a22ca6c10bf2af81c9250af26bda34 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 19 Aug 2020 17:29:23 -0400 Subject: [PATCH] fix(compiler-core): generate NEED_PATCH flag for element with vnode hooks --- .../transforms/transformElement.spec.ts | 5 +++++ .../src/transforms/transformElement.ts | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts index 9b08c4c6..33cfe51d 100644 --- a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts @@ -780,6 +780,11 @@ describe('compiler: element transform', () => { expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_PATCH)) }) + test('NEED_PATCH (vnode hooks)', () => { + const { node } = parseWithBind(`
`) + expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_PATCH)) + }) + test('HYDRATE_EVENTS', () => { // ignore click events (has dedicated fast path) const { node } = parseWithElementTransform(`
`, { diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index 1d93a0af..8a6d39a3 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -25,7 +25,8 @@ import { PatchFlagNames, isSymbol, isOn, - isObject + isObject, + isReservedProp } from '@vue/shared' import { createCompilerError, ErrorCodes } from '../errors' import { @@ -281,22 +282,31 @@ export function buildProps( let hasStyleBinding = false let hasHydrationEventBinding = false let hasDynamicKeys = false + let hasVnodeHook = false const dynamicPropNames: string[] = [] const analyzePatchFlag = ({ key, value }: Property) => { if (isStaticExp(key)) { const name = key.content + const isEventHandler = isOn(name) if ( !isComponent && - isOn(name) && + isEventHandler && // omit the flag for click handlers because hydration gives click // dedicated fast path. name.toLowerCase() !== 'onclick' && // omit v-model handlers - name !== 'onUpdate:modelValue' + name !== 'onUpdate:modelValue' && + // omit onVnodeXXX hooks + !isReservedProp(name) ) { hasHydrationEventBinding = true } + + if (isEventHandler && isReservedProp(name)) { + hasVnodeHook = true + } + if ( value.type === NodeTypes.JS_CACHE_EXPRESSION || ((value.type === NodeTypes.SIMPLE_EXPRESSION || @@ -475,7 +485,7 @@ export function buildProps( } if ( (patchFlag === 0 || patchFlag === PatchFlags.HYDRATE_EVENTS) && - (hasRef || runtimeDirectives.length > 0) + (hasRef || hasVnodeHook || runtimeDirectives.length > 0) ) { patchFlag |= PatchFlags.NEED_PATCH }