-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
145 lines (137 loc) · 4.37 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class ChunkProgressWebpackPlugin {
apply(compiler) {
compiler.hooks.compilation.tap('chunk-progress-webpack-plugin', function (compilation) {
compilation.mainTemplate.hooks.localVars.tap('add-progress-vars', function (source) {
return [source, functionBody(addProgressVars)].join('\n');
});
compilation.mainTemplate.hooks.requireEnsure.tap('replace-require-ensure', function () {
return functionBody(replaceRequireEnsure);
});
});
}
}
module.exports = ChunkProgressWebpackPlugin;
// helper for returning contents of a function
function functionBody(fn) {
const str = fn.toString();
return str.slice(str.indexOf('{') + 1, str.lastIndexOf('}'));
}
function addProgressVars() {
// chunk-progress-webpack-plugin add-progress-vars start
var progress = {
totalSize: 0,
activeLoadCount: 0,
activeLoads: {},
};
var installedChunks = {
main: 0,
};
// chunk-progress-webpack-plugin add-progress-vars end
}
function replaceRequireEnsure(chunkId) {
// chunk-progress-webpack-plugin replace-require-ensure start
var installedChunkData = __webpack_require__.o(installedChunks, chunkId) ? installedChunks[chunkId] : undefined;
if (installedChunkData !== 0) {
// 0 means "already installed".
if (installedChunkData) {
promises.push(installedChunkData[2]);
} else {
// setup Promise in chunk cache
var promise = new Promise((resolve, reject) => (installedChunkData = installedChunks[chunkId] = [resolve, reject]));
promises.push((installedChunkData[2] = promise));
// start chunk loading
var url = __webpack_require__.p + __webpack_require__.u(chunkId);
// create error before stack unwound to get useful stacktrace later
var error = new Error();
progress.activeLoads[url] = 0;
progress.activeLoadCount += 1;
var timeout = setTimeout(function () {
onScriptComplete({
type: 'timeout',
target: script,
});
}, 120000);
new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', url);
xhr.onload = resolve;
xhr.onerror = reject;
xhr.send();
})
.then(function (requestEvent) {
progress.totalSize += parseInt(requestEvent.target.getResponseHeader('content-length'), 10);
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = resolve;
xhr.onerror = reject;
xhr.onprogress = function (progressEvent) {
var loaded = Object.values(progress.activeLoads).reduce(function (sum, num) {
return num + sum;
});
progress.activeLoads[url] = progressEvent.loaded;
document.dispatchEvent(
new CustomEvent('chunk-progress-webpack-plugin', {
detail: {
originalEvent: progressEvent,
originalRequestEvent: requestEvent,
loaded: loaded,
total: progress.totalSize,
resource: {
url: url,
loaded: progressEvent.loaded,
total: progressEvent.total,
},
},
})
);
};
xhr.send();
});
})
.then(function (event) {
return event.target.responseText;
})
.then(function (js) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.textContent = js;
head.appendChild(script);
})
.then(function () {
onScriptComplete();
})
.catch(function (error) {
onScriptComplete(error);
});
function onScriptComplete(event) {
progress.activeLoadCount -= 1;
if (progress.activeLoadCount <= 0) {
progress.totalSize = 0;
progress.activeLoadCount = 0;
progress.activeLoads = {};
}
clearTimeout(timeout);
var chunk = installedChunks[chunkId];
if (!event) {
chunk[0]();
return;
}
if (__webpack_require__.o(installedChunks, chunkId)) {
if (chunk !== 0) installedChunks[chunkId] = undefined;
if (chunk) {
var errorType = event && (event.type === 'load' ? 'missing' : event.type);
var realSrc = event && event.target && event.target.src;
error.message = 'Loading chunk ' + chunkId + ' failed.\n(' + errorType + ': ' + realSrc + ')';
error.name = 'ChunkLoadError';
error.type = errorType;
error.request = realSrc;
chunk[1](error);
}
installedChunks[chunkId] = undefined;
}
}
}
}
// chunk-progress-webpack-plugin replace-require-ensure end
}