Skip to content

Commit df28fa5

Browse files
committed
fix(compiler-vapor): adjust children generation order for hydration
1 parent 07fd7e4 commit df28fa5

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ export function render(_ctx) {
212212
}"
213213
`;
214214
215+
exports[`compile > execution order > with insertionState 1`] = `
216+
"import { resolveComponent as _resolveComponent, child as _child, setInsertionState as _setInsertionState, createSlot as _createSlot, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
217+
const t0 = _template("<div><div></div></div>", true)
218+
219+
export function render(_ctx) {
220+
const _component_Comp = _resolveComponent("Comp")
221+
const n3 = t0()
222+
const n1 = _child(n3)
223+
_setInsertionState(n1)
224+
const n0 = _createSlot("default", null)
225+
_setInsertionState(n3)
226+
const n2 = _createComponentWithFallback(_component_Comp)
227+
return n3
228+
}"
229+
`;
230+
215231
exports[`compile > execution order > with v-once 1`] = `
216232
"import { child as _child, next as _next, nthChild as _nthChild, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, template as _template } from 'vue';
217233
const t0 = _template("<div><span> </span> <br> </div>", true)

packages/compiler-vapor/__tests__/compile.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ describe('compile', () => {
247247
_setText(x0, _toDisplayString(_ctx.bar))`,
248248
)
249249
})
250+
250251
test('with v-once', () => {
251252
const code = compile(
252253
`<div>
@@ -261,5 +262,10 @@ describe('compile', () => {
261262
_setText(n2, " " + _toDisplayString(_ctx.baz))`,
262263
)
263264
})
265+
266+
test('with insertionState', () => {
267+
const code = compile(`<div><div><slot /></div><Comp/></div>`)
268+
expect(code).matchSnapshot()
269+
})
264270
})
265271
})

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformChildren.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ const t0 = _template("<div><div>x</div><div><span> </span></div><div><span> </sp
5757
export function render(_ctx) {
5858
const n3 = t0()
5959
const p0 = _next(_child(n3))
60-
const p1 = _next(p0)
61-
const p2 = _next(p1)
6260
const n0 = _child(p0)
61+
const p1 = _next(p0)
6362
const n1 = _child(p1)
63+
const p2 = _next(p1)
6464
const n2 = _child(p2)
6565
const x0 = _child(n0)
6666
const x1 = _child(n1)

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ const t2 = _template("<form></form>")
343343
344344
export function render(_ctx) {
345345
const n1 = t1()
346-
const n3 = t2()
347346
const n0 = t0()
347+
const n3 = t2()
348348
const n2 = t2()
349349
_insert(n0, n1)
350350
_insert(n2, n3)

packages/compiler-vapor/src/generators/block.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ export function genBlockContent(
6565
push(...genSelf(child, context))
6666
}
6767
for (const child of dynamic.children) {
68-
push(...genChildren(child, context, push, `n${child.id!}`))
68+
if (!child.hasDynamicChild) {
69+
push(...genChildren(child, context, push, `n${child.id!}`))
70+
}
6971
}
7072

7173
push(...genOperations(operation, context))

packages/compiler-vapor/src/generators/template.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function genSelf(
2424
context: CodegenContext,
2525
): CodeFragment[] {
2626
const [frag, push] = buildCodeFragment()
27-
const { id, template, operation } = dynamic
27+
const { id, template, operation, hasDynamicChild } = dynamic
2828

2929
if (id !== undefined && template !== undefined) {
3030
push(NEWLINE, `const n${id} = t${template}()`)
@@ -35,6 +35,10 @@ export function genSelf(
3535
push(...genOperationWithInsertionState(operation, context))
3636
}
3737

38+
if (hasDynamicChild) {
39+
push(...genChildren(dynamic, context, push, `n${id}`))
40+
}
41+
3842
return frag
3943
}
4044

@@ -50,7 +54,6 @@ export function genChildren(
5054

5155
let offset = 0
5256
let prev: [variable: string, elementIndex: number] | undefined
53-
const childrenToGen: [IRDynamicInfo, string][] = []
5457

5558
for (const [index, child] of children.entries()) {
5659
if (child.flags & DynamicFlag.NON_TEMPLATE) {
@@ -96,7 +99,7 @@ export function genChildren(
9699
}
97100
}
98101

99-
if (id === child.anchor) {
102+
if (id === child.anchor && !child.hasDynamicChild) {
100103
push(...genSelf(child, context))
101104
}
102105

@@ -105,13 +108,7 @@ export function genChildren(
105108
}
106109

107110
prev = [variable, elementIndex]
108-
childrenToGen.push([child, variable])
109-
}
110-
111-
if (childrenToGen.length) {
112-
for (const [child, from] of childrenToGen) {
113-
push(...genChildren(child, context, pushBlock, from))
114-
}
111+
push(...genChildren(child, context, pushBlock, variable))
115112
}
116113

117114
return frag

0 commit comments

Comments
 (0)