Skip to content

Commit 9d9dd67

Browse files
latin-1pmmmwh
andauthored
fix: always use the regular function to avoid losing module context (#531)
Co-authored-by: Michael Mok <[email protected]>
1 parent b10f819 commit 9d9dd67

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

lib/utils/makeRefreshRuntimeModule.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,44 @@ function makeRefreshRuntimeModule(webpack) {
2727
`${
2828
runtimeTemplate.supportsConst() ? 'const' : 'var'
2929
} originalFactory = options.factory;`,
30-
`options.factory = ${runtimeTemplate.basicFunction(
31-
'moduleObject, moduleExports, webpackRequire',
32-
[
33-
`${refreshGlobal}.setup(options.id);`,
34-
'try {',
35-
webpack.Template.indent(
36-
'originalFactory.call(this, moduleObject, moduleExports, webpackRequire);'
37-
),
38-
'} finally {',
30+
`options.factory = function (moduleObject, moduleExports, webpackRequire) {`,
31+
webpack.Template.indent([
32+
`${refreshGlobal}.setup(options.id);`,
33+
'try {',
34+
webpack.Template.indent(
35+
'originalFactory.call(this, moduleObject, moduleExports, webpackRequire);'
36+
),
37+
'} finally {',
38+
webpack.Template.indent([
39+
`if (typeof Promise !== 'undefined' && moduleObject.exports instanceof Promise) {`,
3940
webpack.Template.indent([
40-
`if (typeof Promise !== 'undefined' && moduleObject.exports instanceof Promise) {`,
41+
// The exports of the module are re-assigned -
42+
// this ensures anything coming after us would wait for `cleanup` to fire.
43+
// This is particularly important because `cleanup` restores the refresh global,
44+
// maintaining consistency for mutable variables like `moduleId`.
45+
// This `.then` clause is a ponyfill of the `Promise.finally` API -
46+
// it is only part of the spec after ES2018,
47+
// but Webpack's top level await implementation support engines down to ES2015.
48+
'options.module.exports = options.module.exports.then(',
4149
webpack.Template.indent([
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(',
50-
webpack.Template.indent([
51-
`${runtimeTemplate.basicFunction('result', [
52-
`${refreshGlobal}.cleanup(options.id);`,
53-
'return result;',
54-
])},`,
55-
runtimeTemplate.basicFunction('reason', [
56-
`${refreshGlobal}.cleanup(options.id);`,
57-
'return Promise.reject(reason);',
58-
]),
50+
`${runtimeTemplate.basicFunction('result', [
51+
`${refreshGlobal}.cleanup(options.id);`,
52+
'return result;',
53+
])},`,
54+
runtimeTemplate.basicFunction('reason', [
55+
`${refreshGlobal}.cleanup(options.id);`,
56+
'return Promise.reject(reason);',
5957
]),
60-
`);`,
6158
]),
62-
'} else {',
63-
webpack.Template.indent(`${refreshGlobal}.cleanup(options.id)`),
64-
'}',
59+
`);`,
6560
]),
61+
'} else {',
62+
webpack.Template.indent(`${refreshGlobal}.cleanup(options.id)`),
6663
'}',
67-
]
68-
)}`,
64+
]),
65+
'}',
66+
]),
67+
`};`,
6968
]
7069
)})`,
7170
'',

test/unit/makeRefreshRuntimeModule.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe.skipIf(WEBPACK_VERSION !== 5, 'makeRefreshRuntimeModule', () => {
4040
expect(runtime).toMatchInlineSnapshot(`
4141
"__webpack_require__.i.push(function(options) {
4242
var originalFactory = options.factory;
43-
options.factory = function(moduleObject, moduleExports, webpackRequire) {
43+
options.factory = function (moduleObject, moduleExports, webpackRequire) {
4444
__webpack_require__.$Refresh$.setup(options.id);
4545
try {
4646
originalFactory.call(this, moduleObject, moduleExports, webpackRequire);
@@ -60,7 +60,7 @@ describe.skipIf(WEBPACK_VERSION !== 5, 'makeRefreshRuntimeModule', () => {
6060
__webpack_require__.$Refresh$.cleanup(options.id)
6161
}
6262
}
63-
}
63+
};
6464
})
6565
6666
__webpack_require__.$Refresh$ = {
@@ -119,7 +119,7 @@ __webpack_require__.$Refresh$ = {
119119
expect(runtime).toMatchInlineSnapshot(`
120120
"__webpack_require__.i.push((options) => {
121121
const originalFactory = options.factory;
122-
options.factory = (moduleObject, moduleExports, webpackRequire) => {
122+
options.factory = function (moduleObject, moduleExports, webpackRequire) {
123123
__webpack_require__.$Refresh$.setup(options.id);
124124
try {
125125
originalFactory.call(this, moduleObject, moduleExports, webpackRequire);
@@ -139,7 +139,7 @@ __webpack_require__.$Refresh$ = {
139139
__webpack_require__.$Refresh$.cleanup(options.id)
140140
}
141141
}
142-
}
142+
};
143143
})
144144
145145
__webpack_require__.$Refresh$ = {

0 commit comments

Comments
 (0)