Skip to content

Commit 4091b33

Browse files
author
Guillaume Chau
committed
refactor: move fake instance creation to utils file
1 parent 9ba5e77 commit 4091b33

File tree

2 files changed

+148
-144
lines changed

2 files changed

+148
-144
lines changed

Diff for: ssr/index.js

+2-141
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
const chalk = require('chalk')
22
const { VUE_APOLLO_QUERY_KEYWORDS } = require('../lib/consts')
3-
const { VM_HELPERS, SSR_HELPERS, COMPONENT_BLACKLIST } = require('./consts')
4-
const { resolveAsset, defineComputed } = require('./utils')
5-
const { Globals, getMergedDefinition, omit, noop } = require('../lib/utils')
6-
7-
function emptyString () {
8-
return ''
9-
}
3+
const { createFakeInstance, resolveComponent } = require('./utils')
4+
const { Globals, getMergedDefinition, omit } = require('../lib/utils')
105

116
exports.install = function (Vue) {
127
Globals.Vue = Vue
@@ -99,140 +94,6 @@ function prefetchComponent (component, vm, queries) {
9994
}
10095
}
10196

102-
function createFakeInstance (options, data, parent, children, context) {
103-
const vm = Object.assign(
104-
{},
105-
data.attrs,
106-
data.props,
107-
{
108-
$prefetch: true,
109-
$parent: parent,
110-
$children: children,
111-
$attrs: data.attrs,
112-
$props: data.props,
113-
$slots: {},
114-
$scopedSlots: {},
115-
$set: Globals.Vue.set,
116-
$delete: Globals.Vue.delete,
117-
$route: context.route,
118-
$store: context.store,
119-
$apollo: {
120-
queries: {},
121-
loading: false,
122-
},
123-
$apolloData: {
124-
loading: false,
125-
},
126-
_self: {},
127-
_staticTrees: [],
128-
_u: resolveScopedSlots,
129-
}
130-
)
131-
132-
// Render and other helpers
133-
VM_HELPERS.forEach(helper => vm[helper] = noop)
134-
SSR_HELPERS.forEach(helper => vm[helper] = emptyString)
135-
136-
// Scoped slots
137-
if (data.scopedSlots) {
138-
vm.$scopedSlots = data.scopedSlots
139-
}
140-
141-
// Route props
142-
if (context && context.route) {
143-
const { route } = context
144-
const matchedRoute = findRouteMatch(options, route)
145-
if (matchedRoute && matchedRoute.props) {
146-
const { props } = matchedRoute
147-
if (props === true) {
148-
Object.assign(vm, matchedRoute.params)
149-
} else if (typeof props === 'function') {
150-
Object.assign(vm, props(matchedRoute))
151-
} else if (typeof props === 'object') {
152-
Object.assign(vm, props)
153-
}
154-
}
155-
}
156-
157-
// Methods
158-
const methods = options.methods
159-
for (const key in methods) {
160-
vm[key] = methods[key].bind(vm)
161-
}
162-
163-
// Computed
164-
const computed = options.computed
165-
for (const key in computed) {
166-
defineComputed(vm, key, computed[key])
167-
}
168-
169-
// Data
170-
const localData = options.data
171-
if (typeof localData === 'function') {
172-
vm._data = localData.call(vm, vm)
173-
} else if (typeof localData === 'object') {
174-
vm._data = localData
175-
} else {
176-
vm._data = {}
177-
}
178-
vm.$data = vm._data
179-
Object.assign(vm, vm._data)
180-
181-
// Prefetch state
182-
const prefetch = options.prefetch
183-
if (typeof prefetch === 'function') {
184-
Object.assign(vm, prefetch(context))
185-
} else if (typeof prefetch === 'object') {
186-
Object.assign(vm, prefetch)
187-
}
188-
189-
return vm
190-
}
191-
192-
function findRouteMatch (component, route) {
193-
for (const r of route.matched) {
194-
for (const key in r.components) {
195-
if (r.components[key] === component) {
196-
return r
197-
}
198-
}
199-
}
200-
}
201-
202-
function resolveComponent (name, options) {
203-
return new Promise((resolve) => {
204-
if (options.components) {
205-
const component = resolveAsset(options.components, name)
206-
if (component !== undefined) {
207-
resolve(component)
208-
}
209-
}
210-
return resolve(Globals.Vue.options.components[name])
211-
}).then(component => {
212-
if (component) {
213-
component = getMergedDefinition(component)
214-
if (!component.functional && (
215-
!component.name ||
216-
!COMPONENT_BLACKLIST.includes(component.name)
217-
)) {
218-
return component
219-
}
220-
}
221-
})
222-
}
223-
224-
function resolveScopedSlots (fns, res) {
225-
res = res || {}
226-
for (var i = 0; i < fns.length; i++) {
227-
if (Array.isArray(fns[i])) {
228-
resolveScopedSlots(fns[i], res)
229-
} else {
230-
res[fns[i].key] = fns[i].fn
231-
}
232-
}
233-
return res
234-
}
235-
23697
function prefetchQuery (apolloProvider, query, context) {
23798
try {
23899
let variables

Diff for: ssr/utils.js

+146-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
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+
}
29

310
const computedPropDef = {
411
enumerable: true,
@@ -7,7 +14,7 @@ const computedPropDef = {
714
set: noop,
815
}
916

10-
exports.defineComputed = function (target, key, userDef) {
17+
function defineComputed (target, key, userDef) {
1118
if (typeof userDef === 'function') {
1219
computedPropDef.get = userDef
1320
computedPropDef.set = noop
@@ -18,6 +25,120 @@ exports.defineComputed = function (target, key, userDef) {
1825
Object.defineProperty(target, key, computedPropDef)
1926
}
2027

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+
21142
function cached (fn) {
22143
const cache = Object.create(null)
23144
return function cachedFn (str) {
@@ -40,7 +161,7 @@ function hasOwn (obj, key) {
40161
return hasOwnProperty.call(obj, key)
41162
}
42163

43-
exports.resolveAsset = function (assets, id) {
164+
function resolveAsset (assets, id) {
44165
if (typeof id !== 'string') return
45166

46167
if (hasOwn(assets, id)) return assets[id]
@@ -51,3 +172,25 @@ exports.resolveAsset = function (assets, id) {
51172
const pascalCaseId = capitalize(camelCaseId)
52173
if (hasOwn(assets, pascalCaseId)) return assets[pascalCaseId]
53174
}
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

Comments
 (0)