Skip to content

Commit

Permalink
fix: ensure async modules would wait for cleanup before progressing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pmmmwh authored Aug 29, 2021
1 parent 4569dab commit 5aa7c22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/utils/makeRefreshRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ function makeRefreshRuntimeModule(webpack) {
webpack.Template.indent([
`if (typeof Promise !== 'undefined' && moduleObject.exports instanceof Promise) {`,
webpack.Template.indent([
// Ponyfill `Promise.finally` as it is only part of the spec after ES2018,
// and Webpack's top level await implementation only rely on ES2015 Promises.
'options.module.exports.then(',
// The exports of the module are re-assigned -
// this ensures anything coming after us would wait for `cleanup` to fire.
// This is particularly important because `cleanup` restores the refresh global,
// maintaining consistency for mutable variables like `moduleId`.
// This `.then` clause is a ponyfill of the `Promise.finally` API -
// it is only part of the spec after ES2018,
// but Webpack's top level await implementation support engines down to ES2015.
'options.module.exports = options.module.exports.then(',
webpack.Template.indent([
`${runtimeTemplate.basicFunction('result', [
`${refreshGlobal}.cleanup(options.id);`,
Expand Down
4 changes: 2 additions & 2 deletions test/unit/makeRefreshRuntimeModule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe.skipIf(WEBPACK_VERSION !== 5, 'makeRefreshRuntimeModule', () => {
originalFactory.call(this, moduleObject, moduleExports, webpackRequire);
} finally {
if (typeof Promise !== 'undefined' && moduleObject.exports instanceof Promise) {
options.module.exports.then(
options.module.exports = options.module.exports.then(
function(result) {
__webpack_require__.$Refresh$.cleanup(options.id);
return result;
Expand Down Expand Up @@ -125,7 +125,7 @@ __webpack_require__.$Refresh$ = {
originalFactory.call(this, moduleObject, moduleExports, webpackRequire);
} finally {
if (typeof Promise !== 'undefined' && moduleObject.exports instanceof Promise) {
options.module.exports.then(
options.module.exports = options.module.exports.then(
(result) => {
__webpack_require__.$Refresh$.cleanup(options.id);
return result;
Expand Down

0 comments on commit 5aa7c22

Please sign in to comment.