feat(runtime-vapor): support functional component for defineVaporComponent (#12927)

This commit is contained in:
zhiyuanzmj 2025-02-28 17:07:55 +08:00 committed by GitHub
parent 5452404b71
commit bce7164bf0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import { createVaporApp, defineVaporComponent } from '../src'
import { createVaporApp } from '../src'
import type { App } from '@vue/runtime-dom'
import type { VaporComponent, VaporComponentInstance } from '../src/component'
import type { RawProps } from '../src/componentProps'
@ -36,7 +36,8 @@ export function makeRender<C = VaporComponent>(
})
function define(comp: C) {
const component = defineVaporComponent(comp as any)
const component = comp as any
component.__vapor = true
let instance: VaporComponentInstance | undefined
let app: App

View File

@ -127,6 +127,27 @@ describe('component: props', () => {
expect(props).toBe(attrs)
})
test('functional defineVaporComponent without declaration', () => {
let props: any
let attrs: any
const { render } = define(
defineVaporComponent((_props: any, { attrs: _attrs }: any) => {
props = _props
attrs = _attrs
return []
}),
)
render({ foo: () => 1 })
expect(props).toEqual({})
expect(attrs).toEqual({ foo: 1 })
render({ bar: () => 2 })
expect(props).toEqual({})
expect(attrs).toEqual({ bar: 2 })
})
test('boolean casting', () => {
let props: any
const { render } = define({

View File

@ -1,7 +1,20 @@
import type { VaporComponent } from './component'
import type { ObjectVaporComponent, VaporComponent } from './component'
import { extend, isFunction } from '@vue/shared'
/*! #__NO_SIDE_EFFECTS__ */
export function defineVaporComponent(comp: VaporComponent): VaporComponent {
export function defineVaporComponent(
comp: VaporComponent,
extraOptions?: Omit<ObjectVaporComponent, 'setup'>,
): VaporComponent {
if (isFunction(comp)) {
// #8236: extend call and options.name access are considered side-effects
// by Rollup, so we have to wrap it in a pure-annotated IIFE.
return /*@__PURE__*/ (() =>
extend({ name: comp.name }, extraOptions, {
setup: comp,
__vapor: true,
}))()
}
// TODO type inference
comp.__vapor = true
return comp