-
Notifications
You must be signed in to change notification settings - Fork 258
/
config.ts
81 lines (73 loc) · 2.18 KB
/
config.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
import { GlobalMountOptions } from './types'
import { VueWrapper } from './vueWrapper'
import { DOMWrapper } from './domWrapper'
export interface GlobalConfigOptions {
global: Required<GlobalMountOptions>
plugins: {
VueWrapper: Pluggable<VueWrapper>
DOMWrapper: Pluggable<DOMWrapper<Node>>
}
renderStubDefaultSlot: boolean
}
interface Plugin<Instance, O> {
handler(instance: Instance): Record<string, any>
handler(instance: Instance, options: O): Record<string, any>
options: O
}
class Pluggable<Instance = DOMWrapper<Node>> {
installedPlugins: Plugin<Instance, any>[] = []
install<O>(handler: (instance: Instance) => Record<string, any>): void
install<O>(
handler: (instance: Instance, options: O) => Record<string, any>,
options: O
): void
install<O>(
handler: (instance: Instance, options?: O) => Record<string, any>,
options?: O
): void {
if (typeof handler !== 'function') {
console.error('plugin.install must receive a function')
handler = () => ({})
}
this.installedPlugins.push({ handler, options })
}
extend(instance: Instance) {
const invokeSetup = ({ handler, options }: Plugin<Instance, any>) => {
return handler(instance, options) // invoke the setup method passed to install
}
const bindProperty = ([property, value]: [string, any]) => {
;(instance as any)[property] =
typeof value === 'function' ? value.bind(instance) : value
}
const addAllPropertiesFromSetup = (setupResult: Record<string, any>) => {
setupResult = typeof setupResult === 'object' ? setupResult : {}
Object.entries(setupResult).forEach(bindProperty)
}
this.installedPlugins.map(invokeSetup).forEach(addAllPropertiesFromSetup)
}
/** For testing */
reset() {
this.installedPlugins = []
}
}
export const config: GlobalConfigOptions = {
global: {
stubs: {
transition: true,
'transition-group': true
},
provide: {},
components: {},
config: {},
directives: {},
mixins: [],
mocks: {},
plugins: [],
renderStubDefaultSlot: false
},
plugins: {
VueWrapper: new Pluggable(),
DOMWrapper: new Pluggable()
},
renderStubDefaultSlot: false
}