-
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.
Merge pull request #121 from tjenkinson/add-chunk-retry-support
Add `chunkLoadRetry` option for reloading chunks
- Loading branch information
Showing
10 changed files
with
138 additions
and
7 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
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
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.