Skip to content

Commit

Permalink
fix(compiler-core): should generate HYDRATE_EVENTS flag on dynamic co…
Browse files Browse the repository at this point in the history
…mponent that resolves to element

fix #5870
  • Loading branch information
yyx990803 committed May 17, 2022
1 parent f811dc2 commit 415091b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
Expand Up @@ -1053,6 +1053,21 @@ describe('compiler: element transform', () => {
genFlagText([PatchFlags.PROPS, PatchFlags.HYDRATE_EVENTS])
)
})

// #5870
test('HYDRATE_EVENTS on dynamic component', () => {
const { node } = parseWithElementTransform(
`<component :is="foo" @input="foo" />`,
{
directiveTransforms: {
on: transformOn
}
}
)
expect(node.patchFlag).toBe(
genFlagText([PatchFlags.PROPS, PatchFlags.HYDRATE_EVENTS])
)
})
})

describe('dynamic component', () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/compiler-core/src/transforms/transformElement.ts
Expand Up @@ -121,7 +121,13 @@ export const transformElement: NodeTransform = (node, context) => {

// props
if (props.length > 0) {
const propsBuildResult = buildProps(node, context)
const propsBuildResult = buildProps(
node,
context,
undefined,
isComponent,
isDynamicComponent
)
vnodeProps = propsBuildResult.props
patchFlag = propsBuildResult.patchFlag
dynamicPropNames = propsBuildResult.dynamicPropNames
Expand Down Expand Up @@ -380,6 +386,8 @@ export function buildProps(
node: ElementNode,
context: TransformContext,
props: ElementNode['props'] = node.props,
isComponent: boolean,
isDynamicComponent: boolean,
ssr = false
): {
props: PropsExpression | undefined
Expand All @@ -389,7 +397,6 @@ export function buildProps(
shouldUseBlock: boolean
} {
const { tag, loc: elementLoc, children } = node
const isComponent = node.tagType === ElementTypes.COMPONENT
let properties: ObjectExpression['properties'] = []
const mergeArgs: PropsExpression[] = []
const runtimeDirectives: DirectiveNode[] = []
Expand All @@ -411,8 +418,8 @@ export function buildProps(
const name = key.content
const isEventHandler = isOn(name)
if (
!isComponent &&
isEventHandler &&
(!isComponent || isDynamicComponent) &&
// omit the flag for click handlers because hydration gives click
// dedicated fast path.
name.toLowerCase() !== 'onclick' &&
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler-core/src/transforms/transformSlotOutlet.ts
Expand Up @@ -87,7 +87,13 @@ export function processSlotOutlet(
}

if (nonNameProps.length > 0) {
const { props, directives } = buildProps(node, context, nonNameProps)
const { props, directives } = buildProps(
node,
context,
nonNameProps,
false,
false
)
slotProps = props

if (directives.length) {
Expand Down
13 changes: 11 additions & 2 deletions packages/compiler-ssr/src/transforms/ssrTransformComponent.ts
Expand Up @@ -34,7 +34,8 @@ import {
TRANSITION_GROUP,
CREATE_VNODE,
CallExpression,
JSChildNode
JSChildNode,
RESOLVE_DYNAMIC_COMPONENT
} from '@vue/compiler-dom'
import { SSR_RENDER_COMPONENT, SSR_RENDER_VNODE } from '../runtimeHelpers'
import {
Expand Down Expand Up @@ -83,6 +84,8 @@ export const ssrTransformComponent: NodeTransform = (node, context) => {
}

const component = resolveComponentType(node, context, true /* ssr */)
const isDynamicComponent =
isObject(component) && component.callee === RESOLVE_DYNAMIC_COMPONENT
componentTypeMap.set(node, component)

if (isSymbol(component)) {
Expand Down Expand Up @@ -116,7 +119,13 @@ export const ssrTransformComponent: NodeTransform = (node, context) => {
if (node.props.length) {
// note we are not passing ssr: true here because for components, v-on
// handlers should still be passed
const { props, directives } = buildProps(node, context)
const { props, directives } = buildProps(
node,
context,
undefined,
true,
isDynamicComponent
)
if (props || directives.length) {
propsExp = buildSSRProps(props, directives, context)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-ssr/src/transforms/ssrTransformElement.ts
Expand Up @@ -89,6 +89,8 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
node,
context,
node.props,
false /* isComponent */,
false /* isDynamicComponent */,
true /* ssr */
)
if (props || directives.length) {
Expand Down

0 comments on commit 415091b

Please sign in to comment.