Skip to content

Commit 5aa7c22

Browse files
authored
fix: ensure async modules would wait for cleanup before progressing (#493)
1 parent 4569dab commit 5aa7c22

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/utils/makeRefreshRuntimeModule.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ function makeRefreshRuntimeModule(webpack) {
3939
webpack.Template.indent([
4040
`if (typeof Promise !== 'undefined' && moduleObject.exports instanceof Promise) {`,
4141
webpack.Template.indent([
42-
// Ponyfill `Promise.finally` as it is only part of the spec after ES2018,
43-
// and Webpack's top level await implementation only rely on ES2015 Promises.
44-
'options.module.exports.then(',
42+
// The exports of the module are re-assigned -
43+
// this ensures anything coming after us would wait for `cleanup` to fire.
44+
// This is particularly important because `cleanup` restores the refresh global,
45+
// maintaining consistency for mutable variables like `moduleId`.
46+
// This `.then` clause is a ponyfill of the `Promise.finally` API -
47+
// it is only part of the spec after ES2018,
48+
// but Webpack's top level await implementation support engines down to ES2015.
49+
'options.module.exports = options.module.exports.then(',
4550
webpack.Template.indent([
4651
`${runtimeTemplate.basicFunction('result', [
4752
`${refreshGlobal}.cleanup(options.id);`,

test/unit/makeRefreshRuntimeModule.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe.skipIf(WEBPACK_VERSION !== 5, 'makeRefreshRuntimeModule', () => {
4646
originalFactory.call(this, moduleObject, moduleExports, webpackRequire);
4747
} finally {
4848
if (typeof Promise !== 'undefined' && moduleObject.exports instanceof Promise) {
49-
options.module.exports.then(
49+
options.module.exports = options.module.exports.then(
5050
function(result) {
5151
__webpack_require__.$Refresh$.cleanup(options.id);
5252
return result;
@@ -125,7 +125,7 @@ __webpack_require__.$Refresh$ = {
125125
originalFactory.call(this, moduleObject, moduleExports, webpackRequire);
126126
} finally {
127127
if (typeof Promise !== 'undefined' && moduleObject.exports instanceof Promise) {
128-
options.module.exports.then(
128+
options.module.exports = options.module.exports.then(
129129
(result) => {
130130
__webpack_require__.$Refresh$.cleanup(options.id);
131131
return result;

0 commit comments

Comments
 (0)