Skip to content

Commit 3738052

Browse files
committed
Use globalThis in cleanup phase instead of window to better support web workers
1 parent a220083 commit 3738052

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/helpers/createRefreshTemplate.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@ const { Template } = require('webpack');
99
*/
1010
const beforeModule = `
1111
let cleanup = function NoOp() {};
12-
13-
if (window && window.$RefreshSetup$) {
14-
cleanup = window.$RefreshSetup$(module.i);
12+
let check = function (it) {
13+
return it && it.Math == Math && it;
14+
};
15+
16+
let safeThis =
17+
check(typeof globalThis == 'object' && globalThis) ||
18+
check(typeof window == 'object' && window) ||
19+
check(typeof self == 'object' && self) ||
20+
check(typeof global == 'object' && global) ||
21+
Function('return this')();
22+
23+
if (safeThis && safeThis.$RefreshSetup$) {
24+
cleanup = safeThis.$RefreshSetup$(module.i);
1525
}
1626
1727
try {

0 commit comments

Comments
 (0)