Skip to content

Commit bce7164

Browse files
authored
feat(runtime-vapor): support functional component for defineVaporComponent (#12927)
1 parent 5452404 commit bce7164

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

packages/runtime-vapor/__tests__/_utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createVaporApp, defineVaporComponent } from '../src'
1+
import { createVaporApp } from '../src'
22
import type { App } from '@vue/runtime-dom'
33
import type { VaporComponent, VaporComponentInstance } from '../src/component'
44
import type { RawProps } from '../src/componentProps'
@@ -36,7 +36,8 @@ export function makeRender<C = VaporComponent>(
3636
})
3737

3838
function define(comp: C) {
39-
const component = defineVaporComponent(comp as any)
39+
const component = comp as any
40+
component.__vapor = true
4041
let instance: VaporComponentInstance | undefined
4142
let app: App
4243

packages/runtime-vapor/__tests__/componentProps.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@ describe('component: props', () => {
127127
expect(props).toBe(attrs)
128128
})
129129

130+
test('functional defineVaporComponent without declaration', () => {
131+
let props: any
132+
let attrs: any
133+
134+
const { render } = define(
135+
defineVaporComponent((_props: any, { attrs: _attrs }: any) => {
136+
props = _props
137+
attrs = _attrs
138+
return []
139+
}),
140+
)
141+
142+
render({ foo: () => 1 })
143+
expect(props).toEqual({})
144+
expect(attrs).toEqual({ foo: 1 })
145+
146+
render({ bar: () => 2 })
147+
expect(props).toEqual({})
148+
expect(attrs).toEqual({ bar: 2 })
149+
})
150+
130151
test('boolean casting', () => {
131152
let props: any
132153
const { render } = define({

packages/runtime-vapor/src/apiDefineComponent.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
import type { VaporComponent } from './component'
1+
import type { ObjectVaporComponent, VaporComponent } from './component'
2+
import { extend, isFunction } from '@vue/shared'
23

34
/*! #__NO_SIDE_EFFECTS__ */
4-
export function defineVaporComponent(comp: VaporComponent): VaporComponent {
5+
export function defineVaporComponent(
6+
comp: VaporComponent,
7+
extraOptions?: Omit<ObjectVaporComponent, 'setup'>,
8+
): VaporComponent {
9+
if (isFunction(comp)) {
10+
// #8236: extend call and options.name access are considered side-effects
11+
// by Rollup, so we have to wrap it in a pure-annotated IIFE.
12+
return /*@__PURE__*/ (() =>
13+
extend({ name: comp.name }, extraOptions, {
14+
setup: comp,
15+
__vapor: true,
16+
}))()
17+
}
518
// TODO type inference
619
comp.__vapor = true
720
return comp

0 commit comments

Comments
 (0)