@cfp-studio/plugin-react 17.3.2 → 17.3.4
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/package.json +8 -8
- package/preview/error-boundary.tsx +75 -1
- package/preview/index.tsx +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfp-studio/plugin-react",
|
|
3
|
-
"version": "17.3.
|
|
3
|
+
"version": "17.3.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -15,11 +15,11 @@
|
|
|
15
15
|
"@vitejs/plugin-react": "4.3.4",
|
|
16
16
|
"esbuild-plugin-polyfill-node": "0.3.0",
|
|
17
17
|
"typescript": "5.9.3",
|
|
18
|
-
"@cfp-studio/analyzer-api": "0.1.13",
|
|
19
|
-
"@cfp-studio/storybook-helpers": "6.0.2",
|
|
20
18
|
"@cfp-studio/serializable-values": "9.0.8",
|
|
21
|
-
"@cfp-studio/
|
|
22
|
-
"@cfp-studio/vfs": "2.1.8"
|
|
19
|
+
"@cfp-studio/storybook-helpers": "6.0.2",
|
|
20
|
+
"@cfp-studio/vfs": "2.1.8",
|
|
21
|
+
"@cfp-studio/analyzer-api": "0.1.15",
|
|
22
|
+
"@cfp-studio/type-analyzer": "10.1.8"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@playwright/test": "1.64.1",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"react-dom": "18.3.1",
|
|
34
34
|
"vite": "6.4.5",
|
|
35
35
|
"vitest": "5.0.5",
|
|
36
|
-
"@cfp-studio/core": "1.0.
|
|
37
|
-
"@cfp-studio/
|
|
38
|
-
"@cfp-studio/
|
|
36
|
+
"@cfp-studio/core": "1.0.10",
|
|
37
|
+
"@cfp-studio/iframe": "18.4.5",
|
|
38
|
+
"@cfp-studio/testing": "14.0.1"
|
|
39
39
|
},
|
|
40
40
|
"engines": {
|
|
41
41
|
"node": ">=20"
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { describeError, renderErrorPanel, type RenderErrorPanelOptions } from "@cfp-studio/analyzer-api";
|
|
1
2
|
import type { ReactNode } from "react";
|
|
2
3
|
import { Component } from "react";
|
|
3
4
|
|
|
4
5
|
type ErrorBoundaryProps = {
|
|
5
6
|
renderId: number;
|
|
6
7
|
children: ReactNode;
|
|
8
|
+
/** 被预览的可预览项名称,用于错误面板标题。 */
|
|
9
|
+
previewableName?: string;
|
|
7
10
|
};
|
|
8
11
|
|
|
9
12
|
let errorBoundaryInstance: ErrorBoundary | null = null;
|
|
@@ -18,11 +21,39 @@ export async function expectErrorBoundary(renderId: number, shouldAbortRender: (
|
|
|
18
21
|
return errorBoundaryInstance;
|
|
19
22
|
}
|
|
20
23
|
|
|
24
|
+
/**
|
|
25
|
+
* 渲染错误边界(`docs/todo/39`)。
|
|
26
|
+
*
|
|
27
|
+
* ## 为什么不能只 `return <></>`
|
|
28
|
+
*
|
|
29
|
+
* 旧实现捕获到错误后返回空片段 —— 于是**整个预览变白屏**,用户只知道坏了,
|
|
30
|
+
* 不知道是哪个组件的什么错误。而错误信息**已经在手上**(`state.error` /
|
|
31
|
+
* `componentStack`),白白丢掉。
|
|
32
|
+
*
|
|
33
|
+
* ## 现在的行为
|
|
34
|
+
*
|
|
35
|
+
* 捕获后渲染一个错误面板(`renderErrorPanel`,纯 DOM,见 `analyzer-api`),
|
|
36
|
+
* 显示:标题(哪个可预览项)→ 错误消息 → 组件栈(前 12 帧)。
|
|
37
|
+
*
|
|
38
|
+
* 面板只占**渲染区域内部的这块地方**;外层 UI(工具栏、Runtime Tree 等)不受影响。
|
|
39
|
+
*
|
|
40
|
+
* 面板挂在 `document.body` 的独立覆盖层上(**不在 `#root` 里**),生命周期也不跟随本
|
|
41
|
+
* 组件的挂载/卸载 —— 原因见 `mountPanel` 与 `componentWillUnmount`。
|
|
42
|
+
*
|
|
43
|
+
* ## 仍然会向上抛
|
|
44
|
+
*
|
|
45
|
+
* `index.tsx` 的 `render()` 会读 `state.error` 并 `throw`,让 `runNewRender` 的 catch
|
|
46
|
+
* 继续走 `reportEvent({kind:"error"})` —— 宿主侧的 `ErrorPage` 与日志仍然拿得到,
|
|
47
|
+
* 两条通路互补而非替代。
|
|
48
|
+
*/
|
|
21
49
|
export class ErrorBoundary extends Component<ErrorBoundaryProps> {
|
|
22
50
|
state = {
|
|
23
51
|
error: null as Error | null,
|
|
52
|
+
componentStack: null as string | null,
|
|
24
53
|
};
|
|
25
54
|
|
|
55
|
+
private cleanupPanel: (() => void) | null = null;
|
|
56
|
+
|
|
26
57
|
constructor(props: ErrorBoundaryProps) {
|
|
27
58
|
super(props);
|
|
28
59
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
@@ -33,11 +64,54 @@ export class ErrorBoundary extends Component<ErrorBoundaryProps> {
|
|
|
33
64
|
return { error };
|
|
34
65
|
}
|
|
35
66
|
|
|
67
|
+
componentDidCatch(error: Error, errorInfo: { componentStack?: string | null }) {
|
|
68
|
+
// `componentStack` 是定位「哪个组件抛的错」最直接的线索(React 16.6+ 提供)。
|
|
69
|
+
this.setState({ componentStack: errorInfo?.componentStack ?? null });
|
|
70
|
+
this.mountPanel(error, errorInfo?.componentStack ?? null);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
componentWillUnmount() {
|
|
74
|
+
// **故意不清面板**:面板现在挂在 `document.body` 的覆盖层上,生命周期与 React 无关。
|
|
75
|
+
//
|
|
76
|
+
// 之前在这里 `cleanupPanel()`,实测症状是「错误页面闪现一下就没了」:
|
|
77
|
+
// HMR / 重新渲染会卸载这个 Boundary,顺手把刚画出来的面板抹掉。
|
|
78
|
+
//
|
|
79
|
+
// 清面板的责任归于两处:下游重新渲染成功时由 `index.tsx` 调 `clearErrorPanel()`;
|
|
80
|
+
// 整个 iframe 重载时页面本来就从空白开始。
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 把错误面板画成预览页的覆盖层(见 `analyzer-api` 的 `renderErrorPanel`)。
|
|
85
|
+
*
|
|
86
|
+
* 不再写进 `#root`:那个容器归 ReactDOM 管,React 一重渲染就会清掉非托管的子节点。
|
|
87
|
+
*/
|
|
88
|
+
private mountPanel(error: unknown, componentStack: string | null) {
|
|
89
|
+
if (typeof document === "undefined") {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const options: RenderErrorPanelOptions = {
|
|
93
|
+
error,
|
|
94
|
+
previewableName: this.props.previewableName,
|
|
95
|
+
componentStack,
|
|
96
|
+
};
|
|
97
|
+
// 先清旧的(防止连续抛错时叠层),再画新的。
|
|
98
|
+
this.cleanupPanel?.();
|
|
99
|
+
this.cleanupPanel = renderErrorPanel(document.body, options);
|
|
100
|
+
}
|
|
101
|
+
|
|
36
102
|
render() {
|
|
37
103
|
if (this.state.error) {
|
|
38
|
-
|
|
104
|
+
// React 需要这里返回**合法节点**;真正的可见内容由 componentDidCatch 后的
|
|
105
|
+
// `mountPanel()` 写进 `#root`(那一步才能拿到 componentStack)。
|
|
106
|
+
// 返回 `null` 而不是 `<></>`:语义上更明确「这块不渲染任何东西」。
|
|
107
|
+
return null;
|
|
39
108
|
}
|
|
40
109
|
|
|
41
110
|
return this.props.children;
|
|
42
111
|
}
|
|
43
112
|
}
|
|
113
|
+
|
|
114
|
+
/** 供调试/测试:读取错误文本(不依赖 DOM)。 */
|
|
115
|
+
export function errorTextOf(error: unknown): string {
|
|
116
|
+
return describeError(error).message;
|
|
117
|
+
}
|
package/preview/index.tsx
CHANGED
|
@@ -45,7 +45,7 @@ export const loadRenderer: RendererLoader = async ({
|
|
|
45
45
|
: Previewable;
|
|
46
46
|
const Renderer = (props: any) => {
|
|
47
47
|
return (
|
|
48
|
-
<ErrorBoundary renderId={renderId}>
|
|
48
|
+
<ErrorBoundary renderId={renderId} previewableName={previewableName}>
|
|
49
49
|
<Wrapper>
|
|
50
50
|
{decorators.reduce(
|
|
51
51
|
(component, decorator) => () => decorator(component),
|