forked from faceyspacey/babel-plugin-dual-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importCss.js
68 lines (56 loc) · 1.66 KB
/
importCss.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
65
66
67
68
/* eslint-disable */
var ADDED = {};
module.exports = function(chunkName) {
var href = getHref(chunkName)
if (!href) {
if (process.env.NODE_ENV === 'development') {
if (typeof window === 'undefined' || !window.__CSS_CHUNKS__) {
console.warn(
'[DUAL-IMPORT] no css chunks hash found at "window.__CSS_CHUNKS__"'
)
return
}
console.warn(
'[DUAL-IMPORT] no chunk, ',
chunkName,
', found in "window.__CSS_CHUNKS__"'
)
}
return
}
if (ADDED[href] === true) {
return Promise.resolve();
}
ADDED[href] = true;
var head = document.getElementsByTagName('head')[0]
var link = document.createElement('link')
link.href = href
link.charset = 'utf-8'
link.type = 'text/css'
link.rel = 'stylesheet'
link.timeout = 30000
return new Promise(function(resolve, reject) {
var timeout
link.onerror = function() {
link.onerror = link.onload = null // avoid mem leaks in IE.
clearTimeout(timeout)
var message = 'could not load css chunk:${chunkName}'
reject(new Error(message))
}
// link.onload doesn't work well enough, but this will handle it
// since images can't load css (this is a popular fix)
var img = document.createElement('img')
img.onerror = function() {
link.onerror = img.onerror = null // avoid mem leaks in IE.
clearTimeout(timeout)
resolve()
}
timeout = setTimeout(link.onerror, link.timeout)
head.appendChild(link)
img.src = href
})
}
function getHref(chunkName) {
if (typeof window === 'undefined' || !window.__CSS_CHUNKS__) return null
return window.__CSS_CHUNKS__[chunkName]
}