Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime-core): add special property to get class component options #821

Merged
merged 2 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/runtime-core/__tests__/vnode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ describe('vnode', () => {
}
})

test('create with class component', () => {
class Component {
$props: any
static __vueClassComponentOptions = { template: '<div />' }
}
const vnode = createVNode(Component)
expect(vnode.type).toEqual(Component.__vueClassComponentOptions)
})

describe('class normalization', () => {
test('string', () => {
const vnode = createVNode('p', { class: 'foo baz' })
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ export interface FunctionalComponent<P = {}> extends SFCInternalOptions {
displayName?: string
}

export interface ClassComponent {
new (...args: any[]): ComponentPublicInstance<any, any, any, any, any>
__vueClassComponentOptions: ComponentOptions
}

export type Component = ComponentOptions | FunctionalComponent

// A type used in public APIs where a component type is expected.
// The constructor type is an artificial type returned by defineComponent().
export type PublicAPIComponent =
| Component
| { new (): ComponentPublicInstance<any, any, any, any, any> }
| { new (...args: any[]): ComponentPublicInstance<any, any, any, any, any> }

export { ComponentOptions }

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function h<O>(
children?: RawChildren | RawSlots
): VNode

// fake constructor type returned by `defineComponent`
// fake constructor type returned by `defineComponent` or class component
export function h(type: Constructor, children?: RawChildren): VNode
export function h<P>(
type: Constructor<P>,
Expand Down
12 changes: 9 additions & 3 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
ComponentInternalInstance,
Data,
SetupProxySymbol,
Component
Component,
ClassComponent
} from './component'
import { RawSlots } from './componentSlots'
import { isReactive, Ref } from '@vue/reactivity'
Expand Down Expand Up @@ -162,7 +163,7 @@ export function setBlockTracking(value: number) {
// A block root keeps track of dynamic nodes within the block in the
// `dynamicChildren` array.
export function createBlock(
type: VNodeTypes,
type: VNodeTypes | ClassComponent,
props?: { [key: string]: any } | null,
children?: any,
patchFlag?: number,
Expand Down Expand Up @@ -203,7 +204,7 @@ export function isSameVNodeType(n1: VNode, n2: VNode): boolean {
}

export function createVNode(
type: VNodeTypes,
type: VNodeTypes | ClassComponent,
props: (Data & VNodeProps) | null = null,
children: unknown = null,
patchFlag: number = 0,
Expand All @@ -214,6 +215,11 @@ export function createVNode(
type = Comment
}

// class component normalization.
if (isFunction(type) && '__vueClassComponentOptions' in type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a shorter key to save a few bytes? Maybe __vccOpts? (I assume this key will be fully internal and never exposed to the user)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, renamed.

type = type.__vueClassComponentOptions
}

// class & style normalization.
if (props !== null) {
// for reactive or proxy objects, we need to clone it to enable mutation.
Expand Down