Skip to content

Commit fc3d14b

Browse files
committed
update tests
1 parent 2c9de97 commit fc3d14b

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

loader/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const RefreshRuntimePath = require
2020
.replace(/\\/g, '/')
2121
.replace(/'/g, "\\'");
2222

23+
// When available, use `__webpack_global__` which is a stable runtime function to use `__webpack_require__` in this compilation
24+
// See: https://github.com/webpack/webpack/issues/20139
25+
const RefreshGlobals = `(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__)`;
26+
2327
/**
2428
* A simple Webpack loader to inject react-refresh HMR code into modules.
2529
*
@@ -41,12 +45,7 @@ function ReactRefreshLoader(source, inputSourceMap, meta) {
4145

4246
const callback = this.async();
4347

44-
const { ModuleFilenameHelpers, Template, RuntimeGlobals } =
45-
this._compiler.webpack || require('webpack');
46-
47-
// When available, use `__webpack_global__` which is a stable runtime function to use `__webpack_require__` in this compilation
48-
// See: https://github.com/webpack/webpack/issues/20139
49-
const RefreshGlobals = `(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__)`;
48+
const { ModuleFilenameHelpers, Template } = this._compiler.webpack || require('webpack');
5049

5150
const RefreshSetupRuntimes = {
5251
cjs: Template.asString(
@@ -105,7 +104,10 @@ function ReactRefreshLoader(source, inputSourceMap, meta) {
105104
);
106105
}
107106

108-
module.exports = ReactRefreshLoader;
107+
module.exports = {
108+
ReactRefreshLoader,
109+
RefreshGlobals,
110+
};
109111

110112
// Restore the original value of the `fetch` global, if it exists
111113
if (originalFetch) {

test/loader/loader.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ describe('loader', () => {
1616
const { execution, parsed } = compilation.module;
1717

1818
expect(parsed).toMatchInlineSnapshot(`
19-
"__webpack_require__.$Refresh$.runtime = require('react-refresh');
19+
"(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.runtime = require('react-refresh');
2020
2121
module.exports = 'Test';
2222
2323
24-
var $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;
24+
var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
2525
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
2626
$ReactRefreshModuleId$
2727
);
@@ -61,12 +61,12 @@ describe('loader', () => {
6161
\\******************/
6262
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
6363
64-
__webpack_require__.$Refresh$.runtime = __webpack_require__(/*! react-refresh */ "../../../../node_modules/react-refresh/runtime.js");
64+
(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.runtime = __webpack_require__(/*! react-refresh */ "../../../../node_modules/react-refresh/runtime.js");
6565
6666
module.exports = 'Test';
6767
6868
69-
var $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;
69+
var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
7070
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
7171
$ReactRefreshModuleId$
7272
);
@@ -118,12 +118,12 @@ describe('loader', () => {
118118

119119
expect(parsed).toMatchInlineSnapshot(`
120120
"import * as __react_refresh_runtime__ from 'react-refresh';
121-
__webpack_require__.$Refresh$.runtime = __react_refresh_runtime__;
121+
(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.runtime = __react_refresh_runtime__;
122122
123123
export default 'Test';
124124
125125
126-
var $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;
126+
var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
127127
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
128128
$ReactRefreshModuleId$
129129
);
@@ -171,12 +171,12 @@ describe('loader', () => {
171171
/* harmony export */ });
172172
/* harmony import */ var react_refresh__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react-refresh */ "../../../../node_modules/react-refresh/runtime.js");
173173
174-
__webpack_require__.$Refresh$.runtime = /*#__PURE__*/ (react_refresh__WEBPACK_IMPORTED_MODULE_0___namespace_cache || (react_refresh__WEBPACK_IMPORTED_MODULE_0___namespace_cache = __webpack_require__.t(react_refresh__WEBPACK_IMPORTED_MODULE_0__, 2)));
174+
(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.runtime = /*#__PURE__*/ (react_refresh__WEBPACK_IMPORTED_MODULE_0___namespace_cache || (react_refresh__WEBPACK_IMPORTED_MODULE_0___namespace_cache = __webpack_require__.t(react_refresh__WEBPACK_IMPORTED_MODULE_0__, 2)));
175175
176176
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ('Test');
177177
178178
179-
var $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;
179+
var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
180180
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
181181
$ReactRefreshModuleId$
182182
);

test/loader/unit/getRefreshModuleRuntime.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const { Template } = require('webpack');
22
const getRefreshModuleRuntime = require('../../../loader/utils/getRefreshModuleRuntime');
3+
const refreshGlobals = require('../../../loader').RefreshGlobals;
34

45
describe('getRefreshModuleRuntime', () => {
56
it('should return working refresh module runtime without const using CommonJS', () => {
67
const refreshModuleRuntime = getRefreshModuleRuntime(Template, {
78
const: false,
89
moduleSystem: 'cjs',
10+
refreshGlobals,
911
});
1012

1113
expect(refreshModuleRuntime.indexOf('var')).not.toBe(-1);
@@ -14,7 +16,7 @@ describe('getRefreshModuleRuntime', () => {
1416
expect(refreshModuleRuntime.indexOf('module.hot')).not.toBe(-1);
1517
expect(refreshModuleRuntime.indexOf('import.meta.webpackHot')).toBe(-1);
1618
expect(refreshModuleRuntime).toMatchInlineSnapshot(`
17-
"var $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;
19+
"var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
1820
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
1921
$ReactRefreshModuleId$
2022
);
@@ -51,6 +53,7 @@ describe('getRefreshModuleRuntime', () => {
5153
const refreshModuleRuntime = getRefreshModuleRuntime(Template, {
5254
const: true,
5355
moduleSystem: 'cjs',
56+
refreshGlobals,
5457
});
5558

5659
expect(refreshModuleRuntime.indexOf('var')).toBe(-1);
@@ -59,7 +62,7 @@ describe('getRefreshModuleRuntime', () => {
5962
expect(refreshModuleRuntime.indexOf('module.hot')).not.toBe(-1);
6063
expect(refreshModuleRuntime.indexOf('import.meta.webpackHot')).toBe(-1);
6164
expect(refreshModuleRuntime).toMatchInlineSnapshot(`
62-
"const $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;
65+
"const $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
6366
const $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
6467
$ReactRefreshModuleId$
6568
);
@@ -96,6 +99,7 @@ describe('getRefreshModuleRuntime', () => {
9699
const refreshModuleRuntime = getRefreshModuleRuntime(Template, {
97100
const: false,
98101
moduleSystem: 'esm',
102+
refreshGlobals,
99103
});
100104

101105
expect(refreshModuleRuntime.indexOf('var')).not.toBe(-1);
@@ -104,7 +108,7 @@ describe('getRefreshModuleRuntime', () => {
104108
expect(refreshModuleRuntime.indexOf('module.hot')).toBe(-1);
105109
expect(refreshModuleRuntime.indexOf('import.meta.webpackHot')).not.toBe(-1);
106110
expect(refreshModuleRuntime).toMatchInlineSnapshot(`
107-
"var $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;
111+
"var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
108112
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
109113
$ReactRefreshModuleId$
110114
);
@@ -141,6 +145,7 @@ describe('getRefreshModuleRuntime', () => {
141145
const refreshModuleRuntime = getRefreshModuleRuntime(Template, {
142146
const: true,
143147
moduleSystem: 'esm',
148+
refreshGlobals,
144149
});
145150

146151
expect(refreshModuleRuntime.indexOf('var')).toBe(-1);
@@ -149,7 +154,7 @@ describe('getRefreshModuleRuntime', () => {
149154
expect(refreshModuleRuntime.indexOf('module.hot')).toBe(-1);
150155
expect(refreshModuleRuntime.indexOf('import.meta.webpackHot')).not.toBe(-1);
151156
expect(refreshModuleRuntime).toMatchInlineSnapshot(`
152-
"const $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;
157+
"const $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
153158
const $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
154159
$ReactRefreshModuleId$
155160
);

0 commit comments

Comments
 (0)