@rspack/plugin-react-refresh 1.0.0-alpha.0 → 1.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/errorOverlayEntry.js +108 -0
- package/client/overlay/components/CompileErrorTrace.js +58 -0
- package/client/overlay/components/PageHeader.js +58 -0
- package/client/overlay/components/RuntimeErrorFooter.js +93 -0
- package/client/overlay/components/RuntimeErrorHeader.js +37 -0
- package/client/overlay/components/RuntimeErrorStack.js +79 -0
- package/client/overlay/components/Spacer.js +19 -0
- package/client/overlay/containers/CompileErrorContainer.js +25 -0
- package/client/overlay/containers/RuntimeErrorContainer.js +29 -0
- package/client/overlay/index.js +348 -0
- package/client/overlay/theme.js +39 -0
- package/client/overlay/utils.js +74 -0
- package/client/utils/ansi-html.js +305 -0
- package/client/utils/errorEventHandlers.js +102 -0
- package/client/utils/formatWebpackErrors.js +96 -0
- package/client/utils/retry.js +23 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +33 -3
- package/dist/options.d.ts +16 -1
- package/dist/options.js +19 -0
- package/dist/sockets/WDSSocket.d.ts +10 -0
- package/dist/sockets/WDSSocket.js +41 -0
- package/dist/sockets/utils/getCurrentScriptSource.d.ts +1 -0
- package/dist/sockets/utils/getCurrentScriptSource.js +26 -0
- package/dist/sockets/utils/getSocketUrlParts.d.ts +9 -0
- package/dist/sockets/utils/getSocketUrlParts.js +112 -0
- package/dist/sockets/utils/getUrlFromParts.d.ts +9 -0
- package/dist/sockets/utils/getUrlFromParts.js +32 -0
- package/dist/sockets/utils/getWDSMetadata.d.ts +5 -0
- package/dist/sockets/utils/getWDSMetadata.js +30 -0
- package/dist/utils/getAdditionalEntries.d.ts +9 -0
- package/dist/utils/getAdditionalEntries.js +93 -0
- package/dist/utils/getSocketIntegration.d.ts +2 -0
- package/dist/utils/getSocketIntegration.js +17 -0
- package/package.json +7 -3
@@ -0,0 +1,348 @@
|
|
1
|
+
/**
|
2
|
+
* The following code is modified based on
|
3
|
+
* https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/f1c8b9a44198449093ca95f85af5df97925e1cfc/overlay/index.js
|
4
|
+
*
|
5
|
+
* MIT Licensed
|
6
|
+
* Author Michael Mok
|
7
|
+
* Copyright (c) 2019 Michael Mok
|
8
|
+
* https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/0b960573797bf38926937994c481e4fec9ed8aa6/LICENSE
|
9
|
+
*/
|
10
|
+
const RuntimeErrorFooter = require('./components/RuntimeErrorFooter.js');
|
11
|
+
const RuntimeErrorHeader = require('./components/RuntimeErrorHeader.js');
|
12
|
+
const CompileErrorContainer = require('./containers/CompileErrorContainer.js');
|
13
|
+
const RuntimeErrorContainer = require('./containers/RuntimeErrorContainer.js');
|
14
|
+
const theme = require('./theme.js');
|
15
|
+
const utils = require('./utils.js');
|
16
|
+
|
17
|
+
/**
|
18
|
+
* @callback RenderFn
|
19
|
+
* @returns {void}
|
20
|
+
*/
|
21
|
+
|
22
|
+
/* ===== Cached elements for DOM manipulations ===== */
|
23
|
+
/**
|
24
|
+
* The iframe that contains the overlay.
|
25
|
+
* @type {HTMLIFrameElement}
|
26
|
+
*/
|
27
|
+
let iframeRoot = null;
|
28
|
+
/**
|
29
|
+
* The document object from the iframe root, used to create and render elements.
|
30
|
+
* @type {Document}
|
31
|
+
*/
|
32
|
+
let rootDocument = null;
|
33
|
+
/**
|
34
|
+
* The root div elements will attach to.
|
35
|
+
* @type {HTMLDivElement}
|
36
|
+
*/
|
37
|
+
let root = null;
|
38
|
+
/**
|
39
|
+
* A Cached function to allow deferred render.
|
40
|
+
* @type {RenderFn | null}
|
41
|
+
*/
|
42
|
+
let scheduledRenderFn = null;
|
43
|
+
|
44
|
+
/* ===== Overlay State ===== */
|
45
|
+
/**
|
46
|
+
* The latest error message from Webpack compilation.
|
47
|
+
* @type {string}
|
48
|
+
*/
|
49
|
+
let currentCompileErrorMessage = '';
|
50
|
+
/**
|
51
|
+
* Index of the error currently shown by the overlay.
|
52
|
+
* @type {number}
|
53
|
+
*/
|
54
|
+
let currentRuntimeErrorIndex = 0;
|
55
|
+
/**
|
56
|
+
* The latest runtime error objects.
|
57
|
+
* @type {Error[]}
|
58
|
+
*/
|
59
|
+
let currentRuntimeErrors = [];
|
60
|
+
/**
|
61
|
+
* The render mode the overlay is currently in.
|
62
|
+
* @type {'compileError' | 'runtimeError' | null}
|
63
|
+
*/
|
64
|
+
let currentMode = null;
|
65
|
+
|
66
|
+
/**
|
67
|
+
* @typedef {Object} IframeProps
|
68
|
+
* @property {function(): void} onIframeLoad
|
69
|
+
*/
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Creates the main `iframe` the overlay will attach to.
|
73
|
+
* Accepts a callback to be ran after iframe is initialized.
|
74
|
+
* @param {Document} document
|
75
|
+
* @param {HTMLElement} root
|
76
|
+
* @param {IframeProps} props
|
77
|
+
* @returns {HTMLIFrameElement}
|
78
|
+
*/
|
79
|
+
function IframeRoot(document, root, props) {
|
80
|
+
const iframe = document.createElement('iframe');
|
81
|
+
iframe.id = 'react-refresh-overlay';
|
82
|
+
iframe.src = 'about:blank';
|
83
|
+
|
84
|
+
iframe.style.border = 'none';
|
85
|
+
iframe.style.height = '100%';
|
86
|
+
iframe.style.left = '0';
|
87
|
+
iframe.style.minHeight = '100vh';
|
88
|
+
iframe.style.minHeight = '-webkit-fill-available';
|
89
|
+
iframe.style.position = 'fixed';
|
90
|
+
iframe.style.top = '0';
|
91
|
+
iframe.style.width = '100vw';
|
92
|
+
iframe.style.zIndex = '2147483647';
|
93
|
+
iframe.addEventListener('load', function onLoad() {
|
94
|
+
// Reset margin of iframe body
|
95
|
+
iframe.contentDocument.body.style.margin = '0';
|
96
|
+
props.onIframeLoad();
|
97
|
+
});
|
98
|
+
|
99
|
+
// We skip mounting and returns as we need to ensure
|
100
|
+
// the load event is fired after we setup the global variable
|
101
|
+
return iframe;
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* Creates the main `div` element for the overlay to render.
|
106
|
+
* @param {Document} document
|
107
|
+
* @param {HTMLElement} root
|
108
|
+
* @returns {HTMLDivElement}
|
109
|
+
*/
|
110
|
+
function OverlayRoot(document, root) {
|
111
|
+
const div = document.createElement('div');
|
112
|
+
div.id = 'react-refresh-overlay-error';
|
113
|
+
|
114
|
+
// Style the contents container
|
115
|
+
div.style.backgroundColor = '#' + theme.grey;
|
116
|
+
div.style.boxSizing = 'border-box';
|
117
|
+
div.style.color = '#' + theme.white;
|
118
|
+
div.style.fontFamily = [
|
119
|
+
'-apple-system',
|
120
|
+
'BlinkMacSystemFont',
|
121
|
+
'"Segoe UI"',
|
122
|
+
'"Helvetica Neue"',
|
123
|
+
'Helvetica',
|
124
|
+
'Arial',
|
125
|
+
'sans-serif',
|
126
|
+
'"Apple Color Emoji"',
|
127
|
+
'"Segoe UI Emoji"',
|
128
|
+
'Segoe UI Symbol',
|
129
|
+
].join(', ');
|
130
|
+
div.style.fontSize = '0.875rem';
|
131
|
+
div.style.height = '100%';
|
132
|
+
div.style.lineHeight = '1.3';
|
133
|
+
div.style.overflow = 'auto';
|
134
|
+
div.style.padding = '1rem 1.5rem 0';
|
135
|
+
div.style.paddingTop = 'max(1rem, env(safe-area-inset-top))';
|
136
|
+
div.style.paddingRight = 'max(1.5rem, env(safe-area-inset-right))';
|
137
|
+
div.style.paddingBottom = 'env(safe-area-inset-bottom)';
|
138
|
+
div.style.paddingLeft = 'max(1.5rem, env(safe-area-inset-left))';
|
139
|
+
div.style.width = '100vw';
|
140
|
+
|
141
|
+
root.appendChild(div);
|
142
|
+
return div;
|
143
|
+
}
|
144
|
+
|
145
|
+
/**
|
146
|
+
* Ensures the iframe root and the overlay root are both initialized before render.
|
147
|
+
* If check fails, render will be deferred until both roots are initialized.
|
148
|
+
* @param {RenderFn} renderFn A function that triggers a DOM render.
|
149
|
+
* @returns {void}
|
150
|
+
*/
|
151
|
+
function ensureRootExists(renderFn) {
|
152
|
+
if (root) {
|
153
|
+
// Overlay root is ready, we can render right away.
|
154
|
+
renderFn();
|
155
|
+
return;
|
156
|
+
}
|
157
|
+
|
158
|
+
// Creating an iframe may be asynchronous so we'll defer render.
|
159
|
+
// In case of multiple calls, function from the last call will be used.
|
160
|
+
scheduledRenderFn = renderFn;
|
161
|
+
|
162
|
+
if (iframeRoot) {
|
163
|
+
// Iframe is already ready, it will fire the load event.
|
164
|
+
return;
|
165
|
+
}
|
166
|
+
|
167
|
+
// Create the iframe root, and, the overlay root inside it when it is ready.
|
168
|
+
iframeRoot = IframeRoot(document, document.body, {
|
169
|
+
onIframeLoad: function onIframeLoad() {
|
170
|
+
rootDocument = iframeRoot.contentDocument;
|
171
|
+
root = OverlayRoot(rootDocument, rootDocument.body);
|
172
|
+
scheduledRenderFn();
|
173
|
+
},
|
174
|
+
});
|
175
|
+
|
176
|
+
// We have to mount here to ensure `iframeRoot` is set when `onIframeLoad` fires.
|
177
|
+
// This is because onIframeLoad() will be called synchronously
|
178
|
+
// or asynchronously depending on the browser.
|
179
|
+
document.body.appendChild(iframeRoot);
|
180
|
+
}
|
181
|
+
|
182
|
+
/**
|
183
|
+
* Creates the main `div` element for the overlay to render.
|
184
|
+
* @returns {void}
|
185
|
+
*/
|
186
|
+
function render() {
|
187
|
+
ensureRootExists(function () {
|
188
|
+
const currentFocus = rootDocument.activeElement;
|
189
|
+
let currentFocusId;
|
190
|
+
if (currentFocus.localName === 'button' && currentFocus.id) {
|
191
|
+
currentFocusId = currentFocus.id;
|
192
|
+
}
|
193
|
+
|
194
|
+
utils.removeAllChildren(root);
|
195
|
+
|
196
|
+
if (currentCompileErrorMessage) {
|
197
|
+
currentMode = 'compileError';
|
198
|
+
|
199
|
+
CompileErrorContainer(rootDocument, root, {
|
200
|
+
errorMessage: currentCompileErrorMessage,
|
201
|
+
});
|
202
|
+
} else if (currentRuntimeErrors.length) {
|
203
|
+
currentMode = 'runtimeError';
|
204
|
+
|
205
|
+
RuntimeErrorHeader(rootDocument, root, {
|
206
|
+
currentErrorIndex: currentRuntimeErrorIndex,
|
207
|
+
totalErrors: currentRuntimeErrors.length,
|
208
|
+
});
|
209
|
+
RuntimeErrorContainer(rootDocument, root, {
|
210
|
+
currentError: currentRuntimeErrors[currentRuntimeErrorIndex],
|
211
|
+
});
|
212
|
+
RuntimeErrorFooter(rootDocument, root, {
|
213
|
+
initialFocus: currentFocusId,
|
214
|
+
multiple: currentRuntimeErrors.length > 1,
|
215
|
+
onClickCloseButton: function onClose() {
|
216
|
+
clearRuntimeErrors();
|
217
|
+
},
|
218
|
+
onClickNextButton: function onNext() {
|
219
|
+
if (currentRuntimeErrorIndex === currentRuntimeErrors.length - 1) {
|
220
|
+
return;
|
221
|
+
}
|
222
|
+
currentRuntimeErrorIndex += 1;
|
223
|
+
ensureRootExists(render);
|
224
|
+
},
|
225
|
+
onClickPrevButton: function onPrev() {
|
226
|
+
if (currentRuntimeErrorIndex === 0) {
|
227
|
+
return;
|
228
|
+
}
|
229
|
+
currentRuntimeErrorIndex -= 1;
|
230
|
+
ensureRootExists(render);
|
231
|
+
},
|
232
|
+
});
|
233
|
+
}
|
234
|
+
});
|
235
|
+
}
|
236
|
+
|
237
|
+
/**
|
238
|
+
* Destroys the state of the overlay.
|
239
|
+
* @returns {void}
|
240
|
+
*/
|
241
|
+
function cleanup() {
|
242
|
+
// Clean up and reset all internal state.
|
243
|
+
document.body.removeChild(iframeRoot);
|
244
|
+
scheduledRenderFn = null;
|
245
|
+
root = null;
|
246
|
+
iframeRoot = null;
|
247
|
+
}
|
248
|
+
|
249
|
+
/**
|
250
|
+
* Clears Webpack compilation errors and dismisses the compile error overlay.
|
251
|
+
* @returns {void}
|
252
|
+
*/
|
253
|
+
function clearCompileError() {
|
254
|
+
if (!root || currentMode !== 'compileError') {
|
255
|
+
return;
|
256
|
+
}
|
257
|
+
|
258
|
+
currentCompileErrorMessage = '';
|
259
|
+
currentMode = null;
|
260
|
+
cleanup();
|
261
|
+
}
|
262
|
+
|
263
|
+
/**
|
264
|
+
* Clears runtime error records and dismisses the runtime error overlay.
|
265
|
+
* @param {boolean} [dismissOverlay] Whether to dismiss the overlay or not.
|
266
|
+
* @returns {void}
|
267
|
+
*/
|
268
|
+
function clearRuntimeErrors(dismissOverlay) {
|
269
|
+
if (!root || currentMode !== 'runtimeError') {
|
270
|
+
return;
|
271
|
+
}
|
272
|
+
|
273
|
+
currentRuntimeErrorIndex = 0;
|
274
|
+
currentRuntimeErrors = [];
|
275
|
+
|
276
|
+
if (typeof dismissOverlay === 'undefined' || dismissOverlay) {
|
277
|
+
currentMode = null;
|
278
|
+
cleanup();
|
279
|
+
}
|
280
|
+
}
|
281
|
+
|
282
|
+
/**
|
283
|
+
* Shows the compile error overlay with the specific Webpack error message.
|
284
|
+
* @param {string} message
|
285
|
+
* @returns {void}
|
286
|
+
*/
|
287
|
+
function showCompileError(message) {
|
288
|
+
if (!message) {
|
289
|
+
return;
|
290
|
+
}
|
291
|
+
|
292
|
+
currentCompileErrorMessage = message;
|
293
|
+
|
294
|
+
render();
|
295
|
+
}
|
296
|
+
|
297
|
+
/**
|
298
|
+
* Shows the runtime error overlay with the specific error records.
|
299
|
+
* @param {Error[]} errors
|
300
|
+
* @returns {void}
|
301
|
+
*/
|
302
|
+
function showRuntimeErrors(errors) {
|
303
|
+
if (!errors || !errors.length) {
|
304
|
+
return;
|
305
|
+
}
|
306
|
+
|
307
|
+
currentRuntimeErrors = errors;
|
308
|
+
|
309
|
+
render();
|
310
|
+
}
|
311
|
+
|
312
|
+
/**
|
313
|
+
* The debounced version of `showRuntimeErrors` to prevent frequent renders
|
314
|
+
* due to rapid firing listeners.
|
315
|
+
* @param {Error[]} errors
|
316
|
+
* @returns {void}
|
317
|
+
*/
|
318
|
+
const debouncedShowRuntimeErrors = utils.debounce(showRuntimeErrors, 30);
|
319
|
+
|
320
|
+
/**
|
321
|
+
* Detects if an error is a Webpack compilation error.
|
322
|
+
* @param {Error} error The error of interest.
|
323
|
+
* @returns {boolean} If the error is a Webpack compilation error.
|
324
|
+
*/
|
325
|
+
function isWebpackCompileError(error) {
|
326
|
+
return /Module [A-z ]+\(from/.test(error.message) || /Cannot find module/.test(error.message);
|
327
|
+
}
|
328
|
+
|
329
|
+
/**
|
330
|
+
* Handles runtime error contexts captured with EventListeners.
|
331
|
+
* Integrates with a runtime error overlay.
|
332
|
+
* @param {Error} error A valid error object.
|
333
|
+
* @returns {void}
|
334
|
+
*/
|
335
|
+
function handleRuntimeError(error) {
|
336
|
+
if (error && !isWebpackCompileError(error) && currentRuntimeErrors.indexOf(error) === -1) {
|
337
|
+
currentRuntimeErrors = currentRuntimeErrors.concat(error);
|
338
|
+
}
|
339
|
+
debouncedShowRuntimeErrors(currentRuntimeErrors);
|
340
|
+
}
|
341
|
+
|
342
|
+
module.exports = Object.freeze({
|
343
|
+
clearCompileError: clearCompileError,
|
344
|
+
clearRuntimeErrors: clearRuntimeErrors,
|
345
|
+
handleRuntimeError: handleRuntimeError,
|
346
|
+
showCompileError: showCompileError,
|
347
|
+
showRuntimeErrors: showRuntimeErrors,
|
348
|
+
});
|
@@ -0,0 +1,39 @@
|
|
1
|
+
/**
|
2
|
+
* @typedef {Object} Theme
|
3
|
+
* @property {string[]} reset
|
4
|
+
* @property {string} black
|
5
|
+
* @property {string} red
|
6
|
+
* @property {string} green
|
7
|
+
* @property {string} yellow
|
8
|
+
* @property {string} blue
|
9
|
+
* @property {string} magenta
|
10
|
+
* @property {string} cyan
|
11
|
+
* @property {string} white
|
12
|
+
* @property {string} lightgrey
|
13
|
+
* @property {string} darkgrey
|
14
|
+
* @property {string} grey
|
15
|
+
* @property {string} dimgrey
|
16
|
+
*/
|
17
|
+
|
18
|
+
/**
|
19
|
+
* @type {Theme} theme
|
20
|
+
* A collection of colors to be used by the overlay.
|
21
|
+
* Partially adopted from Tomorrow Night Bright.
|
22
|
+
*/
|
23
|
+
const theme = {
|
24
|
+
reset: ['transparent', 'transparent'],
|
25
|
+
black: '000000',
|
26
|
+
red: 'D34F56',
|
27
|
+
green: 'B9C954',
|
28
|
+
yellow: 'E6C452',
|
29
|
+
blue: '7CA7D8',
|
30
|
+
magenta: 'C299D6',
|
31
|
+
cyan: '73BFB1',
|
32
|
+
white: 'FFFFFF',
|
33
|
+
lightgrey: 'C7C7C7',
|
34
|
+
darkgrey: 'A9A9A9',
|
35
|
+
grey: '474747',
|
36
|
+
dimgrey: '343434',
|
37
|
+
};
|
38
|
+
|
39
|
+
module.exports = theme;
|
@@ -0,0 +1,74 @@
|
|
1
|
+
/**
|
2
|
+
* Debounce a function to delay invoking until wait (ms) have elapsed since the last invocation.
|
3
|
+
* @param {function(...*): *} fn The function to be debounced.
|
4
|
+
* @param {number} wait Milliseconds to wait before invoking again.
|
5
|
+
* @return {function(...*): void} The debounced function.
|
6
|
+
*/
|
7
|
+
function debounce(fn, wait) {
|
8
|
+
/**
|
9
|
+
* A cached setTimeout handler.
|
10
|
+
* @type {number | undefined}
|
11
|
+
*/
|
12
|
+
let timer;
|
13
|
+
|
14
|
+
/**
|
15
|
+
* @returns {void}
|
16
|
+
*/
|
17
|
+
function debounced() {
|
18
|
+
const context = this;
|
19
|
+
const args = arguments;
|
20
|
+
|
21
|
+
clearTimeout(timer);
|
22
|
+
timer = setTimeout(function () {
|
23
|
+
return fn.apply(context, args);
|
24
|
+
}, wait);
|
25
|
+
}
|
26
|
+
|
27
|
+
return debounced;
|
28
|
+
}
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Prettify a filename from error stacks into the desired format.
|
32
|
+
* @param {string} filename The filename to be formatted.
|
33
|
+
* @returns {string} The formatted filename.
|
34
|
+
*/
|
35
|
+
function formatFilename(filename) {
|
36
|
+
// Strip away protocol and domain for compiled files
|
37
|
+
const htmlMatch = /^https?:\/\/(.*)\/(.*)/.exec(filename);
|
38
|
+
if (htmlMatch && htmlMatch[1] && htmlMatch[2]) {
|
39
|
+
return htmlMatch[2];
|
40
|
+
}
|
41
|
+
|
42
|
+
// Strip everything before the first directory for source files
|
43
|
+
const sourceMatch = /\/.*?([^./]+[/|\\].*)$/.exec(filename);
|
44
|
+
if (sourceMatch && sourceMatch[1]) {
|
45
|
+
return sourceMatch[1].replace(/\?$/, '');
|
46
|
+
}
|
47
|
+
|
48
|
+
// Unknown filename type, use it as is
|
49
|
+
return filename;
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Remove all children of an element.
|
54
|
+
* @param {HTMLElement} element A valid HTML element.
|
55
|
+
* @param {number} [skip] Number of elements to skip removing.
|
56
|
+
* @returns {void}
|
57
|
+
*/
|
58
|
+
function removeAllChildren(element, skip) {
|
59
|
+
/** @type {Node[]} */
|
60
|
+
const childList = Array.prototype.slice.call(
|
61
|
+
element.childNodes,
|
62
|
+
typeof skip !== 'undefined' ? skip : 0
|
63
|
+
);
|
64
|
+
|
65
|
+
for (let i = 0; i < childList.length; i += 1) {
|
66
|
+
element.removeChild(childList[i]);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
module.exports = {
|
71
|
+
debounce: debounce,
|
72
|
+
formatFilename: formatFilename,
|
73
|
+
removeAllChildren: removeAllChildren,
|
74
|
+
};
|