fix(compiler-sfc): ensure props bindings register before compiling template (#13922)

close #13920
This commit is contained in:
edison 2025-09-24 17:03:47 +08:00 committed by GitHub
parent b555f02eed
commit abd563822a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 1 deletions

View File

@ -12,6 +12,27 @@ export function render(_ctx, _cache) {
}"
`;
exports[`prefixing props edge case in inline mode 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import { unref as _unref, openBlock as _openBlock, createBlock as _createBlock } from "vue"
export default /*@__PURE__*/_defineComponent({
props: {
Foo: { type: Object, required: true }
},
setup(__props: any) {
return (_ctx: any,_cache: any) => {
return (_openBlock(), _createBlock(_unref(__props["Foo"]).Bar))
}
}
})"
`;
exports[`should not hoist srcset URLs in SSR mode 1`] = `
"import { resolveComponent as _resolveComponent, withCtx as _withCtx, createVNode as _createVNode } from "vue"
import { ssrRenderAttr as _ssrRenderAttr, ssrRenderComponent as _ssrRenderComponent } from "vue/server-renderer"

View File

@ -512,3 +512,22 @@ test('non-identifier expression in legacy filter syntax', () => {
babelParse(compilationResult.code, { sourceType: 'module' })
}).not.toThrow()
})
test('prefixing props edge case in inline mode', () => {
const src = `
<script setup lang="ts">
defineProps<{ Foo: { Bar: unknown } }>()
</script>
<template>
<Foo.Bar/>
</template>
`
const { descriptor } = parse(src)
const { content } = compileScript(descriptor, {
id: 'xxx',
inlineTemplate: true,
})
expect(content).toMatchSnapshot()
expect(content).toMatch(`__props["Foo"]).Bar`)
})

View File

@ -833,6 +833,8 @@ export function compileScript(
let templateMap
// 9. generate return statement
let returned
// ensure props bindings register before compile template in inline mode
const propsDecl = genRuntimeProps(ctx)
if (
!options.inlineTemplate ||
(!sfc.template && ctx.hasDefaultExportRender)
@ -965,7 +967,6 @@ export function compileScript(
runtimeOptions += `\n __ssrInlineRender: true,`
}
const propsDecl = genRuntimeProps(ctx)
if (propsDecl) runtimeOptions += `\n props: ${propsDecl},`
const emitsDecl = genRuntimeEmits(ctx)