forked from pmmmwh/react-refresh-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.test.js
More file actions
317 lines (268 loc) · 10.9 KB
/
loader.test.js
File metadata and controls
317 lines (268 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const validate = require('sourcemap-validator');
const mockFetch = require('../mocks/fetch');
describe('loader', () => {
let getCompilation;
beforeEach(() => {
jest.isolateModules(() => {
getCompilation = require('../helpers/compilation');
});
});
describe('on Webpack 5', () => {
it('should work for CommonJS', async () => {
const compilation = await getCompilation('cjs');
const { execution, parsed } = compilation.module;
expect(parsed).toMatchInlineSnapshot(`
"(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.runtime = require('react-refresh');
module.exports = 'Test';
var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
$ReactRefreshModuleId$
);
function $ReactRefreshModuleRuntime$(exports) {
if (module.hot) {
var errorOverlay;
if (typeof __react_refresh_error_overlay__ !== 'undefined') {
errorOverlay = __react_refresh_error_overlay__;
}
var testMode;
if (typeof __react_refresh_test__ !== 'undefined') {
testMode = __react_refresh_test__;
}
return __react_refresh_utils__.executeRuntime(
exports,
$ReactRefreshModuleId$,
module.hot,
errorOverlay,
testMode
);
}
}
if (typeof Promise !== 'undefined' && $ReactRefreshCurrentExports$ instanceof Promise) {
$ReactRefreshCurrentExports$.then($ReactRefreshModuleRuntime$);
} else {
$ReactRefreshModuleRuntime$($ReactRefreshCurrentExports$);
}"
`);
expect(execution).toMatchInlineSnapshot(`
"(self["webpackChunkcjs"] = self["webpackChunkcjs"] || []).push([["main"],{
/***/ "./index.js":
/*!******************!*\\
!*** ./index.js ***!
\\******************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.runtime = __webpack_require__(/*! react-refresh */ "../../../../node_modules/react-refresh/runtime.js");
module.exports = 'Test';
var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
$ReactRefreshModuleId$
);
function $ReactRefreshModuleRuntime$(exports) {
if (true) {
var errorOverlay;
if (typeof __react_refresh_error_overlay__ !== 'undefined') {
errorOverlay = __react_refresh_error_overlay__;
}
var testMode;
if (typeof __react_refresh_test__ !== 'undefined') {
testMode = __react_refresh_test__;
}
return __react_refresh_utils__.executeRuntime(
exports,
$ReactRefreshModuleId$,
module.hot,
errorOverlay,
testMode
);
}
}
if (typeof Promise !== 'undefined' && $ReactRefreshCurrentExports$ instanceof Promise) {
$ReactRefreshCurrentExports$.then($ReactRefreshModuleRuntime$);
} else {
$ReactRefreshModuleRuntime$($ReactRefreshCurrentExports$);
}
/***/ })
},
/******/ __webpack_require__ => { // webpackRuntimeModules
/******/ var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
/******/ __webpack_require__.O(0, ["defaultVendors"], () => (__webpack_exec__("./index.js")));
/******/ var __webpack_exports__ = __webpack_require__.O();
/******/ }
]);"
`);
expect(compilation.errors).toStrictEqual([]);
expect(compilation.warnings).toStrictEqual([]);
});
it('should work for ES Modules', async () => {
const compilation = await getCompilation('esm');
const { execution, parsed } = compilation.module;
expect(parsed).toMatchInlineSnapshot(`
"import * as __react_refresh_runtime__ from 'react-refresh';
(typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.runtime = __react_refresh_runtime__;
export default 'Test';
var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
$ReactRefreshModuleId$
);
function $ReactRefreshModuleRuntime$(exports) {
if (import.meta.webpackHot) {
var errorOverlay;
if (typeof __react_refresh_error_overlay__ !== 'undefined') {
errorOverlay = __react_refresh_error_overlay__;
}
var testMode;
if (typeof __react_refresh_test__ !== 'undefined') {
testMode = __react_refresh_test__;
}
return __react_refresh_utils__.executeRuntime(
exports,
$ReactRefreshModuleId$,
import.meta.webpackHot,
errorOverlay,
testMode
);
}
}
if (typeof Promise !== 'undefined' && $ReactRefreshCurrentExports$ instanceof Promise) {
$ReactRefreshCurrentExports$.then($ReactRefreshModuleRuntime$);
} else {
$ReactRefreshModuleRuntime$($ReactRefreshCurrentExports$);
}"
`);
expect(execution).toMatchInlineSnapshot(`
""use strict";
(self["webpackChunkesm"] = self["webpackChunkesm"] || []).push([["main"],{
/***/ "./index.js":
/*!******************!*\\
!*** ./index.js ***!
\\******************/
/***/ ((__webpack_module__, __webpack_exports__, __webpack_require__) => {
var react_refresh__WEBPACK_IMPORTED_MODULE_0___namespace_cache;
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
/* harmony export */ });
/* harmony import */ var react_refresh__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react-refresh */ "../../../../node_modules/react-refresh/runtime.js");
(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)));
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ('Test');
var $ReactRefreshModuleId$ = (typeof __webpack_global__ !== 'undefined' ? __webpack_global__ : __webpack_require__).$Refresh$.moduleId;
var $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(
$ReactRefreshModuleId$
);
function $ReactRefreshModuleRuntime$(exports) {
if (true) {
var errorOverlay;
if (typeof __react_refresh_error_overlay__ !== 'undefined') {
errorOverlay = __react_refresh_error_overlay__;
}
var testMode;
if (typeof __react_refresh_test__ !== 'undefined') {
testMode = __react_refresh_test__;
}
return __react_refresh_utils__.executeRuntime(
exports,
$ReactRefreshModuleId$,
__webpack_module__.hot,
errorOverlay,
testMode
);
}
}
if (typeof Promise !== 'undefined' && $ReactRefreshCurrentExports$ instanceof Promise) {
$ReactRefreshCurrentExports$.then($ReactRefreshModuleRuntime$);
} else {
$ReactRefreshModuleRuntime$($ReactRefreshCurrentExports$);
}
/***/ })
},
/******/ __webpack_require__ => { // webpackRuntimeModules
/******/ var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
/******/ __webpack_require__.O(0, ["defaultVendors"], () => (__webpack_exec__("./index.js")));
/******/ var __webpack_exports__ = __webpack_require__.O();
/******/ }
]);"
`);
expect(compilation.errors).toStrictEqual([]);
expect(compilation.warnings).toStrictEqual([]);
});
it('should generate valid source map when the "devtool" option is specified', async () => {
const compilation = await getCompilation('cjs', { devtool: 'source-map' });
const { execution, sourceMap } = compilation.module;
expect(sourceMap).toMatchInlineSnapshot(`
"{
"version": 3,
"file": "main.js",
"mappings": ";;;;;;;;;;AAAA",
"sources": [
"webpack://cjs/./index.js"
],
"sourcesContent": [
"module.exports = 'Test';\\n"
],
"names": [],
"ignoreList": [],
"sourceRoot": ""
}"
`);
expect(() => {
validate(execution, sourceMap);
}).not.toThrow();
});
});
it('should generate valid source map when undefined source map is provided', async () => {
const compilation = await getCompilation('cjs', {
devtool: 'source-map',
prevSourceMap: undefined,
});
const { execution, sourceMap } = compilation.module;
expect(() => {
validate(execution, sourceMap);
}).not.toThrow();
});
it('should generate valid source map when null source map is provided', async () => {
const compilation = await getCompilation('cjs', {
devtool: 'source-map',
prevSourceMap: null,
});
const { execution, sourceMap } = compilation.module;
expect(() => {
validate(execution, sourceMap);
}).not.toThrow();
});
it('should generate valid source map when source map string is provided', async () => {
const compilation = await getCompilation('cjs', {
devtool: 'source-map',
prevSourceMap: JSON.stringify({
version: 3,
sources: ['cjs'],
names: [],
mappings: 'AAAA;AACA',
sourcesContent: ["module.exports = 'Test';\n"],
}),
});
const { execution, sourceMap } = compilation.module;
expect(() => {
validate(execution, sourceMap);
}).not.toThrow();
});
it('should generate valid source map when source map object is provided', async () => {
const compilation = await getCompilation('cjs', {
devtool: 'source-map',
prevSourceMap: {
version: 3,
sources: ['cjs'],
names: [],
mappings: 'AAAA;AACA',
sourcesContent: ["module.exports = 'Test';\n"],
},
});
const { execution, sourceMap } = compilation.module;
expect(() => {
validate(execution, sourceMap);
}).not.toThrow();
});
it('should work with global fetch polyfill', async () => {
const [fetch] = mockFetch();
await expect(getCompilation('cjs')).resolves.not.toThrow();
expect(global.fetch).toStrictEqual(fetch);
});
});