From: daiwei Date: Thu, 23 Jan 2025 10:11:16 +0000 (+0800) Subject: wip: inject attrs for skip node X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=172c16d13b021b4c07d3f22d3dff5dd0bbfc1eba;p=thirdparty%2Fvuejs%2Fcore.git wip: inject attrs for skip node --- diff --git a/packages/compiler-ssr/__tests__/ssrVSkip.spec.ts b/packages/compiler-ssr/__tests__/ssrVSkip.spec.ts index 086e957c0d..57f47d1d20 100644 --- a/packages/compiler-ssr/__tests__/ssrVSkip.spec.ts +++ b/packages/compiler-ssr/__tests__/ssrVSkip.spec.ts @@ -4,12 +4,13 @@ describe('ssr: v-skip', () => { test('basic', () => { expect(compile(`
`).code).toMatchInlineSnapshot(` "const { createCommentVNode: _createCommentVNode } = require("vue") + const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") return function ssrRender(_ctx, _push, _parent, _attrs) { if (_ctx.foo) { _createCommentVNode("v-skip", true) } else { - _push(\`
\`) + _push(\`
\`) } }" `) @@ -18,36 +19,38 @@ describe('ssr: v-skip', () => { test('with text children', () => { expect(compile(`
hello
`).code) .toMatchInlineSnapshot(` - " - return function ssrRender(_ctx, _push, _parent, _attrs) { - if (_ctx.foo) { - _push(\`hello\`) - } else { - _push(\`
hello
\`) - } - }" - `) + "const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _push(\`hello\`) + } else { + _push(\`hello\`) + } + }" + `) }) test('with element children', () => { expect(compile(`
`).code) .toMatchInlineSnapshot(` - " - return function ssrRender(_ctx, _push, _parent, _attrs) { - if (_ctx.foo) { - _push(\`\`) - } else { - _push(\`
\`) - } - }" - `) + "const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _push(\`\`) + } else { + _push(\`\`) + } + }" + `) }) test('with component children', () => { expect(compile(`
`).code) .toMatchInlineSnapshot(` "const { resolveComponent: _resolveComponent } = require("vue") - const { ssrRenderComponent: _ssrRenderComponent } = require("vue/server-renderer") + const { ssrRenderComponent: _ssrRenderComponent, ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") return function ssrRender(_ctx, _push, _parent, _attrs) { const _component_MyComponent = _resolveComponent("MyComponent") @@ -55,7 +58,7 @@ describe('ssr: v-skip', () => { if (_ctx.foo) { _push(_ssrRenderComponent(_component_MyComponent, null, null, _parent)) } else { - _push(\`
\`) + _push(\`\`) _push(_ssrRenderComponent(_component_MyComponent, null, null, _parent)) _push(\`
\`) } @@ -66,42 +69,44 @@ describe('ssr: v-skip', () => { test('with multiple children', () => { expect(compile(`
`).code) .toMatchInlineSnapshot(` - " - return function ssrRender(_ctx, _push, _parent, _attrs) { - if (_ctx.foo) { - _push(\`\`) - } else { - _push(\`
\`) - } - }" - `) + "const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") + + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _push(\`\`) + } else { + _push(\`\`) + } + }" + `) }) test('nested v-skip', () => { expect(compile(`
`).code) .toMatchInlineSnapshot(` - "const { createCommentVNode: _createCommentVNode } = require("vue") + "const { createCommentVNode: _createCommentVNode } = require("vue") + const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") - return function ssrRender(_ctx, _push, _parent, _attrs) { - if (_ctx.foo) { - _push(\`\`) - if (_ctx.bar) { - _createCommentVNode("v-skip", true) - } else { - _push(\`
\`) - } - _push(\`\`) - } else { - _push(\`
\`) - if (_ctx.bar) { - _createCommentVNode("v-skip", true) + return function ssrRender(_ctx, _push, _parent, _attrs) { + if (_ctx.foo) { + _push(\`\`) + if (_ctx.bar) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + _push(\`\`) } else { - _push(\`
\`) + _push(\`\`) + if (_ctx.bar) { + _createCommentVNode("v-skip", true) + } else { + _push(\`
\`) + } + _push(\`
\`) } - _push(\`
\`) - } - }" - `) + }" + `) }) test('v-if + v-skip', () => { @@ -126,15 +131,16 @@ describe('ssr: v-skip', () => { `) }) - test.todo('with key', () => { + test('with key', () => { expect(compile(`
`).code).toMatchInlineSnapshot(` - "const { createCommentVNode: _createCommentVNode } = require("vue") + "const { createCommentVNode: _createCommentVNode, mergeProps: _mergeProps } = require("vue") + const { ssrRenderAttrs: _ssrRenderAttrs } = require("vue/server-renderer") return function ssrRender(_ctx, _push, _parent, _attrs) { if (_ctx.ok) { _createCommentVNode("v-skip", true) } else { - _push(\`
\`) + _push(\`
\`) } }" `) @@ -198,7 +204,7 @@ describe('ssr: v-skip', () => { if (_ctx.foo) { _createCommentVNode("v-skip", true) } else { - _push(_ssrRenderComponent(_component_Comp, null, null, _parent)) + _push(_ssrRenderComponent(_component_Comp, _attrs, null, _parent)) } }" `) @@ -215,7 +221,7 @@ describe('ssr: v-skip', () => { if (_ctx.ok) { _push(\`foo\`) } else { - _push(_ssrRenderComponent(_component_Comp, null, { + _push(_ssrRenderComponent(_component_Comp, _attrs, { default: _withCtx((_, _push, _parent, _scopeId) => { if (_push) { _push(\`foo\`) @@ -250,7 +256,7 @@ describe('ssr: v-skip', () => { if (_ctx.ok) { _push(\`default\`) } else { - _push(_ssrRenderComponent(_component_Comp, null, { + _push(_ssrRenderComponent(_component_Comp, _attrs, { default: _withCtx((_, _push, _parent, _scopeId) => { if (_push) { _push(\`default\`) @@ -295,7 +301,7 @@ describe('ssr: v-skip', () => { if (_ctx.ok) { _push(\`
\`) } else { - _push(_ssrRenderComponent(_component_Comp, null, { + _push(_ssrRenderComponent(_component_Comp, _attrs, { foo: _withCtx((_, _push, _parent, _scopeId) => { if (_push) { _push(\`foo\`) @@ -341,7 +347,7 @@ describe('ssr: v-skip', () => { if (_ctx.ok) { _ssrRenderSlot(_ctx.$slots, "default", {}, null, _push, _parent, _scopeId) } else { - _ssrRenderVNode(_push, _createVNode(_resolveDynamicComponent(_ctx.Comp), null, { + _ssrRenderVNode(_push, _createVNode(_resolveDynamicComponent(_ctx.Comp), _attrs, { default: _withCtx((_, _push, _parent, _scopeId) => { if (_push) { _ssrRenderSlot(_ctx.$slots, "default", {}, null, _push, _parent, _scopeId) diff --git a/packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts b/packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts index b1aac0d74c..719c67aec8 100644 --- a/packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts +++ b/packages/compiler-ssr/src/transforms/ssrInjectFallthroughAttrs.ts @@ -67,6 +67,9 @@ export const ssrInjectFallthroughAttrs: NodeTransform = (node, context) => { } } injectFallthroughAttrs(node.children[0]) + } else if (node.type === NodeTypes.SKIP) { + const children = filterChild(node.alternate) + children.forEach(injectFallthroughAttrs) } else if (hasSingleChild(parent)) { injectFallthroughAttrs(node) }