-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathinstall.ts
82 lines (73 loc) · 2.01 KB
/
install.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import type { VueConstructor } from 'vue'
import { AnyObject } from './types/basic'
import { isFunction, hasSymbol, hasOwn, isPlainObject, warn } from './utils'
import { isRef } from './reactivity'
import { setVueConstructor, isVueRegistered } from './runtimeContext'
import { mixin } from './mixin'
/**
* Helper that recursively merges two data objects together.
*/
function mergeData(from: AnyObject, to: AnyObject): Object {
if (!from) return to
if (!to) return from
let key: any
let toVal: any
let fromVal: any
const keys = hasSymbol ? Reflect.ownKeys(from) : Object.keys(from)
for (let i = 0; i < keys.length; i++) {
key = keys[i]
// in case the object is already observed...
if (key === '__ob__') continue
toVal = to[key]
fromVal = from[key]
if (!hasOwn(to, key)) {
to[key] = fromVal
} else if (
toVal !== fromVal &&
isPlainObject(toVal) &&
!isRef(toVal) &&
isPlainObject(fromVal) &&
!isRef(fromVal)
) {
mergeData(fromVal, toVal)
}
}
return to
}
export function install(Vue: VueConstructor) {
if (isVueRegistered(Vue)) {
if (__DEV__) {
warn(
'[vue-composition-api] already installed. Vue.use(VueCompositionAPI) should be called only once.'
)
}
return
}
if (__DEV__) {
if (Vue.version) {
if (Vue.version[0] !== '2' || Vue.version[1] !== '.') {
warn(
`[vue-composition-api] only works with Vue 2, v${Vue.version} found.`
)
}
} else {
warn('[vue-composition-api] no Vue version found')
}
}
Vue.config.optionMergeStrategies.setup = function (
parent: Function,
child: Function
) {
return function mergedSetupFn(props: any, context: any) {
return mergeData(
isFunction(parent) ? parent(props, context) || {} : undefined,
isFunction(child) ? child(props, context) || {} : undefined
)
}
}
setVueConstructor(Vue)
mixin(Vue)
}
export const Plugin = {
install: (Vue: VueConstructor) => install(Vue),
}