1
- const { noop } = require ( '../lib/utils' )
1
+ const { VM_HELPERS , SSR_HELPERS , COMPONENT_BLACKLIST } = require ( './consts' )
2
+ const { Globals, getMergedDefinition, noop } = require ( '../lib/utils' )
3
+
4
+ /* Fake instance creation */
5
+
6
+ function emptyString ( ) {
7
+ return ''
8
+ }
2
9
3
10
const computedPropDef = {
4
11
enumerable : true ,
@@ -7,7 +14,7 @@ const computedPropDef = {
7
14
set : noop ,
8
15
}
9
16
10
- exports . defineComputed = function ( target , key , userDef ) {
17
+ function defineComputed ( target , key , userDef ) {
11
18
if ( typeof userDef === 'function' ) {
12
19
computedPropDef . get = userDef
13
20
computedPropDef . set = noop
@@ -18,6 +25,120 @@ exports.defineComputed = function (target, key, userDef) {
18
25
Object . defineProperty ( target , key , computedPropDef )
19
26
}
20
27
28
+ function resolveScopedSlots ( fns , res ) {
29
+ res = res || { }
30
+ for ( var i = 0 ; i < fns . length ; i ++ ) {
31
+ if ( Array . isArray ( fns [ i ] ) ) {
32
+ resolveScopedSlots ( fns [ i ] , res )
33
+ } else {
34
+ res [ fns [ i ] . key ] = fns [ i ] . fn
35
+ }
36
+ }
37
+ return res
38
+ }
39
+
40
+ function findRouteMatch ( component , route ) {
41
+ for ( const r of route . matched ) {
42
+ for ( const key in r . components ) {
43
+ if ( r . components [ key ] === component ) {
44
+ return r
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ exports . createFakeInstance = function ( options , data , parent , children , context ) {
51
+ const vm = Object . assign (
52
+ { } ,
53
+ data . attrs ,
54
+ data . props ,
55
+ {
56
+ $prefetch : true ,
57
+ $parent : parent ,
58
+ $children : children ,
59
+ $attrs : data . attrs ,
60
+ $props : data . props ,
61
+ $slots : { } ,
62
+ $scopedSlots : { } ,
63
+ $set : Globals . Vue . set ,
64
+ $delete : Globals . Vue . delete ,
65
+ $route : context . route ,
66
+ $store : context . store ,
67
+ $apollo : {
68
+ queries : { } ,
69
+ loading : false ,
70
+ } ,
71
+ $apolloData : {
72
+ loading : false ,
73
+ } ,
74
+ _self : { } ,
75
+ _staticTrees : [ ] ,
76
+ _u : resolveScopedSlots ,
77
+ }
78
+ )
79
+
80
+ // Render and other helpers
81
+ VM_HELPERS . forEach ( helper => vm [ helper ] = noop )
82
+ SSR_HELPERS . forEach ( helper => vm [ helper ] = emptyString )
83
+
84
+ // Scoped slots
85
+ if ( data . scopedSlots ) {
86
+ vm . $scopedSlots = data . scopedSlots
87
+ }
88
+
89
+ // Route props
90
+ if ( context && context . route ) {
91
+ const { route } = context
92
+ const matchedRoute = findRouteMatch ( options , route )
93
+ if ( matchedRoute && matchedRoute . props ) {
94
+ const { props } = matchedRoute
95
+ if ( props === true ) {
96
+ Object . assign ( vm , matchedRoute . params )
97
+ } else if ( typeof props === 'function' ) {
98
+ Object . assign ( vm , props ( matchedRoute ) )
99
+ } else if ( typeof props === 'object' ) {
100
+ Object . assign ( vm , props )
101
+ }
102
+ }
103
+ }
104
+
105
+ // Methods
106
+ const methods = options . methods
107
+ for ( const key in methods ) {
108
+ vm [ key ] = methods [ key ] . bind ( vm )
109
+ }
110
+
111
+ // Computed
112
+ const computed = options . computed
113
+ for ( const key in computed ) {
114
+ defineComputed ( vm , key , computed [ key ] )
115
+ }
116
+
117
+ // Data
118
+ const localData = options . data
119
+ if ( typeof localData === 'function' ) {
120
+ vm . _data = localData . call ( vm , vm )
121
+ } else if ( typeof localData === 'object' ) {
122
+ vm . _data = localData
123
+ } else {
124
+ vm . _data = { }
125
+ }
126
+ vm . $data = vm . _data
127
+ Object . assign ( vm , vm . _data )
128
+
129
+ // Prefetch state
130
+ const prefetch = options . prefetch
131
+ if ( typeof prefetch === 'function' ) {
132
+ Object . assign ( vm , prefetch ( context ) )
133
+ } else if ( typeof prefetch === 'object' ) {
134
+ Object . assign ( vm , prefetch )
135
+ }
136
+
137
+ return vm
138
+ }
139
+
140
+ /* Component resolution */
141
+
21
142
function cached ( fn ) {
22
143
const cache = Object . create ( null )
23
144
return function cachedFn ( str ) {
@@ -40,7 +161,7 @@ function hasOwn (obj, key) {
40
161
return hasOwnProperty . call ( obj , key )
41
162
}
42
163
43
- exports . resolveAsset = function ( assets , id ) {
164
+ function resolveAsset ( assets , id ) {
44
165
if ( typeof id !== 'string' ) return
45
166
46
167
if ( hasOwn ( assets , id ) ) return assets [ id ]
@@ -51,3 +172,25 @@ exports.resolveAsset = function (assets, id) {
51
172
const pascalCaseId = capitalize ( camelCaseId )
52
173
if ( hasOwn ( assets , pascalCaseId ) ) return assets [ pascalCaseId ]
53
174
}
175
+
176
+ exports . resolveComponent = function ( name , options ) {
177
+ return new Promise ( ( resolve ) => {
178
+ if ( options . components ) {
179
+ const component = resolveAsset ( options . components , name )
180
+ if ( component !== undefined ) {
181
+ resolve ( component )
182
+ }
183
+ }
184
+ return resolve ( Globals . Vue . options . components [ name ] )
185
+ } ) . then ( component => {
186
+ if ( component ) {
187
+ component = getMergedDefinition ( component )
188
+ if ( ! component . functional && (
189
+ ! component . name ||
190
+ ! COMPONENT_BLACKLIST . includes ( component . name )
191
+ ) ) {
192
+ return component
193
+ }
194
+ }
195
+ } )
196
+ }
0 commit comments