2
2
3
3
import Vue from 'vue'
4
4
import { compileToFunctions } from 'vue-template-compiler'
5
- import { throwError } from './util'
5
+ import {
6
+ throwError ,
7
+ camelize ,
8
+ capitalize ,
9
+ hyphenate
10
+ } from './util'
6
11
import {
7
12
componentNeedsCompiling ,
8
13
templateContainsComponent
@@ -21,28 +26,37 @@ function isValidStub (stub: any) {
21
26
)
22
27
}
23
28
29
+ function resolveComponent ( obj , component ) {
30
+ return obj [ component ] ||
31
+ obj [ hyphenate ( component ) ] ||
32
+ obj [ camelize ( component ) ] ||
33
+ obj [ capitalize ( camelize ( component ) ) ] ||
34
+ obj [ capitalize ( component ) ] ||
35
+ { }
36
+ }
37
+
24
38
function isRequiredComponent ( name ) {
25
39
return (
26
40
name === 'KeepAlive' || name === 'Transition' || name === 'TransitionGroup'
27
41
)
28
42
}
29
43
30
- function getCoreProperties ( component : Component ) : Object {
44
+ function getCoreProperties ( componentOptions : Component ) : Object {
31
45
return {
32
- attrs : component . attrs ,
33
- name : component . name ,
34
- on : component . on ,
35
- key : component . key ,
36
- ref : component . ref ,
37
- props : component . props ,
38
- domProps : component . domProps ,
39
- class : component . class ,
40
- staticClass : component . staticClass ,
41
- staticStyle : component . staticStyle ,
42
- style : component . style ,
43
- normalizedStyle : component . normalizedStyle ,
44
- nativeOn : component . nativeOn ,
45
- functional : component . functional
46
+ attrs : componentOptions . attrs ,
47
+ name : componentOptions . name ,
48
+ on : componentOptions . on ,
49
+ key : componentOptions . key ,
50
+ ref : componentOptions . ref ,
51
+ props : componentOptions . props ,
52
+ domProps : componentOptions . domProps ,
53
+ class : componentOptions . class ,
54
+ staticClass : componentOptions . staticClass ,
55
+ staticStyle : componentOptions . staticStyle ,
56
+ style : componentOptions . style ,
57
+ normalizedStyle : componentOptions . normalizedStyle ,
58
+ nativeOn : componentOptions . nativeOn ,
59
+ functional : componentOptions . functional
46
60
}
47
61
}
48
62
function createStubFromString (
@@ -62,24 +76,31 @@ function createStubFromString (
62
76
throwError ( 'options.stub cannot contain a circular reference' )
63
77
}
64
78
79
+ const componentOptions = typeof originalComponent === 'function'
80
+ ? originalComponent . extendOptions
81
+ : originalComponent
82
+
65
83
return {
66
- ...getCoreProperties ( originalComponent ) ,
84
+ ...getCoreProperties ( componentOptions ) ,
67
85
...compileToFunctions ( templateString )
68
86
}
69
87
}
70
88
71
- function createBlankStub ( originalComponent : Component ) {
72
- const name = `${ originalComponent . name } -stub`
89
+ function createBlankStub ( originalComponent : Component , name : string ) {
90
+ const componentOptions = typeof originalComponent === 'function'
91
+ ? originalComponent . extendOptions
92
+ : originalComponent
93
+ const tagName = `${ name } -stub`
73
94
74
95
// ignoreElements does not exist in Vue 2.0.x
75
96
if ( Vue . config . ignoredElements ) {
76
- Vue . config . ignoredElements . push ( name )
97
+ Vue . config . ignoredElements . push ( tagName )
77
98
}
78
99
79
100
return {
80
- ...getCoreProperties ( originalComponent ) ,
101
+ ...getCoreProperties ( componentOptions ) ,
81
102
render ( h ) {
82
- return h ( name )
103
+ return h ( tagName )
83
104
}
84
105
}
85
106
}
@@ -101,7 +122,9 @@ export function createComponentStubs (
101
122
if ( typeof stub !== 'string' ) {
102
123
throwError ( `each item in an options.stubs array must be a ` + `string` )
103
124
}
104
- components [ stub ] = createBlankStub ( { name : stub } )
125
+ const component = resolveComponent ( originalComponents , stub )
126
+
127
+ components [ stub ] = createBlankStub ( component , stub )
105
128
} )
106
129
} else {
107
130
Object . keys ( stubs ) . forEach ( stub => {
@@ -114,7 +137,8 @@ export function createComponentStubs (
114
137
)
115
138
}
116
139
if ( stubs [ stub ] === true ) {
117
- components [ stub ] = createBlankStub ( { name : stub } )
140
+ const component = resolveComponent ( originalComponents , stub )
141
+ components [ stub ] = createBlankStub ( component , stub )
118
142
return
119
143
}
120
144
@@ -162,12 +186,16 @@ export function createComponentStubs (
162
186
163
187
function stubComponents ( components : Object , stubbedComponents : Object ) {
164
188
Object . keys ( components ) . forEach ( component => {
189
+ const cmp = components [ component ]
190
+ const componentOptions = typeof cmp === 'function'
191
+ ? cmp . extendOptions
192
+ : cmp
165
193
// Remove cached constructor
166
- delete components [ component ] . _Ctor
167
- if ( ! components [ component ] . name ) {
168
- components [ component ] . name = component
194
+ delete componentOptions . _Ctor
195
+ if ( ! componentOptions . name ) {
196
+ componentOptions . name = component
169
197
}
170
- stubbedComponents [ component ] = createBlankStub ( components [ component ] )
198
+ stubbedComponents [ component ] = createBlankStub ( componentOptions , component )
171
199
} )
172
200
}
173
201
@@ -178,7 +206,7 @@ export function createComponentStubsForAll (component: Component): Object {
178
206
stubComponents ( component . components , stubbedComponents )
179
207
}
180
208
181
- stubbedComponents [ component . name ] = createBlankStub ( component )
209
+ stubbedComponents [ component . name ] = createBlankStub ( component , component . name )
182
210
183
211
let extended = component . extends
184
212
@@ -204,7 +232,7 @@ export function createComponentStubsForGlobals (instance: Component): Object {
204
232
return
205
233
}
206
234
207
- components [ c ] = createBlankStub ( instance . options . components [ c ] )
235
+ components [ c ] = createBlankStub ( instance . options . components [ c ] , c )
208
236
delete instance . options . components [ c ] . _Ctor
209
237
delete components [ c ] . _Ctor
210
238
} )
0 commit comments