Skip to content

Commit

Permalink
fix(compiler-ssr): fix node clone edge case caused by AST reuse (#9983)
Browse files Browse the repository at this point in the history
close #9981
  • Loading branch information
edison1105 authored Jan 4, 2024
1 parent d2d8955 commit 7dbdb3e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
25 changes: 25 additions & 0 deletions packages/compiler-sfc/__tests__/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,31 @@ test('prefixing edge case for reused AST', () => {
expect(code).not.toMatch(`_ctx.t`)
})

test('prefixing edge case for reused AST ssr mode', () => {
const src = `
<script setup lang="ts">
import { Foo } from './foo'
</script>
<template>
<Bar>
<template #option="{ foo }"></template>
</Bar>
</template>
`
const { descriptor } = parse(src)
// compileScript triggers importUsageCheck
compileScript(descriptor, { id: 'xxx' })
expect(() =>
compileTemplate({
id: 'xxx',
filename: 'test.vue',
ast: descriptor.template!.ast,
source: descriptor.template!.content,
ssr: true,
}),
).not.toThrowError()
})

interface Pos {
line: number
column: number
Expand Down
8 changes: 5 additions & 3 deletions packages/compiler-ssr/src/transforms/ssrTransformComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
ssrProcessTransitionGroup,
ssrTransformTransitionGroup,
} from './ssrTransformTransitionGroup'
import { extend, isArray, isObject, isSymbol } from '@vue/shared'
import { extend, isArray, isObject, isPlainObject, isSymbol } from '@vue/shared'
import { buildSSRProps } from './ssrTransformElement'
import {
ssrProcessTransition,
Expand Down Expand Up @@ -121,6 +121,8 @@ export const ssrTransformComponent: NodeTransform = (node, context) => {
const vnodeBranches: ReturnStatement[] = []
const clonedNode = clone(node)

console.log(clonedNode)

return function ssrPostTransformComponent() {
// Using the cloned node, build the normal VNode-based branches (for
// fallback in case the child is render-fn based). Store them in an array
Expand Down Expand Up @@ -371,10 +373,10 @@ function subTransform(
function clone(v: any): any {
if (isArray(v)) {
return v.map(clone)
} else if (isObject(v)) {
} else if (isPlainObject(v)) {
const res: any = {}
for (const key in v) {
res[key] = clone(v[key])
res[key] = clone(v[key as keyof typeof v])
}
return res
} else {
Expand Down

0 comments on commit 7dbdb3e

Please sign in to comment.