-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
chunkLoadRetry
option for reloading chunks
Supports `true` or an object that can tweak the config a bit further. When `true` it will have 5 attempts with exponential backoff.
- Loading branch information
1 parent
b988273
commit bd59541
Showing
8 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { RuntimeGlobals } = require('webpack') | ||
|
||
const name = 'RetryChunkLoadPlugin' | ||
|
||
class RetryChunkLoadPlugin { | ||
#maxAttempts | ||
#base | ||
#multiplier | ||
|
||
constructor({ maxAttempts = 5, base = 1.8, multiplier = 500 } = {}) { | ||
if (typeof maxAttempts !== 'number' || maxAttempts < 1) { | ||
throw new Error('Invalid `maxAttempts`') | ||
} | ||
|
||
if (typeof base !== 'number' || base < 1) { | ||
throw new Error('Invalid `base`') | ||
} | ||
|
||
if (typeof multiplier !== 'number' || multiplier < 0) { | ||
throw new Error('Invalid `multiplier`') | ||
} | ||
|
||
this.#maxAttempts = maxAttempts | ||
this.#base = base | ||
this.#multiplier = multiplier | ||
} | ||
|
||
apply(compiler) { | ||
compiler.hooks.thisCompilation.tap(name, (compilation) => { | ||
const { mainTemplate, runtimeTemplate } = compilation | ||
mainTemplate.hooks.localVars.tap({ name, stage: 1 }, (source) => { | ||
const script = runtimeTemplate.iife( | ||
'', | ||
` | ||
if (typeof ${RuntimeGlobals.require} !== "undefined") { | ||
var initialEnsureChunk = ${RuntimeGlobals.ensureChunk}; | ||
${RuntimeGlobals.ensureChunk} = function (chunkId) { | ||
var attemptCount = 0; | ||
return new Promise(function (resolve, reject) { | ||
var load = function () { | ||
initialEnsureChunk(chunkId).then((res) => resolve(res)).catch((e) => { | ||
if (++attemptCount >= ${this.#maxAttempts}) { | ||
reject(e); | ||
} else { | ||
setTimeout(() => load(), (${this.#base} ** (attemptCount - 1)) * ${this.#multiplier}) | ||
} | ||
}); | ||
}; | ||
load(); | ||
}); | ||
}; | ||
}` | ||
) | ||
return source + script + ';' | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = RetryChunkLoadPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.