vue3-core/packages/runtime-vapor/src/apiCreateApp.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import {
type VaporComponent,
type VaporComponentInstance,
createComponent,
mountComponent,
unmountComponent,
} from './component'
2024-12-04 11:54:26 +08:00
import {
type AppMountFn,
type AppUnmountFn,
type CreateAppFunction,
createAppAPI,
2024-12-04 14:22:26 +08:00
normalizeContainer,
} from '@vue/runtime-dom'
import type { RawProps } from './componentProps'
2024-12-04 11:54:26 +08:00
2024-12-04 13:50:54 +08:00
let _createApp: CreateAppFunction<ParentNode, VaporComponent>
2024-12-04 11:54:26 +08:00
const mountApp: AppMountFn<ParentNode> = (app, container) => {
// clear content before mounting
if (container.nodeType === 1 /* Node.ELEMENT_NODE */) {
container.textContent = ''
}
const instance = createComponent(app._component, app._props as RawProps)
mountComponent(instance, container)
2024-12-04 11:54:26 +08:00
return instance
}
const unmountApp: AppUnmountFn = app => {
unmountComponent(app._instance as VaporComponentInstance, app._container)
2024-12-04 11:54:26 +08:00
}
2024-12-03 16:48:28 +08:00
2024-12-04 13:50:54 +08:00
export const createVaporApp: CreateAppFunction<
ParentNode,
VaporComponent
> = comp => {
if (!_createApp)
_createApp = createAppAPI(mountApp, unmountApp, i => i.exposed)
2024-12-04 11:54:26 +08:00
const app = _createApp(comp)
const mount = app.mount
app.mount = (container, ...args: any[]) => {
2024-12-04 14:22:26 +08:00
container = normalizeContainer(container) as ParentNode
2024-12-04 11:54:26 +08:00
return mount(container, ...args)
2024-12-03 16:48:28 +08:00
}
2024-12-04 11:54:26 +08:00
return app
2024-12-03 16:48:28 +08:00
}