Skip to content

Commit

Permalink
formatting/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
seleb committed Aug 7, 2021
1 parent 709bfa0 commit 342dcb4
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ 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');
return [source, functionBody(addProgressVars)].join('\n');
});
compilation.mainTemplate.hooks.requireEnsure.tap('replace-require-ensure', function () {
return functionBody(replaceRequireEnsure);
Expand All @@ -19,7 +16,7 @@ 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("}"));
return str.slice(str.indexOf('{') + 1, str.lastIndexOf('}'));
}

function addProgressVars() {
Expand All @@ -37,28 +34,27 @@ function addProgressVars() {

function replaceRequireEnsure(chunkId) {
// chunk-progress-webpack-plugin replace-require-ensure start
if (installedChunkData !== 0) { // 0 means "already installed".
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(function (resolve, reject) {
installedChunkData = installedChunks[chunkId] = [resolve, reject];
});
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;
promises.push(installedChunkData[2] = promise);

var timeout = setTimeout(function () {
onScriptComplete({
type: 'timeout',
target: script
target: script,
});
}, 120000);

Expand All @@ -81,8 +77,8 @@ function replaceRequireEnsure(chunkId) {
return num + sum;
});
progress.activeLoads[url] = progressEvent.loaded;
document.dispatchEvent(new CustomEvent(
'chunk-progress-webpack-plugin', {
document.dispatchEvent(
new CustomEvent('chunk-progress-webpack-plugin', {
detail: {
originalEvent: progressEvent,
originalRequestEvent: requestEvent,
Expand All @@ -91,10 +87,11 @@ function replaceRequireEnsure(chunkId) {
resource: {
url: url,
loaded: progressEvent.loaded,
total: progressEvent.total
}
}
}));
total: progressEvent.total,
},
},
})
);
};
xhr.send();
});
Expand All @@ -112,7 +109,7 @@ function replaceRequireEnsure(chunkId) {
onScriptComplete();
})
.catch(function (error) {
onScriptComplete(error)
onScriptComplete(error);
});

function onScriptComplete(event) {
Expand Down

0 comments on commit 342dcb4

Please sign in to comment.