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.

126 lines
3.6 KiB

2 weeks ago
  1. var _VueDemiGlobal = typeof globalThis !== 'undefined'
  2. ? globalThis
  3. : typeof global !== 'undefined'
  4. ? global
  5. : typeof self !== 'undefined'
  6. ? self
  7. : this
  8. var VueDemi = (function (VueDemi, Vue, VueCompositionAPI) {
  9. if (VueDemi.install) {
  10. return VueDemi
  11. }
  12. if (!Vue) {
  13. console.error('[vue-demi] no Vue instance found, please be sure to import `vue` before `vue-demi`.')
  14. return VueDemi
  15. }
  16. // Vue 2.7
  17. if (Vue.version.slice(0, 4) === '2.7.') {
  18. for (var key in Vue) {
  19. VueDemi[key] = Vue[key]
  20. }
  21. VueDemi.isVue2 = true
  22. VueDemi.isVue3 = false
  23. VueDemi.install = function () {}
  24. VueDemi.Vue = Vue
  25. VueDemi.Vue2 = Vue
  26. VueDemi.version = Vue.version
  27. VueDemi.warn = Vue.util.warn
  28. VueDemi.hasInjectionContext = function() {
  29. return !!VueDemi.getCurrentInstance()
  30. }
  31. function createApp(rootComponent, rootProps) {
  32. var vm
  33. var provide = {}
  34. var app = {
  35. config: Vue.config,
  36. use: Vue.use.bind(Vue),
  37. mixin: Vue.mixin.bind(Vue),
  38. component: Vue.component.bind(Vue),
  39. provide: function (key, value) {
  40. provide[key] = value
  41. return this
  42. },
  43. directive: function (name, dir) {
  44. if (dir) {
  45. Vue.directive(name, dir)
  46. return app
  47. } else {
  48. return Vue.directive(name)
  49. }
  50. },
  51. mount: function (el, hydrating) {
  52. if (!vm) {
  53. vm = new Vue(Object.assign({ propsData: rootProps }, rootComponent, { provide: Object.assign(provide, rootComponent.provide) }))
  54. vm.$mount(el, hydrating)
  55. return vm
  56. } else {
  57. return vm
  58. }
  59. },
  60. unmount: function () {
  61. if (vm) {
  62. vm.$destroy()
  63. vm = undefined
  64. }
  65. },
  66. }
  67. return app
  68. }
  69. VueDemi.createApp = createApp
  70. }
  71. // Vue 2.6.x
  72. else if (Vue.version.slice(0, 2) === '2.') {
  73. if (VueCompositionAPI) {
  74. for (var key in VueCompositionAPI) {
  75. VueDemi[key] = VueCompositionAPI[key]
  76. }
  77. VueDemi.isVue2 = true
  78. VueDemi.isVue3 = false
  79. VueDemi.install = function () {}
  80. VueDemi.Vue = Vue
  81. VueDemi.Vue2 = Vue
  82. VueDemi.version = Vue.version
  83. VueDemi.hasInjectionContext = function() {
  84. return !!VueDemi.getCurrentInstance()
  85. }
  86. } else {
  87. console.error('[vue-demi] no VueCompositionAPI instance found, please be sure to import `@vue/composition-api` before `vue-demi`.')
  88. }
  89. }
  90. // Vue 3
  91. else if (Vue.version.slice(0, 2) === '3.') {
  92. for (var key in Vue) {
  93. VueDemi[key] = Vue[key]
  94. }
  95. VueDemi.isVue2 = false
  96. VueDemi.isVue3 = true
  97. VueDemi.install = function () {}
  98. VueDemi.Vue = Vue
  99. VueDemi.Vue2 = undefined
  100. VueDemi.version = Vue.version
  101. VueDemi.set = function (target, key, val) {
  102. if (Array.isArray(target)) {
  103. target.length = Math.max(target.length, key)
  104. target.splice(key, 1, val)
  105. return val
  106. }
  107. target[key] = val
  108. return val
  109. }
  110. VueDemi.del = function (target, key) {
  111. if (Array.isArray(target)) {
  112. target.splice(key, 1)
  113. return
  114. }
  115. delete target[key]
  116. }
  117. } else {
  118. console.error('[vue-demi] Vue version ' + Vue.version + ' is unsupported.')
  119. }
  120. return VueDemi
  121. })(
  122. (_VueDemiGlobal.VueDemi = _VueDemiGlobal.VueDemi || (typeof VueDemi !== 'undefined' ? VueDemi : {})),
  123. _VueDemiGlobal.Vue || (typeof Vue !== 'undefined' ? Vue : undefined),
  124. _VueDemiGlobal.VueCompositionAPI || (typeof VueCompositionAPI !== 'undefined' ? VueCompositionAPI : undefined)
  125. );