apiCreateApp.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {
  2. type VaporComponent,
  3. type VaporComponentInstance,
  4. createComponent,
  5. getExposed,
  6. mountComponent,
  7. unmountComponent,
  8. } from './component'
  9. import {
  10. type App,
  11. type AppMountFn,
  12. type AppUnmountFn,
  13. type CreateAppFunction,
  14. createAppAPI,
  15. flushOnAppMount,
  16. initFeatureFlags,
  17. normalizeContainer,
  18. setDevtoolsHook,
  19. warn,
  20. } from '@vue/runtime-dom'
  21. import type { RawProps } from './componentProps'
  22. import { getGlobalThis } from '@vue/shared'
  23. import { optimizePropertyLookup } from './dom/prop'
  24. import { setIsHydratingEnabled, withHydration } from './dom/hydration'
  25. let _createApp: CreateAppFunction<ParentNode, VaporComponent>
  26. const mountApp: AppMountFn<ParentNode> = (app, container) => {
  27. optimizePropertyLookup()
  28. // clear content before mounting
  29. if (container.nodeType === 1 /* Node.ELEMENT_NODE */) {
  30. if (__DEV__ && container.childNodes.length) {
  31. warn('mount target container is not empty and will be cleared.')
  32. }
  33. container.textContent = ''
  34. }
  35. const instance =
  36. (app._ceComponent as VaporComponentInstance) ||
  37. createComponent(
  38. app._component,
  39. app._props as RawProps,
  40. null,
  41. false,
  42. false,
  43. app._context,
  44. )
  45. mountComponent(instance, container)
  46. flushOnAppMount()
  47. return instance!
  48. }
  49. let _hydrateApp: CreateAppFunction<ParentNode, VaporComponent>
  50. const hydrateApp: AppMountFn<ParentNode> = (app, container) => {
  51. optimizePropertyLookup()
  52. let instance: VaporComponentInstance
  53. withHydration(container, () => {
  54. instance =
  55. (app._ceComponent as VaporComponentInstance) ||
  56. createComponent(
  57. app._component,
  58. app._props as RawProps,
  59. null,
  60. false,
  61. false,
  62. app._context,
  63. )
  64. mountComponent(instance, container)
  65. flushOnAppMount()
  66. })
  67. return instance!
  68. }
  69. const unmountApp: AppUnmountFn = app => {
  70. unmountComponent(app._instance as VaporComponentInstance, app._container)
  71. }
  72. function prepareApp() {
  73. // compile-time feature flags check
  74. if (__ESM_BUNDLER__ && !__TEST__) {
  75. initFeatureFlags()
  76. }
  77. const target = getGlobalThis()
  78. target.__VUE__ = true
  79. if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
  80. setDevtoolsHook(target.__VUE_DEVTOOLS_GLOBAL_HOOK__, target)
  81. }
  82. }
  83. function postPrepareApp(app: App) {
  84. app.vapor = true
  85. const mount = app.mount
  86. app.mount = (container, ...args: any[]) => {
  87. container = normalizeContainer(container) as ParentNode
  88. const proxy = mount(container, ...args)
  89. if (container instanceof Element) {
  90. container.removeAttribute('v-cloak')
  91. container.setAttribute('data-v-app', '')
  92. }
  93. return proxy
  94. }
  95. }
  96. export const createVaporApp: CreateAppFunction<ParentNode, VaporComponent> = (
  97. comp,
  98. props,
  99. ) => {
  100. prepareApp()
  101. if (!_createApp) _createApp = createAppAPI(mountApp, unmountApp, getExposed)
  102. const app = _createApp(comp, props)
  103. postPrepareApp(app)
  104. return app
  105. }
  106. export const createVaporSSRApp: CreateAppFunction<
  107. ParentNode,
  108. VaporComponent
  109. > = (comp, props) => {
  110. setIsHydratingEnabled(true)
  111. prepareApp()
  112. if (!_hydrateApp)
  113. _hydrateApp = createAppAPI(hydrateApp, unmountApp, getExposed)
  114. const app = _hydrateApp(comp, props)
  115. postPrepareApp(app)
  116. return app
  117. }