Skip to content

Commit 808d17d

Browse files
committed
refactor(runtime-vapor): split create component & render
1 parent 7e0f15f commit 808d17d

File tree

5 files changed

+15
-24
lines changed

5 files changed

+15
-24
lines changed

packages/runtime-vapor/__tests__/_utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type ObjectComponent,
55
type SetupFn,
66
render as _render,
7+
createComponentInstance,
78
defineComponent,
89
} from '../src'
910

@@ -30,7 +31,8 @@ export function makeRender<Component = ObjectComponent | SetupFn>(
3031
props: Data = {},
3132
container: string | ParentNode = '#host',
3233
) => {
33-
instance = _render(component, props, container)
34+
instance = createComponentInstance(component, props)
35+
_render(instance, container)
3436
return res()
3537
}
3638
const res = () => ({

packages/runtime-vapor/src/component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { EffectScope } from '@vue/reactivity'
22

3-
import { EMPTY_OBJ } from '@vue/shared'
3+
import { EMPTY_OBJ, isFunction } from '@vue/shared'
44
import type { Block } from './render'
55
import type { DirectiveBinding } from './directives'
66
import {
77
type ComponentPropsOptions,
88
type NormalizedPropsOptions,
9+
initProps,
910
normalizePropsOptions,
1011
} from './componentProps'
1112
import {
@@ -243,6 +244,8 @@ export const createComponentInstance = (
243244
// [VaporLifecycleHooks.SERVER_PREFETCH]: null,
244245
}
245246

247+
// TODO init first
248+
initProps(instance, rawProps, !isFunction(component))
246249
instance.emit = emit.bind(null, instance)
247250

248251
return instance

packages/runtime-vapor/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export { on, delegate, delegateEvents, setDynamicEvents } from './dom/event'
8888
export { setRef } from './dom/templateRef'
8989

9090
export { defineComponent } from './apiDefineComponent'
91+
export { createComponentInstance } from './component'
9192
export {
9293
onBeforeMount,
9394
onMounted,

packages/runtime-vapor/src/render.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import { proxyRefs } from '@vue/reactivity'
2+
import { invokeArrayFns, isArray, isFunction, isObject } from '@vue/shared'
23
import {
3-
type Data,
4-
invokeArrayFns,
5-
isArray,
6-
isFunction,
7-
isObject,
8-
} from '@vue/shared'
9-
import {
10-
type Component,
114
type ComponentInternalInstance,
12-
createComponentInstance,
135
setCurrentInstance,
146
unsetCurrentInstance,
157
} from './component'
16-
import { initProps } from './componentProps'
178
import { invokeDirectiveHook } from './directives'
189
import { insert, querySelector, remove } from './dom/element'
1910
import { flushPostFlushCbs, queuePostRenderEffect } from './scheduler'
@@ -28,18 +19,11 @@ export type Fragment = {
2819
}
2920

3021
export function render(
31-
comp: Component,
32-
props: Data,
22+
instance: ComponentInternalInstance,
3323
container: string | ParentNode,
34-
): ComponentInternalInstance {
35-
const instance = createComponentInstance(comp, props)
36-
initProps(instance, props, !isFunction(instance.component))
37-
const component = mountComponent(
38-
instance,
39-
(container = normalizeContainer(container)),
40-
)
24+
): void {
25+
mountComponent(instance, (container = normalizeContainer(container)))
4126
flushPostFlushCbs()
42-
return component
4327
}
4428

4529
function normalizeContainer(container: string | ParentNode): ParentNode {

playground/src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, unmountComponent } from 'vue/vapor'
1+
import { createComponentInstance, render, unmountComponent } from 'vue/vapor'
22
import { createApp } from 'vue'
33
import './style.css'
44

@@ -7,7 +7,8 @@ const mod = (modules['.' + location.pathname] || modules['./App.vue'])()
77

88
mod.then(({ default: mod }) => {
99
if (mod.vapor) {
10-
const instance = render(mod, {}, '#app')
10+
const instance = createComponentInstance(mod, {})
11+
render(instance, '#app')
1112
// @ts-expect-error
1213
globalThis.unmount = () => {
1314
unmountComponent(instance)

0 commit comments

Comments
 (0)