index.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import {
  2. createRenderer,
  3. createHydrationRenderer,
  4. warn,
  5. RootRenderFunction,
  6. CreateAppFunction,
  7. Renderer,
  8. HydrationRenderer,
  9. App,
  10. RootHydrateFunction,
  11. isRuntimeOnly,
  12. DeprecationTypes,
  13. compatUtils
  14. } from '@vue/runtime-core'
  15. import { nodeOps } from './nodeOps'
  16. import { patchProp } from './patchProp'
  17. // Importing from the compiler, will be tree-shaken in prod
  18. import {
  19. isFunction,
  20. isString,
  21. isHTMLTag,
  22. isSVGTag,
  23. extend,
  24. NOOP
  25. } from '@vue/shared'
  26. declare module '@vue/reactivity' {
  27. export interface RefUnwrapBailTypes {
  28. // Note: if updating this, also update `types/refBail.d.ts`.
  29. runtimeDOMBailTypes: Node | Window
  30. }
  31. }
  32. const rendererOptions = extend({ patchProp }, nodeOps)
  33. // lazy create the renderer - this makes core renderer logic tree-shakable
  34. // in case the user only imports reactivity utilities from Vue.
  35. let renderer: Renderer<Element | ShadowRoot> | HydrationRenderer
  36. let enabledHydration = false
  37. function ensureRenderer() {
  38. return (
  39. renderer ||
  40. (renderer = createRenderer<Node, Element | ShadowRoot>(rendererOptions))
  41. )
  42. }
  43. function ensureHydrationRenderer() {
  44. renderer = enabledHydration
  45. ? renderer
  46. : createHydrationRenderer(rendererOptions)
  47. enabledHydration = true
  48. return renderer as HydrationRenderer
  49. }
  50. // use explicit type casts here to avoid import() calls in rolled-up d.ts
  51. export const render = ((...args) => {
  52. ensureRenderer().render(...args)
  53. }) as RootRenderFunction<Element | ShadowRoot>
  54. export const hydrate = ((...args) => {
  55. ensureHydrationRenderer().hydrate(...args)
  56. }) as RootHydrateFunction
  57. export const createApp = ((...args) => {
  58. const app = ensureRenderer().createApp(...args)
  59. if (__DEV__) {
  60. injectNativeTagCheck(app)
  61. injectCompilerOptionsCheck(app)
  62. }
  63. const { mount } = app
  64. app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
  65. const container = normalizeContainer(containerOrSelector)
  66. if (!container) return
  67. const component = app._component
  68. if (!isFunction(component) && !component.render && !component.template) {
  69. // __UNSAFE__
  70. // Reason: potential execution of JS expressions in in-DOM template.
  71. // The user must make sure the in-DOM template is trusted. If it's
  72. // rendered by the server, the template should not contain any user data.
  73. component.template = container.innerHTML
  74. // 2.x compat check
  75. if (__COMPAT__ && __DEV__) {
  76. for (let i = 0; i < container.attributes.length; i++) {
  77. const attr = container.attributes[i]
  78. if (attr.name !== 'v-cloak' && /^(v-|:|@)/.test(attr.name)) {
  79. compatUtils.warnDeprecation(
  80. DeprecationTypes.GLOBAL_MOUNT_CONTAINER,
  81. null
  82. )
  83. break
  84. }
  85. }
  86. }
  87. }
  88. // clear content before mounting
  89. container.innerHTML = ''
  90. const proxy = mount(container, false, container instanceof SVGElement)
  91. if (container instanceof Element) {
  92. container.removeAttribute('v-cloak')
  93. container.setAttribute('data-v-app', '')
  94. }
  95. return proxy
  96. }
  97. return app
  98. }) as CreateAppFunction<Element>
  99. export const createSSRApp = ((...args) => {
  100. const app = ensureHydrationRenderer().createApp(...args)
  101. if (__DEV__) {
  102. injectNativeTagCheck(app)
  103. injectCompilerOptionsCheck(app)
  104. }
  105. const { mount } = app
  106. app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
  107. const container = normalizeContainer(containerOrSelector)
  108. if (container) {
  109. return mount(container, true, container instanceof SVGElement)
  110. }
  111. }
  112. return app
  113. }) as CreateAppFunction<Element>
  114. function injectNativeTagCheck(app: App) {
  115. // Inject `isNativeTag`
  116. // this is used for component name validation (dev only)
  117. Object.defineProperty(app.config, 'isNativeTag', {
  118. value: (tag: string) => isHTMLTag(tag) || isSVGTag(tag),
  119. writable: false
  120. })
  121. }
  122. // dev only
  123. function injectCompilerOptionsCheck(app: App) {
  124. if (isRuntimeOnly()) {
  125. const isCustomElement = app.config.isCustomElement
  126. Object.defineProperty(app.config, 'isCustomElement', {
  127. get() {
  128. return isCustomElement
  129. },
  130. set() {
  131. warn(
  132. `The \`isCustomElement\` config option is deprecated. Use ` +
  133. `\`compilerOptions.isCustomElement\` instead.`
  134. )
  135. }
  136. })
  137. const compilerOptions = app.config.compilerOptions
  138. const msg =
  139. `The \`compilerOptions\` config option is only respected when using ` +
  140. `a build of Vue.js that includes the runtime compiler (aka "full build"). ` +
  141. `Since you are using the runtime-only build, \`compilerOptions\` ` +
  142. `must be passed to \`@vue/compiler-dom\` in the build setup instead.\n` +
  143. `- For vue-loader: pass it via vue-loader's \`compilerOptions\` loader option.\n` +
  144. `- For vue-cli: see https://cli.vuejs.org/guide/webpack.html#modifying-options-of-a-loader\n` +
  145. `- For vite: pass it via @vitejs/plugin-vue options. See https://github.com/vitejs/vite/tree/main/packages/plugin-vue#example-for-passing-options-to-vuecompiler-dom`
  146. Object.defineProperty(app.config, 'compilerOptions', {
  147. get() {
  148. warn(msg)
  149. return compilerOptions
  150. },
  151. set() {
  152. warn(msg)
  153. }
  154. })
  155. }
  156. }
  157. function normalizeContainer(
  158. container: Element | ShadowRoot | string
  159. ): Element | null {
  160. if (isString(container)) {
  161. const res = document.querySelector(container)
  162. if (__DEV__ && !res) {
  163. warn(
  164. `Failed to mount app: mount target selector "${container}" returned null.`
  165. )
  166. }
  167. return res
  168. }
  169. if (
  170. __DEV__ &&
  171. window.ShadowRoot &&
  172. container instanceof window.ShadowRoot &&
  173. container.mode === 'closed'
  174. ) {
  175. warn(
  176. `mounting on a ShadowRoot with \`{mode: "closed"}\` may lead to unpredictable bugs`
  177. )
  178. }
  179. return container as any
  180. }
  181. // Custom element support
  182. export {
  183. defineCustomElement,
  184. defineSSRCustomElement,
  185. VueElement,
  186. VueElementConstructor
  187. } from './apiCustomElement'
  188. // SFC CSS utilities
  189. export { useCssModule } from './helpers/useCssModule'
  190. export { useCssVars } from './helpers/useCssVars'
  191. // DOM-only components
  192. export { Transition, TransitionProps } from './components/Transition'
  193. export {
  194. TransitionGroup,
  195. TransitionGroupProps
  196. } from './components/TransitionGroup'
  197. // **Internal** DOM-only runtime directive helpers
  198. export {
  199. vModelText,
  200. vModelCheckbox,
  201. vModelRadio,
  202. vModelSelect,
  203. vModelDynamic
  204. } from './directives/vModel'
  205. export { withModifiers, withKeys } from './directives/vOn'
  206. export { vShow } from './directives/vShow'
  207. import { initVModelForSSR } from './directives/vModel'
  208. import { initVShowForSSR } from './directives/vShow'
  209. let ssrDirectiveInitialized = false
  210. /**
  211. * @internal
  212. */
  213. export const initDirectivesForSSR = __SSR__
  214. ? () => {
  215. if (!ssrDirectiveInitialized) {
  216. ssrDirectiveInitialized = true
  217. initVModelForSSR()
  218. initVShowForSSR()
  219. }
  220. }
  221. : NOOP
  222. // re-export everything from core
  223. // h, Component, reactivity API, nextTick, flags & types
  224. export * from '@vue/runtime-core'