From: daiwei Date: Wed, 13 Aug 2025 13:07:05 +0000 (+0800) Subject: refactor: disable fragment anchor generation for vapor mode X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b90ade0b98c7b870e3bfbb866ee9a488d04a60f1;p=thirdparty%2Fvuejs%2Fcore.git refactor: disable fragment anchor generation for vapor mode --- diff --git a/packages/compiler-ssr/__tests__/ssrVaporAnchors.spec.ts b/packages/compiler-ssr/__tests__/ssrVaporAnchors.spec.ts index 50657f3734..46b2175802 100644 --- a/packages/compiler-ssr/__tests__/ssrVaporAnchors.spec.ts +++ b/packages/compiler-ssr/__tests__/ssrVaporAnchors.spec.ts @@ -395,7 +395,7 @@ describe('insertion anchors', () => { if (_ctx.foo) { _push(\`\`) if (_ctx.depth < 5) { - _push(\` foo \`) + _push(\` foo \`) _push(\`\`) } else { _push(\`\`) @@ -436,11 +436,11 @@ describe('insertion anchors', () => { vapor: true, }), ).toMatchInlineSnapshot(` - "\`
\`) + "\`
\`) _ssrRenderList(_ctx.items, (item) => { _push(\`\`) }) - _push(\`
\`" + _push(\`
\`" `) }) @@ -459,11 +459,11 @@ describe('insertion anchors', () => { _ssrRenderVNode(_push, _createVNode(_resolveDynamicComponent('div'), null, { default: _withCtx((_, _push, _parent, _scopeId) => { if (_push) { - _push(\`\`) + _push(\`\`) _ssrRenderList(_ctx.items, (item) => { _push(\`\`) }) - _push(\`\`) + _push(\`\`) } else { return [ _createVNode("div", null, [ @@ -719,7 +719,6 @@ describe('block anchors', () => { _ssrRenderVNode(_push, _createVNode(_resolveDynamicComponent(_ctx.tag), null, { default: _withCtx((_, _push, _parent, _scopeId) => { if (_push) { - _push(\`\`) _ssrRenderList(_ctx.items, (item) => { _push(\` { _ssrInterpolate(item) }\`) }) - _push(\`\`) + _push(\`\`) } else { return [ (_openBlock(true), _createBlock(_Fragment, null, _renderList(_ctx.items, (item) => { diff --git a/packages/compiler-ssr/src/ssrCodegenTransform.ts b/packages/compiler-ssr/src/ssrCodegenTransform.ts index 79a3738df3..6f94175c4c 100644 --- a/packages/compiler-ssr/src/ssrCodegenTransform.ts +++ b/packages/compiler-ssr/src/ssrCodegenTransform.ts @@ -168,16 +168,14 @@ export function processChildren( disableNestedFragments = false, disableComment = false, ): void { - if (asFragment) { + const vapor = context.options.vapor + if (asFragment && !vapor) { context.pushStringPart(``) } const { children } = parent - if ( - context.options.vapor && - isElementWithChildren(parent as PlainElementNode) - ) { + if (vapor && isElementWithChildren(parent as PlainElementNode)) { processBlockNodeAnchor(children) } @@ -261,7 +259,7 @@ export function processChildren( return exhaustiveCheck } } - if (asFragment) { + if (asFragment && !vapor) { context.pushStringPart(``) } } diff --git a/packages/compiler-ssr/src/transforms/ssrVFor.ts b/packages/compiler-ssr/src/transforms/ssrVFor.ts index f7878d4f80..f786751f8b 100644 --- a/packages/compiler-ssr/src/transforms/ssrVFor.ts +++ b/packages/compiler-ssr/src/transforms/ssrVFor.ts @@ -37,8 +37,10 @@ export function ssrProcessFor( context, needFragmentWrapper, ) + + const vapor = context.options.vapor // v-for always renders a fragment unless explicitly disabled - if (!disableNestedFragments) { + if (!disableNestedFragments && !vapor) { context.pushStringPart(``) } context.pushStatement( @@ -47,12 +49,12 @@ export function ssrProcessFor( renderLoop, ]), ) - if (!disableNestedFragments) { + if (!disableNestedFragments && !vapor) { context.pushStringPart(``) } // anchor for vapor v-for fragment - if (context.options.vapor) { + if (vapor) { context.pushStringPart(``) } } diff --git a/packages/runtime-vapor/__tests__/hydration.spec.ts b/packages/runtime-vapor/__tests__/hydration.spec.ts index ba32291817..3c9a5d336c 100644 --- a/packages/runtime-vapor/__tests__/hydration.spec.ts +++ b/packages/runtime-vapor/__tests__/hydration.spec.ts @@ -148,21 +148,13 @@ describe('Vapor Mode hydration', () => { `) expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - " - foofoo - " - `, + `"foofoo"`, ) data.value = 'bar' await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - " - barbar - " - `, + `"barbar"`, ) }) @@ -186,21 +178,13 @@ describe('Vapor Mode hydration', () => { `) expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - " - fooAfooBfoo - " - `, + `"fooAfooBfoo"`, ) data.value = 'bar' await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - " - barAbarBbar - " - `, + `"barAbarBbar"`, ) }) @@ -265,21 +249,13 @@ describe('Vapor Mode hydration', () => { `) expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - " - Afoofoo - " - `, + `" Afoofoo"`, ) data.value = 'bar' await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - " - Abarbar - " - `, + `" Abarbar"`, ) }) @@ -381,9 +357,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
-foo- - +
foo
-foo-
" `, ) @@ -393,9 +367,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
bar
-bar- - +
bar
-bar-
" `, ) @@ -411,9 +383,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
-foo- - +
foo
-foo-
" `, ) @@ -423,9 +393,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
bar
-bar- - +
bar
-bar-
" `, ) @@ -444,11 +412,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
-
foo
-foo- -
- +
foo
-foo-
" `, ) @@ -458,11 +422,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
-
bar
-bar- -
- +
bar
-bar-
" `, ) @@ -798,9 +758,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
-foo - +
foo
-foo
" `, ) @@ -810,9 +768,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
bar
-bar - +
bar
-bar
" `, ) @@ -831,9 +787,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
-foo- - +
foo
-foo-
" `, ) @@ -843,9 +797,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
bar
-bar- - +
bar
-bar-
" `, ) @@ -865,9 +817,7 @@ describe('Vapor Mode hydration', () => { ` "
- -
foo
-foo- - +
foo
-foo-
" `, @@ -879,9 +829,7 @@ describe('Vapor Mode hydration', () => { ` "
- -
bar
-bar- - +
bar
-bar-
" `, @@ -906,12 +854,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
-foo - - -
foo
-foo - +
foo
-foo +
foo
-foo
" `, ) @@ -921,12 +865,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
bar
-bar - - -
bar
-bar - +
bar
-bar +
bar
-bar
" `, ) @@ -945,12 +885,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
-foo- - - -
foo
-foo- - +
foo
-foo- +
foo
-foo-
" `, ) @@ -960,12 +896,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
bar
-bar- - - -
bar
-bar- - +
bar
-bar- +
bar
-bar-
" `, ) @@ -985,12 +917,8 @@ describe('Vapor Mode hydration', () => { ` "
- -
foo
-foo- - - -
foo
-foo- - +
foo
-foo- +
foo
-foo-
" `, @@ -1002,12 +930,8 @@ describe('Vapor Mode hydration', () => { ` "
- -
bar
-bar- - - -
bar
-bar- - +
bar
-bar- +
bar
-bar-
" `, @@ -1027,12 +951,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - -
foo
-foo- -
foo
-foo- - - +
foo
-foo-
foo
-foo-
" `, ) @@ -1042,12 +961,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - -
bar
-bar- -
bar
-bar- - - +
bar
-bar-
bar
-bar-
" `, ) @@ -1072,13 +986,9 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
-foo - +
foo
-foo - -
foo
-foo - +
foo
-foo
" `, ) @@ -1088,13 +998,9 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
bar
-bar - +
bar
-bar - -
bar
-bar - +
bar
-bar
" `, ) @@ -1119,13 +1025,9 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
-foo - +
foo
-foo foo - -
foo
-foo - +
foo
-foo
" `, ) @@ -1135,13 +1037,9 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
bar
-bar - +
bar
-bar bar - -
bar
-bar - +
bar
-bar
" `, ) @@ -1725,30 +1623,20 @@ describe('Vapor Mode hydration', () => { data, ) expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - "
-
true
-true- -
" - `, + `"
true
-true-
"`, ) data.value = false await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - "
- -
" - `, + `"
"`, ) data.value = true await nextTick() - expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(` - "
- -
true
-true-
" - `) + expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( + `"
true
-true-
"`, + ) }) test('on fragment component with insertion anchor', async () => { @@ -1769,9 +1657,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
true
-true- - +
true
-true-
" `, ) @@ -1781,9 +1667,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - - +
" `, ) @@ -1792,9 +1676,7 @@ describe('Vapor Mode hydration', () => { await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(` "
- - -
true
-true- +
true
-true-
" `) }) @@ -1818,12 +1700,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
true
-true- - - -
true
-true- - +
true
-true- +
true
-true-
" `, ) @@ -1833,12 +1711,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - - - - - + +
" `, ) @@ -1847,12 +1721,8 @@ describe('Vapor Mode hydration', () => { await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(` "
- - -
true
-true- - - -
true
-true- +
true
-true- +
true
-true-
" `) }) @@ -1910,21 +1780,13 @@ describe('Vapor Mode hydration', () => { ref(['a', 'b', 'c']), ) expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - "
- abc -
" - `, + `"
abc
"`, ) data.value.push('d') await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - "
- abc - d
" - `, + `"
abcd
"`, ) }) @@ -1943,9 +1805,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - abc - + abc
" `, ) @@ -1955,9 +1815,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - abc - d + abcd
" `, ) @@ -1967,9 +1825,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - bc - d + bcd
" `, ) @@ -1991,12 +1847,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - abc - - - abc - + abc + abc
" `, ) @@ -2006,12 +1858,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - abc - d - - abc - d + abcd + abcd
" `, ) @@ -2021,12 +1869,8 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- - c - d - - c - d + cd + cd
" `, ) @@ -2046,21 +1890,13 @@ describe('Vapor Mode hydration', () => { ) expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - "
-
comp
comp
comp
-
" - `, + `"
comp
comp
comp
"`, ) data.value.push('d') await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - "
-
comp
comp
comp
-
comp
" - `, + `"
comp
comp
comp
comp
"`, ) }) @@ -2081,14 +1917,12 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- a b c - -
" + " `, ) @@ -2097,14 +1931,12 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- a b c - - d
" + d" `, ) }) @@ -2122,29 +1954,13 @@ describe('Vapor Mode hydration', () => { ref(['a', 'b', 'c']), ) expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - "
- -
foo
-bar- -
foo
-bar- -
foo
-bar- - -
" - `, + `"
foo
-bar-
foo
-bar-
foo
-bar-
"`, ) data.value.push('d') await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( - ` - "
- -
foo
-bar- -
foo
-bar- -
foo
-bar- - -
foo
-bar-
" - `, + `"
foo
-bar-
foo
-bar-
foo
-bar-
foo
-bar-
"`, ) }) }) @@ -2196,12 +2012,10 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` " - foo - - " + " `, ) @@ -2210,12 +2024,10 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` " - bar - - " + " `, ) }) @@ -2281,9 +2093,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` " - - abc - + abc " `, ) @@ -2293,9 +2103,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` " - - " `, ) @@ -2305,9 +2113,7 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` " - - abc" `, ) @@ -2367,11 +2173,9 @@ describe('Vapor Mode hydration', () => { ) expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` - " -
+ "
foo -
- " +
" `, ) @@ -2379,11 +2183,9 @@ describe('Vapor Mode hydration', () => { await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` - " -
+ "
bar -
- " +
" `, ) }) @@ -2447,11 +2249,9 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` - " - foo + "foo foo - hi - " + hi" `, ) @@ -2459,11 +2259,9 @@ describe('Vapor Mode hydration', () => { await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` - " - foo + "foo foo - bar - " + bar" `, ) }) @@ -2639,15 +2437,11 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
foo
bar - +
foo
bar foo - -
foo
bar - +
foo
bar
" `, ) @@ -2658,15 +2452,11 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` "
- -
hello
vapor - +
hello
vapor hello - -
hello
vapor - +
hello
vapor
" `, ) @@ -2696,11 +2486,9 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` - " -
foo
+ "
foo
foo -
foo
- " +
foo
" `, ) @@ -2708,22 +2496,18 @@ describe('Vapor Mode hydration', () => { await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` - " - + " foo - - " + " `, ) data.show = true await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(` - " -
foo
+ "
foo
foo -
foo
- " +
foo
" `) }) @@ -2751,15 +2535,9 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` - " - -
a
b
c
- + "
a
b
c
foo - -
a
b
c
- - " +
a
b
c
" `, ) @@ -2767,15 +2545,9 @@ describe('Vapor Mode hydration', () => { await nextTick() expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` - " - -
a
b
c
-
d
+ "
a
b
c
d
foo - -
a
b
c
-
d
- " +
a
b
c
d
" `, ) }) @@ -2804,12 +2576,10 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` " - foo bar - - " + " `, ) @@ -2819,12 +2589,10 @@ describe('Vapor Mode hydration', () => { expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot( ` " - hello vapor - - " + " `, ) }) diff --git a/packages/runtime-vapor/src/apiCreateFor.ts b/packages/runtime-vapor/src/apiCreateFor.ts index 09a3d2fc51..a34bc8b3ab 100644 --- a/packages/runtime-vapor/src/apiCreateFor.ts +++ b/packages/runtime-vapor/src/apiCreateFor.ts @@ -135,10 +135,10 @@ export const createFor = ( } if (isHydrating) { - parentAnchor = - locateVaporFragmentAnchor(currentHydrationNode!, FOR_ANCHOR_LABEL) || - // fallback to the fragment end anchor if in ssr slots vnode fallback - locateVaporFragmentAnchor(currentHydrationNode!, ']')! + parentAnchor = locateVaporFragmentAnchor( + currentHydrationNode!, + FOR_ANCHOR_LABEL, + )! if (__DEV__ && !parentAnchor) { // this should not happen throw new Error(`v-for fragment anchor node was not found.`) diff --git a/packages/runtime-vapor/src/vdomInterop.ts b/packages/runtime-vapor/src/vdomInterop.ts index b60875873d..74982b4d57 100644 --- a/packages/runtime-vapor/src/vdomInterop.ts +++ b/packages/runtime-vapor/src/vdomInterop.ts @@ -43,6 +43,7 @@ import { import { type Block, type VaporTransitionHooks, insert, remove } from './block' import { EMPTY_OBJ, + SLOT_ANCHOR_LABEL, extend, isArray, isFunction, @@ -195,7 +196,7 @@ const vaporInteropImpl: Omit< vnode.vb = slot(new Proxy(propsRef, vaporSlotPropsProxyHandler)) vnode.el = vnode.anchor = locateVaporFragmentAnchor( currentHydrationNode!, - 'slot', + SLOT_ANCHOR_LABEL, ) }) return _next(node)