-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.js
64 lines (54 loc) · 1.84 KB
/
loader.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
54
55
56
57
58
59
60
61
62
63
64
/* global makeSznSelectBundleScript */
'use strict'
;(() => {
const LOADER_ATTRIBUTE_PREFIX = 'data-szn-select--loader-urls--'
const SUPPORTED_LOADER_ATTRIBUTES = [
'package',
'es2016',
'es3',
'bundle-elements-es2016',
'bundle-elements-es3',
'bundle-full-ce',
'bundle-full-es2016',
'bundle-full-es3',
]
// %{EMBEDDABLE_LOADER}%
const loaderScript = getLoaderScript()
const urlsConfiguration = loadUrlsConfig(loaderScript)
const script = makeSznSelectBundleScript(urlsConfiguration, loaderScript.async)
if (!script) {
return
}
// TODO: do not use document.write if the hosts do not match: https://www.chromestatus.com/feature/5718547946799104
if (['interactive', 'complete'].indexOf(document.readyState) === -1) {
document.write(script.outerHTML)
} else {
(document.head || document.querySelector('head')).appendChild(script) // document.head is supported since IE9
}
function loadUrlsConfig(loaderScriptElement) {
const urls = {}
const attributes = loaderScriptElement.attributes
for (let i = attributes.length - 1; i >= 0; i--) {
const attribute = attributes[i]
if (!/^data-szn-select--loader-urls--/.test(attribute.name)) {
continue
}
const optionNameParts = attribute.name.substring(30).split('-')
const optionName = optionNameParts.length > 1 ?
`${optionNameParts.slice(0, -1).join('-')}.${optionNameParts[optionNameParts.length - 1]}`
:
optionNameParts[0]
urls[optionName] = attribute.value
}
return urls
}
function getLoaderScript() {
return (
document.currentScript ||
document.querySelector(
`[${LOADER_ATTRIBUTE_PREFIX}${SUPPORTED_LOADER_ATTRIBUTES.join(`],[${LOADER_ATTRIBUTE_PREFIX}`)}]`,
) ||
{attributes: []} // there is no overriding configuration
)
}
})()