@@ -14,19 +14,19 @@ import {
14
14
} from './validators'
15
15
import { compileTemplate } from './compile-template'
16
16
17
- function isVueComponent ( comp ) {
17
+ function isVueComponent ( comp ) : boolean {
18
18
return comp && ( comp . render || comp . template || comp . options )
19
19
}
20
20
21
- function isValidStub ( stub : any ) {
21
+ function isValidStub ( stub : any ) : boolean {
22
22
return (
23
23
( ! ! stub && typeof stub === 'string' ) ||
24
24
stub === true ||
25
25
isVueComponent ( stub )
26
26
)
27
27
}
28
28
29
- function resolveComponent ( obj , component ) {
29
+ function resolveComponent ( obj : Object , component : string ) : Object {
30
30
return obj [ component ] ||
31
31
obj [ hyphenate ( component ) ] ||
32
32
obj [ camelize ( component ) ] ||
@@ -35,7 +35,7 @@ function resolveComponent (obj, component) {
35
35
{ }
36
36
}
37
37
38
- function isRequiredComponent ( name ) {
38
+ function isRequiredComponent ( name ) : boolean {
39
39
return (
40
40
name === 'KeepAlive' || name === 'Transition' || name === 'TransitionGroup'
41
41
)
@@ -59,11 +59,12 @@ function getCoreProperties (componentOptions: Component): Object {
59
59
functional : componentOptions . functional
60
60
}
61
61
}
62
+
62
63
function createStubFromString (
63
64
templateString : string ,
64
65
originalComponent : Component ,
65
66
name : string
66
- ) : Object {
67
+ ) : Component {
67
68
if ( ! compileToFunctions ) {
68
69
throwError (
69
70
`vueTemplateCompiler is undefined, you must pass ` +
@@ -86,7 +87,10 @@ function createStubFromString (
86
87
}
87
88
}
88
89
89
- function createBlankStub ( originalComponent : Component , name : string ) {
90
+ function createBlankStub (
91
+ originalComponent : Component ,
92
+ name : string
93
+ ) : Component {
90
94
const componentOptions = typeof originalComponent === 'function'
91
95
? originalComponent . extendOptions
92
96
: originalComponent
@@ -107,8 +111,8 @@ function createBlankStub (originalComponent: Component, name: string) {
107
111
108
112
export function createComponentStubs (
109
113
originalComponents : Object = { } ,
110
- stubs : Object
111
- ) : Object {
114
+ stubs : Stubs
115
+ ) : Components {
112
116
const components = { }
113
117
if ( ! stubs ) {
114
118
return components
@@ -127,55 +131,61 @@ export function createComponentStubs (
127
131
components [ stub ] = createBlankStub ( component , stub )
128
132
} )
129
133
} else {
130
- Object . keys ( stubs ) . forEach ( stub => {
131
- if ( stubs [ stub ] === false ) {
134
+ const stubsObject = ( stubs : { [ name : string ] : Component | string | true } )
135
+ Object . keys ( stubsObject ) . forEach ( stubName => {
136
+ const stub = stubsObject [ stubName ]
137
+ if ( stub === false ) {
132
138
return
133
139
}
134
- if ( ! isValidStub ( stubs [ stub ] ) ) {
140
+
141
+ if ( ! isValidStub ( stub ) ) {
135
142
throwError (
136
143
`options.stub values must be passed a string or ` + `component`
137
144
)
138
145
}
139
- if ( stubs [ stub ] === true ) {
140
- const component = resolveComponent ( originalComponents , stub )
141
- components [ stub ] = createBlankStub ( component , stub )
146
+
147
+ if ( stub === true ) {
148
+ const component = resolveComponent ( originalComponents , stubName )
149
+ components [ stubName ] = createBlankStub ( component , stubName )
142
150
return
143
151
}
144
152
145
- if ( componentNeedsCompiling ( stubs [ stub ] ) ) {
146
- compileTemplate ( stubs [ stub ] )
153
+ if ( typeof stub !== 'string' && componentNeedsCompiling ( stub ) ) {
154
+ compileTemplate ( stub )
147
155
}
148
156
149
- if ( originalComponents [ stub ] ) {
157
+ if ( originalComponents [ stubName ] ) {
150
158
// Remove cached constructor
151
- delete originalComponents [ stub ] . _Ctor
152
- if ( typeof stubs [ stub ] === 'string' ) {
153
- components [ stub ] = createStubFromString (
154
- stubs [ stub ] ,
155
- originalComponents [ stub ] ,
156
- stub
159
+ delete originalComponents [ stubName ] . _Ctor
160
+ if ( typeof stub === 'string' ) {
161
+ components [ stubName ] = createStubFromString (
162
+ stub ,
163
+ originalComponents [ stubName ] ,
164
+ stubName
157
165
)
158
166
} else {
159
- components [ stub ] = {
160
- ...stubs [ stub ] ,
161
- name : originalComponents [ stub ] . name
167
+ const stubObject = ( stub : Object )
168
+ components [ stubName ] = {
169
+ ...stubObject ,
170
+ name : originalComponents [ stubName ] . name
162
171
}
163
172
}
164
173
} else {
165
- if ( typeof stubs [ stub ] === 'string' ) {
174
+ if ( typeof stub === 'string' ) {
166
175
if ( ! compileToFunctions ) {
167
176
throwError (
168
177
`vueTemplateCompiler is undefined, you must pass ` +
169
178
`precompiled components if vue-template-compiler is ` +
170
179
`undefined`
171
180
)
172
181
}
173
- components [ stub ] = {
174
- ...compileToFunctions ( stubs [ stub ] )
182
+ components [ stubName ] = {
183
+ ...compileToFunctions ( stub )
175
184
}
176
185
} else {
177
- components [ stub ] = {
178
- ...stubs [ stub ]
186
+ const stubObject = ( stub : Object )
187
+ components [ stubName ] = {
188
+ ...stubObject
179
189
}
180
190
}
181
191
}
@@ -184,7 +194,10 @@ export function createComponentStubs (
184
194
return components
185
195
}
186
196
187
- function stubComponents ( components : Object , stubbedComponents : Object ) {
197
+ function stubComponents (
198
+ components : Components ,
199
+ stubbedComponents : Components
200
+ ) : void {
188
201
Object . keys ( components ) . forEach ( component => {
189
202
const cmp = components [ component ]
190
203
const componentOptions = typeof cmp === 'function'
@@ -199,7 +212,7 @@ function stubComponents (components: Object, stubbedComponents: Object) {
199
212
} )
200
213
}
201
214
202
- export function createComponentStubsForAll ( component : Component ) : Object {
215
+ export function createComponentStubsForAll ( component : Component ) : Components {
203
216
const stubbedComponents = { }
204
217
205
218
if ( component . components ) {
@@ -225,7 +238,9 @@ export function createComponentStubsForAll (component: Component): Object {
225
238
return stubbedComponents
226
239
}
227
240
228
- export function createComponentStubsForGlobals ( instance : Component ) : Object {
241
+ export function createComponentStubsForGlobals (
242
+ instance : Component
243
+ ) : Components {
229
244
const components = { }
230
245
Object . keys ( instance . options . components ) . forEach ( c => {
231
246
if ( isRequiredComponent ( c ) ) {
0 commit comments