From: daiwei Date: Tue, 12 Aug 2025 09:46:52 +0000 (+0800) Subject: fix: add anchors for template v-if node in vnode-based slot X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=18bfe30456787ffe3c600770d69063a78e9a1475;p=thirdparty%2Fvuejs%2Fcore.git fix: add anchors for template v-if node in vnode-based slot --- diff --git a/packages/compiler-core/src/ast.ts b/packages/compiler-core/src/ast.ts index dfcc1bef3c..1ecab369a3 100644 --- a/packages/compiler-core/src/ast.ts +++ b/packages/compiler-core/src/ast.ts @@ -180,6 +180,7 @@ export interface TemplateNode extends BaseElementNode { tagType: ElementTypes.TEMPLATE // TemplateNode is a container type that always gets compiled away codegenNode: undefined + anchor?: string } export interface TextNode extends Node { diff --git a/packages/compiler-ssr/__tests__/ssrVaporAnchors.spec.ts b/packages/compiler-ssr/__tests__/ssrVaporAnchors.spec.ts index f5747b5b32..a2aab28e74 100644 --- a/packages/compiler-ssr/__tests__/ssrVaporAnchors.spec.ts +++ b/packages/compiler-ssr/__tests__/ssrVaporAnchors.spec.ts @@ -351,6 +351,60 @@ describe('insertion anchors', () => { `) }) + test('prepend anchor with template v-if', () => { + expect( + getCompiledString( + ` +
+ +
+
+
`, + { vapor: true }, + ), + ).toMatchInlineSnapshot(` + "\`\`) + _ssrRenderVNode(_push, _createVNode(_resolveDynamicComponent(_ctx.tag), null, { + default: _withCtx((_, _push, _parent, _scopeId) => { + if (_push) { + if (_ctx.foo) { + _push(\`\`) + if (_ctx.depth < 5) { + _push(\` foo \`) + _push(\`\`) + } else { + _push(\`\`) + } + _push(\`\`) + _push(\`\`) + } else { + _push(\`\`) + } + } else { + return [ + (_ctx.foo) + ? (_openBlock(), _createBlock("div", { key: 0 }, [ + _createCommentVNode("[p"), + (_ctx.depth < 5) + ? (_openBlock(), _createBlock(_Fragment, { key: 0 }, [ + _createTextVNode(" foo ") + ], 64 /* STABLE_FRAGMENT */)) + : _createCommentVNode("v-if", true), + _createCommentVNode("p]"), + _createVNode("div") + ])) + : _createCommentVNode("v-if", true) + ] + } + }), + _: 1 /* STABLE */ + }), _parent) + _push(\`\`" + `) + }) + test('prepend anchor with v-for', () => { expect( getCompiledString('
', { diff --git a/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts b/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts index 5137da0e28..beec63151c 100644 --- a/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts +++ b/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts @@ -417,7 +417,11 @@ function injectVaporInsertionAnchors( const { tagType, props } = child let anchor: string | undefined - if (tagType === ElementTypes.COMPONENT || tagType === ElementTypes.SLOT) { + if ( + tagType === ElementTypes.COMPONENT || + tagType === ElementTypes.SLOT || + tagType === ElementTypes.TEMPLATE + ) { anchor = child.anchor } else if (tagType === ElementTypes.ELEMENT) { let hasIf = false