-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_manager.js
53 lines (45 loc) · 1.31 KB
/
component_manager.js
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
const ALREADY_REGISTERED = ['composition', 'MDText', 'sitemap']
function addJSSuffix(component) {
return component.match(/.*.js/) ? component : `${component}.js`
}
export default (siteconf) => {
const _buf = {}
const _promises = {}
function _load (name) {
const url = name.match(/http*./) ? name
: addJSSuffix(siteconf.componentsUrl + name)
_promises[name] = import(url).then(module => {
_buf[name] = module
delete _promises[name]
return module
})
return _promises[name]
}
function load (url) {
if (url in _buf) return new Promise(resolve => resolve(_buf[url]))
return url in _promises ? _promises[url] : _load(url)
}
function loadfn (url) {
return () => load(url)
}
function prepareComponents(children) {
const active = _.filter(children, i => !i.disabled)
return _.map(active, i => {
if (i.component === 'composition') {
i.children = prepareComponents(i.children)
} else if (!_.contains(ALREADY_REGISTERED, i.component)) {
i.component = loadfn(i.component)
}
return i
})
}
// register global components
_.map(siteconf.globalComponents || {}, async (url, name) => {
const Component = await load(url)
Vue.component(name, Component.default)
})
return {
load,
prepareComponents
}
}