fix(compiler-ssr): only inject fallthrough attrs for root transition/keep-alive
This commit is contained in:
parent
c03459b9b6
commit
c65b805ef1
@ -290,6 +290,25 @@ describe('ssr: components', () => {
|
|||||||
}"
|
}"
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
// should inject attrs if root with coomments
|
||||||
|
expect(compile(`<!--root--><transition><div/></transition>`).code)
|
||||||
|
.toMatchInlineSnapshot(`
|
||||||
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"vue/server-renderer\\")
|
||||||
|
|
||||||
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
||||||
|
_push(\`<!--[--><!--root--><div\${_ssrRenderAttrs(_attrs)}></div><!--]-->\`)
|
||||||
|
}"
|
||||||
|
`)
|
||||||
|
|
||||||
|
// should not inject attrs if not root
|
||||||
|
expect(compile(`<div/><transition><div/></transition>`).code)
|
||||||
|
.toMatchInlineSnapshot(`
|
||||||
|
"
|
||||||
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
||||||
|
_push(\`<!--[--><div></div><div></div><!--]-->\`)
|
||||||
|
}"
|
||||||
|
`)
|
||||||
|
|
||||||
expect(compile(`<keep-alive><foo/></keep-alive>`).code)
|
expect(compile(`<keep-alive><foo/></keep-alive>`).code)
|
||||||
.toMatchInlineSnapshot(`
|
.toMatchInlineSnapshot(`
|
||||||
"const { resolveComponent: _resolveComponent } = require(\\"vue\\")
|
"const { resolveComponent: _resolveComponent } = require(\\"vue\\")
|
||||||
|
@ -11,8 +11,11 @@ import {
|
|||||||
isBuiltInType
|
isBuiltInType
|
||||||
} from '@vue/compiler-dom'
|
} from '@vue/compiler-dom'
|
||||||
|
|
||||||
|
const filterChild = (node: ParentNode) =>
|
||||||
|
node.children.filter(n => n.type !== NodeTypes.COMMENT)
|
||||||
|
|
||||||
const hasSingleChild = (node: ParentNode): boolean =>
|
const hasSingleChild = (node: ParentNode): boolean =>
|
||||||
node.children.filter(n => n.type !== NodeTypes.COMMENT).length === 1
|
filterChild(node).length === 1
|
||||||
|
|
||||||
export const ssrInjectFallthroughAttrs: NodeTransform = (node, context) => {
|
export const ssrInjectFallthroughAttrs: NodeTransform = (node, context) => {
|
||||||
// _attrs is provided as a function argument.
|
// _attrs is provided as a function argument.
|
||||||
@ -28,11 +31,14 @@ export const ssrInjectFallthroughAttrs: NodeTransform = (node, context) => {
|
|||||||
(isBuiltInType(node.tag, 'Transition') ||
|
(isBuiltInType(node.tag, 'Transition') ||
|
||||||
isBuiltInType(node.tag, 'KeepAlive'))
|
isBuiltInType(node.tag, 'KeepAlive'))
|
||||||
) {
|
) {
|
||||||
|
const rootChildren = filterChild(context.root)
|
||||||
|
if (rootChildren.length === 1 && rootChildren[0] === node) {
|
||||||
if (hasSingleChild(node)) {
|
if (hasSingleChild(node)) {
|
||||||
injectFallthroughAttrs(node.children[0])
|
injectFallthroughAttrs(node.children[0])
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const parent = context.parent
|
const parent = context.parent
|
||||||
if (!parent || parent.type !== NodeTypes.ROOT) {
|
if (!parent || parent.type !== NodeTypes.ROOT) {
|
||||||
|
Loading…
Reference in New Issue
Block a user