index.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {
  2. createRenderer,
  3. createHydrationRenderer,
  4. warn,
  5. RootRenderFunction,
  6. CreateAppFunction,
  7. Renderer,
  8. HydrationRenderer,
  9. App,
  10. RootHydrateFunction
  11. } from '@vue/runtime-core'
  12. import { nodeOps } from './nodeOps'
  13. import { patchProp, forcePatchProp } from './patchProp'
  14. // Importing from the compiler, will be tree-shaken in prod
  15. import { isFunction, isString, isHTMLTag, isSVGTag, extend } from '@vue/shared'
  16. declare module '@vue/reactivity' {
  17. export interface RefUnwrapBailTypes {
  18. // Note: if updating this, also update `types/refBail.d.ts`.
  19. runtimeDOMBailTypes: Node | Window
  20. }
  21. }
  22. const rendererOptions = extend({ patchProp, forcePatchProp }, nodeOps)
  23. // lazy create the renderer - this makes core renderer logic tree-shakable
  24. // in case the user only imports reactivity utilities from Vue.
  25. let renderer: Renderer<Element> | HydrationRenderer
  26. let enabledHydration = false
  27. function ensureRenderer() {
  28. return renderer || (renderer = createRenderer<Node, Element>(rendererOptions))
  29. }
  30. function ensureHydrationRenderer() {
  31. renderer = enabledHydration
  32. ? renderer
  33. : createHydrationRenderer(rendererOptions)
  34. enabledHydration = true
  35. return renderer as HydrationRenderer
  36. }
  37. // use explicit type casts here to avoid import() calls in rolled-up d.ts
  38. export const render = ((...args) => {
  39. ensureRenderer().render(...args)
  40. }) as RootRenderFunction<Element>
  41. export const hydrate = ((...args) => {
  42. ensureHydrationRenderer().hydrate(...args)
  43. }) as RootHydrateFunction
  44. export const createApp = ((...args) => {
  45. const app = ensureRenderer().createApp(...args)
  46. if (__DEV__) {
  47. injectNativeTagCheck(app)
  48. }
  49. const { mount } = app
  50. app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
  51. const container = normalizeContainer(containerOrSelector)
  52. if (!container) return
  53. const component = app._component
  54. if (!isFunction(component) && !component.render && !component.template) {
  55. component.template = container.innerHTML
  56. }
  57. // clear content before mounting
  58. container.innerHTML = ''
  59. const proxy = mount(container)
  60. if (container instanceof Element) {
  61. container.removeAttribute('v-cloak')
  62. container.setAttribute('data-v-app', '')
  63. }
  64. return proxy
  65. }
  66. return app
  67. }) as CreateAppFunction<Element>
  68. export const createSSRApp = ((...args) => {
  69. const app = ensureHydrationRenderer().createApp(...args)
  70. if (__DEV__) {
  71. injectNativeTagCheck(app)
  72. }
  73. const { mount } = app
  74. app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
  75. const container = normalizeContainer(containerOrSelector)
  76. if (container) {
  77. return mount(container, true)
  78. }
  79. }
  80. return app
  81. }) as CreateAppFunction<Element>
  82. function injectNativeTagCheck(app: App) {
  83. // Inject `isNativeTag`
  84. // this is used for component name validation (dev only)
  85. Object.defineProperty(app.config, 'isNativeTag', {
  86. value: (tag: string) => isHTMLTag(tag) || isSVGTag(tag),
  87. writable: false
  88. })
  89. }
  90. function normalizeContainer(
  91. container: Element | ShadowRoot | string
  92. ): Element | null {
  93. if (isString(container)) {
  94. const res = document.querySelector(container)
  95. if (__DEV__ && !res) {
  96. warn(
  97. `Failed to mount app: mount target selector "${container}" returned null.`
  98. )
  99. }
  100. return res
  101. }
  102. if (
  103. __DEV__ &&
  104. container instanceof window.ShadowRoot &&
  105. container.mode === 'closed'
  106. ) {
  107. warn(
  108. `mounting on a ShadowRoot with \`{mode: "closed"}\` may lead to unpredictable bugs`
  109. )
  110. }
  111. return container as any
  112. }
  113. // SFC CSS utilities
  114. export { useCssModule } from './helpers/useCssModule'
  115. export { useCssVars } from './helpers/useCssVars'
  116. // DOM-only components
  117. export { Transition, TransitionProps } from './components/Transition'
  118. export {
  119. TransitionGroup,
  120. TransitionGroupProps
  121. } from './components/TransitionGroup'
  122. // **Internal** DOM-only runtime directive helpers
  123. export {
  124. vModelText,
  125. vModelCheckbox,
  126. vModelRadio,
  127. vModelSelect,
  128. vModelDynamic
  129. } from './directives/vModel'
  130. export { withModifiers, withKeys } from './directives/vOn'
  131. export { vShow } from './directives/vShow'
  132. // re-export everything from core
  133. // h, Component, reactivity API, nextTick, flags & types
  134. export * from '@vue/runtime-core'