forked from webpack-contrib/bundle-loader
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpatch.js
80 lines (64 loc) · 1.74 KB
/
patch.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
69
70
71
72
73
74
75
76
77
78
79
80
patch();
function patch() {
var head = document.querySelector('head');
var ensure = __webpack_require__.e;
var chunks = __webpack_require__.s;
var failures;
__webpack_require__.e = function(chunkId, callback) {
var loaded = false;
var immediate = true;
var handler = function(error) {
if (!callback) return;
callback(__webpack_require__, error);
callback = null;
};
if (!chunks && failures && failures[chunkId]) {
handler(true);
return;
}
ensure(chunkId, function() {
if (loaded) return;
loaded = true;
if (immediate) {
// webpack fires callback immediately if chunk was already loaded
// IE also fires callback immediately if script was already
// in a cache (AppCache counts too)
setTimeout(function() {
handler();
});
} else {
handler();
}
});
// This is |true| if chunk is already loaded and does not need onError call.
// This happens because in such case ensure() is performed in sync way
if (loaded) {
return;
}
immediate = false;
onError(function() {
if (loaded) return;
loaded = true;
if (chunks) {
chunks[chunkId] = void 0;
} else {
failures || (failures = {});
failures[chunkId] = true;
}
handler(true);
});
};
function onError(callback) {
var script = head.lastChild;
if (script.tagName !== 'SCRIPT') {
if (typeof console !== 'undefined' && console.warn) {
console.warn('Script is not a script', script);
}
return;
}
script.onload = script.onerror = function() {
script.onload = script.onerror = null;
setTimeout(callback, 0);
};
};
};