2024-12-08 23:16:49 +08:00
|
|
|
import {
|
|
|
|
type VaporComponent,
|
|
|
|
type VaporComponentInstance,
|
|
|
|
createComponent,
|
2024-12-10 17:00:35 +08:00
|
|
|
getExposed,
|
2024-12-08 23:16:49 +08:00
|
|
|
mountComponent,
|
|
|
|
unmountComponent,
|
|
|
|
} from './component'
|
2024-12-04 11:54:26 +08:00
|
|
|
import {
|
|
|
|
type AppMountFn,
|
|
|
|
type AppUnmountFn,
|
|
|
|
type CreateAppFunction,
|
|
|
|
createAppAPI,
|
2024-12-10 18:43:26 +08:00
|
|
|
flushOnAppMount,
|
2024-12-04 14:22:26 +08:00
|
|
|
normalizeContainer,
|
2024-12-10 12:49:47 +08:00
|
|
|
warn,
|
2024-12-04 14:22:26 +08:00
|
|
|
} from '@vue/runtime-dom'
|
2024-12-06 22:50:53 +08:00
|
|
|
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 = ''
|
|
|
|
}
|
2024-12-10 18:43:26 +08:00
|
|
|
|
2024-12-10 12:49:47 +08:00
|
|
|
const instance = createComponent(
|
|
|
|
app._component,
|
|
|
|
app._props as RawProps,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
app._context,
|
|
|
|
)
|
2024-12-10 18:43:26 +08:00
|
|
|
|
2024-12-08 23:16:49 +08:00
|
|
|
mountComponent(instance, container)
|
2024-12-10 18:43:26 +08:00
|
|
|
flushOnAppMount()
|
|
|
|
|
2024-12-04 11:54:26 +08:00
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
|
|
|
const unmountApp: AppUnmountFn = app => {
|
2024-12-08 23:16:49 +08:00
|
|
|
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-09 22:04:15 +08:00
|
|
|
export const createVaporApp: CreateAppFunction<ParentNode, VaporComponent> = (
|
|
|
|
comp,
|
|
|
|
props,
|
|
|
|
) => {
|
2024-12-10 17:00:35 +08:00
|
|
|
if (!_createApp) _createApp = createAppAPI(mountApp, unmountApp, getExposed)
|
2024-12-09 22:04:15 +08:00
|
|
|
const app = _createApp(comp, props)
|
2024-12-10 12:49:47 +08:00
|
|
|
|
|
|
|
if (__DEV__) {
|
|
|
|
app.config.globalProperties = new Proxy(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
set() {
|
|
|
|
warn(`app.config.globalProperties is not supported in vapor mode.`)
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-12-04 11:54:26 +08:00
|
|
|
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
|
|
|
}
|