-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathcreate-instance.js
70 lines (54 loc) · 1.92 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// @flow
import addSlots from './add-slots'
import addMocks from './add-mocks'
import addAttrs from './add-attrs'
import addListeners from './add-listeners'
import addProvide from './add-provide'
import { addEventLogger } from './log-events'
import { stubComponents } from './stub-components'
import { throwError } from './util'
import { compileTemplate } from './compile-template'
import createLocalVue from '../create-local-vue'
import extractOptions from '../options/extract-options'
import deleteMountingOptions from '../options/delete-mounting-options'
import createFunctionalComponent from './create-functional-component'
import cloneDeep from 'lodash/cloneDeep'
export default function createConstructor (
component: Component,
options: Options
): Component {
const mountingOptions = extractOptions(options)
const vue = mountingOptions.localVue || createLocalVue()
if (mountingOptions.mocks) {
addMocks(mountingOptions.mocks, vue)
}
if (component.functional) {
component = createFunctionalComponent(component, mountingOptions)
} else if (mountingOptions.context) {
throwError(
'mount.context can only be used when mounting a functional component'
)
}
if (mountingOptions.provide) {
addProvide(component, mountingOptions.provide, options)
}
if (mountingOptions.stubs) {
stubComponents(component, mountingOptions.stubs)
}
if (!component.render && component.template && !component.functional) {
compileTemplate(component)
}
addEventLogger(vue)
const Constructor = vue.extend(component)
const instanceOptions = { ...options }
deleteMountingOptions(instanceOptions)
const vm = new Constructor(instanceOptions)
addAttrs(vm, mountingOptions.attrs)
addListeners(vm, mountingOptions.listeners)
vm.$_mountingOptionsSlots = mountingOptions.slots
vm.$_originalSlots = cloneDeep(vm.$slots)
if (mountingOptions.slots) {
addSlots(vm, mountingOptions.slots)
}
return vm
}