@cfp-studio/plugin-preact 11.1.1 → 11.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfp-studio/plugin-preact",
3
- "version": "11.1.1",
3
+ "version": "11.1.2",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -13,10 +13,10 @@
13
13
  "dependencies": {
14
14
  "esbuild-plugin-polyfill-node": "0.3.0",
15
15
  "typescript": "5.9.3",
16
- "@cfp-studio/analyzer-api": "0.1.14",
17
- "@cfp-studio/serializable-values": "9.0.8",
16
+ "@cfp-studio/analyzer-api": "0.1.15",
18
17
  "@cfp-studio/storybook-helpers": "6.0.2",
19
18
  "@cfp-studio/vfs": "2.1.8",
19
+ "@cfp-studio/serializable-values": "9.0.8",
20
20
  "@cfp-studio/type-analyzer": "10.1.8"
21
21
  },
22
22
  "devDependencies": {
@@ -26,9 +26,9 @@
26
26
  "preact": "10.26.4",
27
27
  "rimraf": "6.0.1",
28
28
  "vitest": "5.0.5",
29
- "@cfp-studio/core": "1.0.9",
30
- "@cfp-studio/iframe": "18.4.4",
31
- "@cfp-studio/testing": "14.0.1"
29
+ "@cfp-studio/iframe": "18.4.5",
30
+ "@cfp-studio/testing": "14.0.1",
31
+ "@cfp-studio/core": "1.0.10"
32
32
  },
33
33
  "cfp": {
34
34
  "domain": "web-preview",
@@ -27,8 +27,11 @@ export async function expectErrorBoundary(renderId: number, shouldAbortRender: (
27
27
  * 旧实现 `componentDidCatch` 是空函数、`render` 返回 `<></>` —— 整片白屏,
28
28
  * 且错误信息(`state.error`)被丢掉。
29
29
  *
30
- * 现在改为把错误面板画进 `#root`(`renderErrorPanel`,纯 DOM,来自 `analyzer-api`),
31
- * 显示标题 / 错误消息 / 组件栈。
30
+ * 现在改为把错误面板画成预览页的**独立覆盖层**(`renderErrorPanel`,纯 DOM,
31
+ * 来自 `analyzer-api`),显示标题 / 错误消息 / 组件栈。
32
+ *
33
+ * 面板挂在 `document.body` 下(**不在 `#root` 里**),生命周期也不跟随本组件的
34
+ * 挂载/卸载 —— 原因见 `mountPanel` 与 `componentWillUnmount`。
32
35
  *
33
36
  * `index.tsx` 仍会读 `state.error` 并 `throw`,让宿主侧的 `ErrorPage` 与日志也拿得到 ——
34
37
  * 两条通路互补。
@@ -53,30 +56,41 @@ export class ErrorBoundary extends Component<ErrorBoundaryProps> {
53
56
 
54
57
  componentDidCatch(error: Error, errorInfo: { componentStack?: string | null }) {
55
58
  this.setState({ componentStack: errorInfo?.componentStack ?? null });
56
- this.mountPanel();
59
+ this.mountPanel(error, errorInfo?.componentStack ?? null);
57
60
  }
58
61
 
59
62
  componentWillUnmount() {
60
- this.cleanupPanel?.();
61
- this.cleanupPanel = null;
63
+ // **故意不清面板** —— 与 `plugin-react` 的同名实现保持一致。
64
+ //
65
+ // 面板挂在 `document.body` 的覆盖层上,生命周期与 Preact 无关。之前在这里
66
+ // `cleanupPanel()`,卸载 Boundary 时会顺手把刚画出来的面板抹掉
67
+ //(「错误页闪现一下就没了」)。
68
+ //
69
+ // 清面板的责任归于:重新渲染成功时由 `index.tsx` 调 `clearErrorPanel()`。
62
70
  }
63
71
 
64
- private mountPanel() {
65
- const container = typeof document === "undefined" ? null : document.getElementById("root");
66
- if (!container) {
72
+ /**
73
+ * 把错误面板画成预览页的覆盖层(见 `analyzer-api` 的 `renderErrorPanel`)。
74
+ *
75
+ * 不再写进 `#root`:那个容器归 Preact / 框架渲染器管,它一重渲染就会清掉
76
+ * 非托管的子节点。
77
+ */
78
+ private mountPanel(error: unknown, componentStack: string | null) {
79
+ if (typeof document === "undefined") {
67
80
  return;
68
81
  }
82
+ // 先清旧的(防止连续抛错时叠层),再画新的。
69
83
  this.cleanupPanel?.();
70
- this.cleanupPanel = renderErrorPanel(container, {
71
- error: this.state.error,
84
+ this.cleanupPanel = renderErrorPanel(document.body, {
85
+ error,
72
86
  previewableName: this.props.previewableName,
73
- componentStack: this.state.componentStack,
87
+ componentStack,
74
88
  });
75
89
  }
76
90
 
77
91
  render() {
78
92
  if (this.state.error) {
79
- // 可见内容由 `mountPanel()` 写进 `#root`(那一步才拿得到 componentStack)。
93
+ // 可见内容由 `mountPanel()` 写成 `document.body` 上的覆盖层(那一步才拿得到 componentStack)。
80
94
  return null;
81
95
  }
82
96
 
package/preview/index.tsx CHANGED
@@ -1,3 +1,4 @@
1
+ import { clearErrorPanel } from "@cfp-studio/analyzer-api";
1
2
  import type { GetPropsFn, RendererLoader } from "@cfp-studio/iframe";
2
3
  import { Fragment, createElement, render } from "preact";
3
4
 
@@ -69,6 +70,11 @@ export const loadRenderer: RendererLoader = async ({
69
70
  if (Previewable.play) {
70
71
  await Previewable.play({ canvasElement: container });
71
72
  }
73
+ // 渲染成功且没把错误重新抛出去:上一次的错误覆盖层该撤了。
74
+ //
75
+ // 面板挂在 `document.body` 上(不占 `#root`),所以上面的
76
+ // `container.innerHTML = ""` 带不走它 —— 必须显式清。
77
+ clearErrorPanel();
72
78
  },
73
79
  jsxFactory: createElement,
74
80
  };