-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathcreate-instance.js
52 lines (41 loc) · 1.19 KB
/
create-instance.js
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
// @flow
import Vue from 'vue'
import addSlots from './add-slots'
import addGlobals from './add-globals'
import addProvide from './add-provide'
import { stubComponents } from './stub-components'
import { throwError } from './util'
import { cloneDeep } from 'lodash'
export default function createConstructor (component: Component, options: Options): Component {
const vue = options.localVue || Vue
if (options.context) {
if (!component.functional) {
throwError('mount.context can only be used when mounting a functional component')
}
if (typeof options.context !== 'object') {
throwError('mount.context must be an object')
}
const clonedComponent = cloneDeep(component)
component = {
render (h) {
return h(clonedComponent, options.context)
}
}
}
if (options.provide) {
addProvide(component, options)
}
if (options.stub) {
stubComponents(component, options.stub)
}
const Constructor = vue.extend(component)
if (options.intercept) {
const globals = addGlobals(options.intercept)
Constructor.use(globals)
}
const vm = new Constructor(options)
if (options.slots) {
addSlots(vm, options.slots)
}
return vm
}