@lark-apaas/coding-preset-vite-react 0.1.1-alpha.0
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/dist/index.d.ts +320 -0
- package/dist/index.js +1941 -0
- package/dist/index.js.map +1 -0
- package/package.json +69 -0
- package/src/empty.css +1 -0
- package/src/inspector-stub.js +6 -0
- package/src/module-alias/clsx.mjs +8 -0
- package/src/module-alias/echarts-for-react.mjs +130 -0
- package/src/module-alias/echarts.mjs +43 -0
- package/src/module-alias/registry_echarts_theme.mjs +390 -0
- package/src/overlay/components.js +94 -0
- package/src/overlay/index.js +443 -0
- package/src/overlay/vite-client.js +555 -0
- package/src/polyfills/index.ts +35 -0
- package/src/runtime/ws-watchdog-client.mjs +148 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
/* eslint-disable no-undef */
|
|
2
|
+
const { ErrorContainer, RootStyle } = require('./components');
|
|
3
|
+
|
|
4
|
+
/* ===== Cached elements for DOM manipulations ===== */
|
|
5
|
+
/**
|
|
6
|
+
* The iframe that contains the overlay.
|
|
7
|
+
* @type {HTMLIFrameElement}
|
|
8
|
+
*/
|
|
9
|
+
let iframeRoot = null;
|
|
10
|
+
/**
|
|
11
|
+
* The document object from the iframe root, used to create and render elements.
|
|
12
|
+
* @type {Document}
|
|
13
|
+
*/
|
|
14
|
+
let rootDocument = null;
|
|
15
|
+
/**
|
|
16
|
+
* The root div elements will attach to.
|
|
17
|
+
* @type {HTMLDivElement}
|
|
18
|
+
*/
|
|
19
|
+
let root = null;
|
|
20
|
+
/**
|
|
21
|
+
* A Cached function to allow deferred render.
|
|
22
|
+
* @type {RenderFn | null}
|
|
23
|
+
*/
|
|
24
|
+
let scheduledRenderFn = null;
|
|
25
|
+
|
|
26
|
+
/* ===== Overlay State ===== */
|
|
27
|
+
/**
|
|
28
|
+
* The latest error message from Vite compilation.
|
|
29
|
+
* @type {string}
|
|
30
|
+
*/
|
|
31
|
+
let currentCompileErrorMessage = '';
|
|
32
|
+
/**
|
|
33
|
+
* The latest runtime error objects.
|
|
34
|
+
* @type {Error[]}
|
|
35
|
+
*/
|
|
36
|
+
let currentRuntimeErrors = [];
|
|
37
|
+
/**
|
|
38
|
+
* The render mode the overlay is currently in.
|
|
39
|
+
* @type {'compileError' | 'runtimeError' | null}
|
|
40
|
+
*/
|
|
41
|
+
let currentMode = null;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @typedef {Object} IframeProps
|
|
45
|
+
* @property {function(): void} onIframeLoad
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates the main `iframe` the overlay will attach to.
|
|
50
|
+
* Accepts a callback to be ran after iframe is initialized.
|
|
51
|
+
* @param {Document} document
|
|
52
|
+
* @param {HTMLElement} root
|
|
53
|
+
* @param {IframeProps} props
|
|
54
|
+
* @returns {HTMLIFrameElement}
|
|
55
|
+
*/
|
|
56
|
+
function IframeRoot(document, root, props) {
|
|
57
|
+
const iframe = document.createElement('iframe');
|
|
58
|
+
iframe.id = 'vite-error-overlay';
|
|
59
|
+
iframe.src = 'about:blank';
|
|
60
|
+
|
|
61
|
+
iframe.style.border = 'none';
|
|
62
|
+
iframe.style.height = '100%';
|
|
63
|
+
iframe.style.left = '0';
|
|
64
|
+
iframe.style.minHeight = '100vh';
|
|
65
|
+
iframe.style.minHeight = '-webkit-fill-available';
|
|
66
|
+
iframe.style.position = 'fixed';
|
|
67
|
+
iframe.style.top = '0';
|
|
68
|
+
iframe.style.width = '100vw';
|
|
69
|
+
iframe.style.zIndex = '2147483647';
|
|
70
|
+
iframe.addEventListener('load', function onLoad() {
|
|
71
|
+
try {
|
|
72
|
+
// Reset margin of iframe body
|
|
73
|
+
iframe.contentDocument.body.style.margin = '0';
|
|
74
|
+
props.onIframeLoad();
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error('Error during iframe load:', error);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// We skip mounting and returns as we need to ensure
|
|
81
|
+
// the load event is fired after we setup the global variable
|
|
82
|
+
return iframe;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Creates the main `div` element for the overlay to render.
|
|
87
|
+
* @param {Document} document
|
|
88
|
+
* @param {HTMLElement} root
|
|
89
|
+
* @returns {HTMLDivElement}
|
|
90
|
+
*/
|
|
91
|
+
function OverlayRoot(document, root) {
|
|
92
|
+
const div = document.createElement('div');
|
|
93
|
+
div.id = 'container';
|
|
94
|
+
root.appendChild(div);
|
|
95
|
+
return div;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Ensures the iframe root and the overlay root are both initialized before render.
|
|
100
|
+
* If check fails, render will be deferred until both roots are initialized.
|
|
101
|
+
* @param {RenderFn} renderFn A function that triggers a DOM render.
|
|
102
|
+
* @returns {void}
|
|
103
|
+
*/
|
|
104
|
+
function ensureRootExists(renderFn) {
|
|
105
|
+
if (root) {
|
|
106
|
+
// Overlay root is ready, we can render right away.
|
|
107
|
+
renderFn();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Creating an iframe may be asynchronous so we'll defer render.
|
|
112
|
+
// In case of multiple calls, function from the last call will be used.
|
|
113
|
+
scheduledRenderFn = renderFn;
|
|
114
|
+
|
|
115
|
+
if (iframeRoot) {
|
|
116
|
+
// Iframe is already ready, it will fire the load event.
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Create the iframe root, and, the overlay root inside it when it is ready.
|
|
121
|
+
iframeRoot = IframeRoot(document, document.body, {
|
|
122
|
+
onIframeLoad: function onIframeLoad() {
|
|
123
|
+
rootDocument = iframeRoot.contentDocument;
|
|
124
|
+
RootStyle(rootDocument, rootDocument.head);
|
|
125
|
+
root = OverlayRoot(rootDocument, rootDocument.body);
|
|
126
|
+
scheduledRenderFn();
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// We have to mount here to ensure `iframeRoot` is set when `onIframeLoad` fires.
|
|
131
|
+
// This is because onIframeLoad() will be called synchronously
|
|
132
|
+
// or asynchronously depending on the browser.
|
|
133
|
+
document.body.appendChild(iframeRoot);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function removeAllChildren(element, skip) {
|
|
137
|
+
/** @type {Node[]} */
|
|
138
|
+
const childList = Array.prototype.slice.call(
|
|
139
|
+
element.childNodes,
|
|
140
|
+
typeof skip !== 'undefined' ? skip : 0
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
for (let i = 0; i < childList.length; i += 1) {
|
|
144
|
+
element.removeChild(childList[i]);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function getParentOriginFromParams() {
|
|
149
|
+
try {
|
|
150
|
+
var params = new URLSearchParams(window.location.search);
|
|
151
|
+
var origin = params.get('__parentOrigin');
|
|
152
|
+
if (origin) {
|
|
153
|
+
sessionStorage.setItem('__parentOrigin', origin);
|
|
154
|
+
return origin;
|
|
155
|
+
}
|
|
156
|
+
} catch (e) {
|
|
157
|
+
// ignore
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
return sessionStorage.getItem('__parentOrigin') || '';
|
|
161
|
+
} catch (e) {
|
|
162
|
+
return '';
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function getLegacyParentOrigin() {
|
|
167
|
+
const currentOrigin = window.location?.origin || '';
|
|
168
|
+
if (currentOrigin.includes('force.feishuapp.net')) {
|
|
169
|
+
return 'https://force.feishu.cn';
|
|
170
|
+
}
|
|
171
|
+
if (currentOrigin.includes('force-pre.feishuapp.net')) {
|
|
172
|
+
return 'https://force.feishu-pre.cn';
|
|
173
|
+
}
|
|
174
|
+
if (currentOrigin.includes('force.byted.org')) {
|
|
175
|
+
return 'https://force.feishu-boe.cn';
|
|
176
|
+
}
|
|
177
|
+
if (currentOrigin.includes('feishuapp.cn') || currentOrigin.includes('miaoda.feishuapp.net')) {
|
|
178
|
+
return 'https://miaoda.feishu.cn';
|
|
179
|
+
}
|
|
180
|
+
if (currentOrigin.includes('fsapp.kundou.cn') || currentOrigin.includes('miaoda-pre.feishuapp.net')) {
|
|
181
|
+
return 'https://miaoda.feishu-pre.cn';
|
|
182
|
+
}
|
|
183
|
+
return 'https://miaoda.feishu-boe.cn';
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function sendPostMessage(message, targetOrigin) {
|
|
187
|
+
try {
|
|
188
|
+
const parentOrigin = getParentOriginFromParams() || process?.env?.FORCE_FRAMEWORK_DOMAIN_MAIN || getLegacyParentOrigin();
|
|
189
|
+
const origin = targetOrigin || parentOrigin;
|
|
190
|
+
window.parent.postMessage(message, origin);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
console.error('ViteErrorOverlay 发送 postMessage 失败:', error);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 上报错误信息
|
|
198
|
+
* @param {(string | Error)[]} messages 日志信息
|
|
199
|
+
* @param {'compileError' | 'runtimeError'} mode 日志模式
|
|
200
|
+
*/
|
|
201
|
+
function logError(messages, mode) {
|
|
202
|
+
const logMeta = {
|
|
203
|
+
noStacktrace: mode === 'compileError',
|
|
204
|
+
stacktrace: mode === 'compileError' ? [] : undefined,
|
|
205
|
+
type: mode === 'compileError' ? 'compile-error' : 'uncaught-render-error',
|
|
206
|
+
};
|
|
207
|
+
try {
|
|
208
|
+
// 优先使用暴露的 __RUNTIME_LOGGER__ 上报错误
|
|
209
|
+
// 运行时错误出现时,window已经挂载
|
|
210
|
+
if (window.__RUNTIME_LOGGER__) {
|
|
211
|
+
window.__RUNTIME_LOGGER__.get().log({
|
|
212
|
+
level: 'error',
|
|
213
|
+
args: messages,
|
|
214
|
+
meta: logMeta,
|
|
215
|
+
});
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
// 首次执行编译报错会走此处逻辑;
|
|
219
|
+
const parts = [];
|
|
220
|
+
for (const m of messages) {
|
|
221
|
+
if (m instanceof Error) {
|
|
222
|
+
parts.push(m.message, m);
|
|
223
|
+
} else {
|
|
224
|
+
parts.push(m);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const logJSON = {
|
|
228
|
+
id: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
229
|
+
const r = Math.random() * 16 | 0;
|
|
230
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
231
|
+
return v.toString(16);
|
|
232
|
+
}),
|
|
233
|
+
type: 'typedLogV2',
|
|
234
|
+
level: 'error',
|
|
235
|
+
args: parts,
|
|
236
|
+
meta: logMeta,
|
|
237
|
+
};
|
|
238
|
+
sendPostMessage({ type: 'SELECTED_LOG', payload: JSON.stringify(logJSON) });
|
|
239
|
+
} catch (e) {
|
|
240
|
+
console.log('上报错误信息失败', e)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Creates the main `div` element for the overlay to render.
|
|
247
|
+
* @returns {void}
|
|
248
|
+
*/
|
|
249
|
+
function render() {
|
|
250
|
+
ensureRootExists(function () {
|
|
251
|
+
removeAllChildren(root);
|
|
252
|
+
// 通知前端,渲染错误页面已准备就绪
|
|
253
|
+
sendPostMessage({
|
|
254
|
+
type: 'PreviewReady',
|
|
255
|
+
});
|
|
256
|
+
// 通知主应用存在自定义 overlay
|
|
257
|
+
sendPostMessage({
|
|
258
|
+
type: 'app-features',
|
|
259
|
+
payload: [
|
|
260
|
+
{
|
|
261
|
+
feature: 'error-overlay',
|
|
262
|
+
enable: true,
|
|
263
|
+
},
|
|
264
|
+
],
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
if (currentCompileErrorMessage) {
|
|
268
|
+
currentMode = 'compileError';
|
|
269
|
+
|
|
270
|
+
// 通知主应用,当前出现渲染报错
|
|
271
|
+
sendPostMessage({
|
|
272
|
+
type: 'RenderError',
|
|
273
|
+
data: currentMode,
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// 发送编译错误
|
|
277
|
+
logError(['Compile Error', currentCompileErrorMessage], currentMode);
|
|
278
|
+
ErrorContainer(rootDocument, root, '编译出错');
|
|
279
|
+
} else if (currentRuntimeErrors.length) {
|
|
280
|
+
currentMode = 'runtimeError';
|
|
281
|
+
|
|
282
|
+
// 通知主应用,当前出现渲染报错
|
|
283
|
+
sendPostMessage({
|
|
284
|
+
type: 'RenderError',
|
|
285
|
+
data: currentMode,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// 发送运行错误
|
|
289
|
+
logError(['Uncaught Render Error', ...currentRuntimeErrors], currentMode);
|
|
290
|
+
// 叠加全部报错,将报错信息发送到妙搭
|
|
291
|
+
ErrorContainer(rootDocument, root, '页面出错了');
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Destroys the state of the overlay.
|
|
298
|
+
* @returns {void}
|
|
299
|
+
*/
|
|
300
|
+
function cleanup() {
|
|
301
|
+
// Clean up and reset all internal state.
|
|
302
|
+
try {
|
|
303
|
+
document.body.removeChild(iframeRoot);
|
|
304
|
+
} catch (e) {
|
|
305
|
+
// In case user render react app directly to body, will trigger `NotFoundError` when recovery from an Error
|
|
306
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild#exceptions
|
|
307
|
+
}
|
|
308
|
+
scheduledRenderFn = null;
|
|
309
|
+
root = null;
|
|
310
|
+
iframeRoot = null;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Clears Vite compilation errors and dismisses the compile error overlay.
|
|
315
|
+
* @returns {void}
|
|
316
|
+
*/
|
|
317
|
+
function clearCompileError() {
|
|
318
|
+
if (!root || currentMode !== 'compileError') {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
currentCompileErrorMessage = '';
|
|
323
|
+
currentMode = null;
|
|
324
|
+
cleanup();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Clears runtime error records and dismisses the runtime error overlay.
|
|
329
|
+
* @param {boolean} [dismissOverlay] Whether to dismiss the overlay or not.
|
|
330
|
+
* @returns {void}
|
|
331
|
+
*/
|
|
332
|
+
function clearRuntimeErrors(dismissOverlay) {
|
|
333
|
+
if (!root || currentMode !== 'runtimeError') {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
currentRuntimeErrors = [];
|
|
338
|
+
|
|
339
|
+
if (typeof dismissOverlay === 'undefined' || dismissOverlay) {
|
|
340
|
+
currentMode = null;
|
|
341
|
+
cleanup();
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Shows the compile error overlay with the specific Vite error message.
|
|
347
|
+
* @param {string} message
|
|
348
|
+
* @returns {void}
|
|
349
|
+
*/
|
|
350
|
+
function showCompileError(message) {
|
|
351
|
+
if (!message) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
currentCompileErrorMessage = message;
|
|
356
|
+
|
|
357
|
+
render();
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Shows the runtime error overlay with the specific error records.
|
|
362
|
+
* @param {Error[]} errors
|
|
363
|
+
* @returns {void}
|
|
364
|
+
*/
|
|
365
|
+
function showRuntimeErrors(errors) {
|
|
366
|
+
if (!errors || !errors.length) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
currentRuntimeErrors = errors;
|
|
371
|
+
|
|
372
|
+
render();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function debounce(fn, wait) {
|
|
376
|
+
/**
|
|
377
|
+
* A cached setTimeout handler.
|
|
378
|
+
* @type {number | undefined}
|
|
379
|
+
*/
|
|
380
|
+
let timer;
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* @returns {void}
|
|
384
|
+
*/
|
|
385
|
+
function debounced() {
|
|
386
|
+
const context = this;
|
|
387
|
+
const args = arguments;
|
|
388
|
+
|
|
389
|
+
clearTimeout(timer);
|
|
390
|
+
timer = setTimeout(function () {
|
|
391
|
+
return fn.apply(context, args);
|
|
392
|
+
}, wait);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return debounced;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* The debounced version of `showRuntimeErrors` to prevent frequent renders
|
|
399
|
+
* due to rapid firing listeners.
|
|
400
|
+
* @param {Error[]} errors
|
|
401
|
+
* @returns {void}
|
|
402
|
+
*/
|
|
403
|
+
const debouncedShowRuntimeErrors = debounce(showRuntimeErrors, 30);
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Detects if an error is a Vite compilation error.
|
|
407
|
+
* @param {Error} error The error of interest.
|
|
408
|
+
* @returns {boolean} If the error is a Vite compilation error.
|
|
409
|
+
*/
|
|
410
|
+
function isViteCompileError(error) {
|
|
411
|
+
return (
|
|
412
|
+
/Module [A-z ]+\(from/.test(error.message) ||
|
|
413
|
+
/Cannot find module/.test(error.message) ||
|
|
414
|
+
/Failed to resolve import/.test(error.message) ||
|
|
415
|
+
/Transform failed/.test(error.message) ||
|
|
416
|
+
/Pre-transform error/.test(error.message)
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Handles runtime error contexts captured with EventListeners.
|
|
422
|
+
* Integrates with a runtime error overlay.
|
|
423
|
+
* @param {Error} error A valid error object.
|
|
424
|
+
* @returns {void}
|
|
425
|
+
*/
|
|
426
|
+
function handleRuntimeError(error) {
|
|
427
|
+
if (
|
|
428
|
+
error &&
|
|
429
|
+
!isViteCompileError(error) &&
|
|
430
|
+
currentRuntimeErrors.indexOf(error) === -1
|
|
431
|
+
) {
|
|
432
|
+
currentRuntimeErrors = currentRuntimeErrors.concat(error);
|
|
433
|
+
}
|
|
434
|
+
debouncedShowRuntimeErrors(currentRuntimeErrors);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
module.exports = Object.freeze({
|
|
438
|
+
clearCompileError: clearCompileError,
|
|
439
|
+
clearRuntimeErrors: clearRuntimeErrors,
|
|
440
|
+
handleRuntimeError: handleRuntimeError,
|
|
441
|
+
showCompileError: showCompileError,
|
|
442
|
+
showRuntimeErrors: showRuntimeErrors,
|
|
443
|
+
});
|