/
stubs.ts
255 lines (218 loc) · 6.71 KB
/
stubs.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import {
transformVNodeArgs,
Transition,
TransitionGroup,
h,
ComponentPublicInstance,
ComponentOptions,
defineComponent,
VNodeTypes,
ConcreteComponent,
DefineComponent,
ComponentPropsOptions
} from 'vue'
import { hyphenate } from './utils/vueShared'
import { matchName } from './utils/matchName'
import { isComponent, isFunctionalComponent, isObjectComponent } from './utils'
import { ComponentInternalInstance } from '@vue/runtime-core'
import {
isLegacyExtendedComponent,
unwrapLegacyVueExtendComponent
} from './utils/vueCompatSupport'
import { Stub, Stubs } from './types'
interface StubOptions {
name: string
propsDeclaration?: ComponentPropsOptions
renderStubDefaultSlot?: boolean
}
const stubsMap: WeakMap<ConcreteComponent, VNodeTypes> = new WeakMap()
export const getOriginalVNodeTypeFromStub = (
type: ConcreteComponent
): VNodeTypes | undefined => stubsMap.get(type)
const doNotStubComponents: WeakSet<ConcreteComponent> = new WeakSet()
const shouldNotStub = (type: ConcreteComponent) => doNotStubComponents.has(type)
export const addToDoNotStubComponents = (type: ConcreteComponent) =>
doNotStubComponents.add(type)
export const createStub = ({
name,
propsDeclaration,
renderStubDefaultSlot
}: StubOptions) => {
const anonName = 'anonymous-stub'
const tag = name ? `${hyphenate(name)}-stub` : anonName
const render = (ctx: ComponentPublicInstance) => {
return h(tag, ctx.$props, renderStubDefaultSlot ? ctx.$slots : undefined)
}
return defineComponent({
name: name || anonName,
compatConfig: { MODE: 3, RENDER_FUNCTION: false },
render,
props: propsDeclaration || {}
})
}
const createTransitionStub = ({ name }: StubOptions) => {
const render = (ctx: ComponentPublicInstance) => {
return h(name, {}, ctx.$slots)
}
return defineComponent({
name,
compatConfig: { MODE: 3, RENDER_FUNCTION: false },
render
})
}
const resolveComponentStubByName = (componentName: string, stubs: Stubs) => {
if (Array.isArray(stubs) && stubs.length) {
// ['Foo', 'Bar'] => { Foo: true, Bar: true }
stubs = stubs.reduce((acc, current) => {
acc[current] = true
return acc
}, {} as Record<string, Stub>)
}
for (const [stubKey, value] of Object.entries(stubs)) {
if (matchName(componentName, stubKey)) {
return value
}
}
}
const getComponentRegisteredName = (
instance: ComponentInternalInstance | null,
type: VNodeTypes
): string | null => {
if (!instance || !instance.parent) return null
// try to infer the name based on local resolution
const registry = (instance.type as any).components
for (const key in registry) {
if (registry[key] === type) {
return key
}
}
return null
}
const getComponentName = (type: VNodeTypes): string => {
if (isObjectComponent(type)) {
return type.name || ''
}
if (isLegacyExtendedComponent(type)) {
return unwrapLegacyVueExtendComponent(type).name || ''
}
if (isFunctionalComponent(type)) {
return type.displayName || type.name
}
return ''
}
function createStubOnceForType(
type: {} & VNodeTypes,
factoryFn: () => ConcreteComponent,
cache: WeakMap<{} & VNodeTypes, ConcreteComponent>
): ConcreteComponent {
const cachedStub = cache.get(type)
if (cachedStub) {
return cachedStub
}
const stub = factoryFn()
stubsMap.set(stub, type)
cache.set(type, stub)
return stub
}
export function stubComponents(
stubs: Stubs = {},
shallow: boolean = false,
renderStubDefaultSlot: boolean = false
) {
const createdStubsMap: WeakMap<{} & VNodeTypes, ConcreteComponent> =
new WeakMap()
const createStubOnce = (
type: {} & VNodeTypes,
factoryFn: () => ConcreteComponent
) => createStubOnceForType(type, factoryFn, createdStubsMap)
transformVNodeArgs((args, instance: ComponentInternalInstance | null) => {
const [nodeType, props, children, patchFlag, dynamicProps] = args
const type = nodeType as VNodeTypes
// stub transition by default via config.global.stubs
if (type === Transition && 'transition' in stubs && stubs['transition']) {
return [
createTransitionStub({
name: 'transition-stub'
}),
undefined,
children
]
}
// stub transition-group by default via config.global.stubs
if (
type === TransitionGroup &&
'transition-group' in stubs &&
stubs['transition-group']
) {
return [
createTransitionStub({
name: 'transition-group-stub'
}),
undefined,
children
]
}
if (isComponent(type) || isFunctionalComponent(type)) {
if (shouldNotStub(type)) {
return args
}
const registeredName = getComponentRegisteredName(instance, type)
const componentName = getComponentName(type)
let stub = null
let name = null
// Prio 1 using the key in locally registered components in the parent
if (registeredName) {
stub = resolveComponentStubByName(registeredName, stubs)
if (stub) {
name = registeredName
}
}
// Prio 2 using the name attribute in the component
if (!stub && componentName) {
stub = resolveComponentStubByName(componentName, stubs)
if (stub) {
name = componentName
}
}
// case 2: custom implementation
if (isComponent(stub)) {
const stubFn = isFunctionalComponent(stub) ? stub : null
const specializedStubComponent: ConcreteComponent = stubFn
? (...args) => stubFn(...args)
: { ...stub }
specializedStubComponent.props = stub.props
const specializedStub = createStubOnce(
type,
() => specializedStubComponent
)
// pass the props and children, for advanced stubbing
return [specializedStub, props, children, patchFlag, dynamicProps]
}
if (stub === false) {
// we explicitly opt out of stubbing this component
return args
}
// we return a stub by matching Vue's `h` function
// where the signature is h(Component, props, slots)
// case 1: default stub
if (stub === true || shallow) {
// Set name when using shallow without stub
const stubName = name || registeredName || componentName
if (!isComponent(type)) {
throw new Error('Attempted to stub a non-component')
}
const propsDeclaration = type.props || {}
const newStub = createStubOnce(type, () =>
createStub({
name: stubName,
propsDeclaration,
renderStubDefaultSlot
})
)
return [newStub, props, children, patchFlag, dynamicProps]
}
}
// do not stub anything what is not a component
return args
})
}