You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.9 KiB

1 month ago
  1. import Vue from 'vue'
  2. import { getCurrentInstance } from 'vue'
  3. var isVue2 = true
  4. var isVue3 = false
  5. var Vue2 = Vue
  6. var warn = Vue.util.warn
  7. function install() {}
  8. // createApp polyfill
  9. export function createApp(rootComponent, rootProps) {
  10. var vm
  11. var provide = {}
  12. var app = {
  13. config: Vue.config,
  14. use: Vue.use.bind(Vue),
  15. mixin: Vue.mixin.bind(Vue),
  16. component: Vue.component.bind(Vue),
  17. provide: function (key, value) {
  18. provide[key] = value
  19. return this
  20. },
  21. directive: function (name, dir) {
  22. if (dir) {
  23. Vue.directive(name, dir)
  24. return app
  25. } else {
  26. return Vue.directive(name)
  27. }
  28. },
  29. mount: function (el, hydrating) {
  30. if (!vm) {
  31. vm = new Vue(Object.assign({ propsData: rootProps }, rootComponent, { provide: Object.assign(provide, rootComponent.provide) }))
  32. vm.$mount(el, hydrating)
  33. return vm
  34. } else {
  35. return vm
  36. }
  37. },
  38. unmount: function () {
  39. if (vm) {
  40. vm.$destroy()
  41. vm = undefined
  42. }
  43. },
  44. }
  45. return app
  46. }
  47. export {
  48. Vue,
  49. Vue2,
  50. isVue2,
  51. isVue3,
  52. install,
  53. warn
  54. }
  55. // Vue 3 components mock
  56. function createMockComponent(name) {
  57. return {
  58. setup() {
  59. throw new Error('[vue-demi] ' + name + ' is not supported in Vue 2. It\'s provided to avoid compiler errors.')
  60. }
  61. }
  62. }
  63. export var Fragment = /*#__PURE__*/ createMockComponent('Fragment')
  64. export var Transition = /*#__PURE__*/ createMockComponent('Transition')
  65. export var TransitionGroup = /*#__PURE__*/ createMockComponent('TransitionGroup')
  66. export var Teleport = /*#__PURE__*/ createMockComponent('Teleport')
  67. export var Suspense = /*#__PURE__*/ createMockComponent('Suspense')
  68. export var KeepAlive = /*#__PURE__*/ createMockComponent('KeepAlive')
  69. export * from 'vue'
  70. // Not implemented https://github.com/vuejs/core/pull/8111, falls back to getCurrentInstance()
  71. export function hasInjectionContext() {
  72. return !!getCurrentInstance()
  73. }