-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathstub-components.js
91 lines (80 loc) · 2.9 KB
/
stub-components.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// @flow
import { compileToFunctions } from 'vue-template-compiler'
import { validateStubOptions } from 'shared/stub-components-validate'
import { componentNeedsCompiling } from 'shared/validators'
import { compileTemplate } from 'shared/compile-template'
function getCoreProperties (component: Component): Object {
if (!component) return {}
return {
attrs: component.attrs,
name: component.name,
on: component.on,
key: component.key,
ref: component.ref,
props: component.props,
domProps: component.domProps,
class: component.class,
staticClass: component.staticClass,
staticStyle: component.staticStyle,
style: component.style,
normalizedStyle: component.normalizedStyle,
nativeOn: component.nativeOn,
functional: component.functional
}
}
function createStubFromString (originalComponent: Component, template: string): Object {
return {
...getCoreProperties(originalComponent),
...compileToFunctions(template)
}
}
function createBlankStub (originalComponent: Component): Object {
return {
...getCoreProperties(originalComponent),
render: h => h('')
}
}
function createStubFromComponent (component: Component, name: string): Object {
if (componentNeedsCompiling(component)) compileTemplate(component)
return name ? { ...component, name } : component
}
function createStub (originalComponent: Component, stubValue): Object {
if (stubValue === true) {
return createBlankStub(originalComponent)
} else if (typeof stubValue === 'string') {
return createStubFromString(originalComponent, stubValue)
} else {
return createStubFromComponent(stubValue, originalComponent && originalComponent.name)
}
}
function normalizeStubOptions (components: ?Object, stubOptions: ?Object | Array<string>): Object {
if (!stubOptions) {
stubOptions = Object.keys(components || {})
}
if (Array.isArray(stubOptions)) {
stubOptions = stubOptions.reduce((object, name) => {
object[name] = true
return object
}, {})
}
return stubOptions
}
export function createComponentStubs (components: Object = {}, stubOptions: Object): Object {
validateStubOptions(stubOptions)
return createStubs(components, stubOptions)
}
export function createComponentStubsForAll (component: Component, stubs: Object = {}): Object {
if (!component) return stubs
Object.assign(stubs, createStubs(component.components))
return createComponentStubsForAll(component.extends || component.extendOptions, stubs)
}
export function createStubs (components: Object, stubOptions: ?Object | Array<string>): Object {
const options: Object = normalizeStubOptions(components, stubOptions)
return Object.keys(options)
.filter(name => !['KeepAlive', 'Transition', 'TransitionGroup'].includes(name))
.filter(name => options[name] !== false)
.reduce((stubs, name) => {
stubs[name] = createStub(components[name], options[name])
return stubs
}, {})
}