@cfp-studio/analyzer-api 0.1.13 → 0.1.15
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/README.md +4 -2
- package/dist/analyzer.mjs +1 -1
- package/dist/index.d.mts +86 -1
- package/dist/index.mjs +3 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -30,13 +30,15 @@ analyzer.dispose();
|
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
可预览 ID:
|
|
33
|
+
|
|
33
34
|
```ts
|
|
34
|
-
import { generatePreviewableId, decodePreviewableId } from
|
|
35
|
-
const id = generatePreviewableId({ filePath:
|
|
35
|
+
import { generatePreviewableId, decodePreviewableId } from "@cfp-studio/analyzer-api";
|
|
36
|
+
const id = generatePreviewableId({ filePath: "/src/App.tsx", name: "App" });
|
|
36
37
|
const { filePath, name } = decodePreviewableId(id);
|
|
37
38
|
```
|
|
38
39
|
|
|
39
40
|
## 依赖
|
|
41
|
+
|
|
40
42
|
- `@cfp-studio/serializable-values`:可序列化值类型
|
|
41
43
|
- `@cfp-studio/type-analyzer`:TS 类型分析
|
|
42
44
|
- `@cfp-studio/vfs`:虚拟文件系统
|
package/dist/analyzer.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{factoryWithDefaultOptions as e}from"./factory.mjs";import{createTypeAnalyzer as
|
|
1
|
+
import{factoryWithDefaultOptions as e}from"./factory.mjs";import t from"node:path";import{createTypeAnalyzer as n}from"@cfp-studio/type-analyzer";function r(r){return e(({rootDir:e,reader:i,logger:a})=>{let o={rootDir:e,reader:i,logger:a},s=n({rootDir:e,warn:a.warn.bind(a),...r.typeAnalyzerOptions(o)}),c=r.toTypeScriptPath??(e=>e);return{typeAnalyzer:s,crawlFiles:async n=>{let i=n.map(n=>t.isAbsolute(n)?n:t.join(e,n)),a=s.analyze(i.map(c)),l=[],u=[];for(let e of i)for(let t of await r.crawlFile({...o,resolver:a,absoluteFilePath:e}))`exported`in t?l.push(t):u.push(t);return{components:l,stories:u}},dispose:()=>{s.dispose()}}})}export{r as createAnalyzer};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,91 @@
|
|
|
1
1
|
import { a as BasicComponent, c as FileSourcePosition, d as StoryArgs, i as BasePreviewable, l as Story, n as AnalyzerFactory, o as Component, r as AnalyzerOptions, s as ComponentAnalysis, t as Analyzer, u as StoryAnalysis } from "./api-3X9rm2RT.mjs";
|
|
2
2
|
import { ValueType } from "@cfp-studio/type-analyzer";
|
|
3
3
|
|
|
4
|
+
//#region src/error-panel.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* 渲染错误面板:在**预览区域内部**显示错误,而不是白屏(`docs/todo/39`)。
|
|
7
|
+
*
|
|
8
|
+
* ## 为什么放在这里(`analyzer-api`)
|
|
9
|
+
*
|
|
10
|
+
* 5 个框架插件(react / vue3 / svelte / solid / preact)**都依赖本包**,
|
|
11
|
+
* 且都面临同一个问题:「被预览组件抛错 → 整片白屏,用户只知道坏了,不知道为什么」。
|
|
12
|
+
*
|
|
13
|
+
* 各插件各写一份错误 UI 会重复且容易漂移;这里用**纯 DOM**实现,
|
|
14
|
+
* 不引入任何框架依赖,因此 5 个插件都能直接用。
|
|
15
|
+
*
|
|
16
|
+
* ## 设计约束
|
|
17
|
+
*
|
|
18
|
+
* 1. **内联样式**:面板渲染在用户页面之上,会继承用户的全局 CSS
|
|
19
|
+
* (Umi / antd 的 reset 等)。用内联样式避免被覆盖。
|
|
20
|
+
* 2. **不污染 Runtime Tree**:面板元素带 `data-preview-error-panel`,
|
|
21
|
+
* `iframe` 的 `runtime-bridge.walk()` 会跳过它 —— 否则错误面板会出现在组件树里。
|
|
22
|
+
* 3. **不写源码、不发网络**:纯本地渲染。
|
|
23
|
+
* 4. **独立于框架渲染器**:面板挂在 `document.body` 下,不放进 `#root`(原因见 `renderErrorPanel`)。
|
|
24
|
+
*/
|
|
25
|
+
interface RenderErrorPanelOptions {
|
|
26
|
+
/** 错误本体(`Error` 或任何被 throw 的值)。 */
|
|
27
|
+
error: unknown;
|
|
28
|
+
/** 被预览的可预览项名称(如 `Button`、`Default`),用于标题。 */
|
|
29
|
+
previewableName?: string;
|
|
30
|
+
/**
|
|
31
|
+
* React / Preact 的 `componentStack`,能指出**哪个组件**抛的错。
|
|
32
|
+
* 其它框架可为空。
|
|
33
|
+
*/
|
|
34
|
+
componentStack?: string | null;
|
|
35
|
+
}
|
|
36
|
+
/** 面板元素的标记属性:`runtime-bridge.walk()` 据此跳过。 */
|
|
37
|
+
declare const ERROR_PANEL_ATTRIBUTE = "data-preview-error-panel";
|
|
38
|
+
/**
|
|
39
|
+
* 错误遮罩层的 id:整个预览页只存在一个。
|
|
40
|
+
*
|
|
41
|
+
* 固定在 `document.body` 下,**不放进 `#root`** —— `#root` 由框架的渲染器独占
|
|
42
|
+
*(ReactDOM 清非托管子节点、Vue/Solid 同理),面板放进去会跟着一起被抹掉。
|
|
43
|
+
*/
|
|
44
|
+
declare const ERROR_PANEL_ID = "cfp-error-panel";
|
|
45
|
+
/**
|
|
46
|
+
* 把错误面板渲染成预览页的**独立覆盖层**。
|
|
47
|
+
*
|
|
48
|
+
* 返回一个清理函数,移除面板。重复调用会先移除旧面板(全页只留一个)。
|
|
49
|
+
*
|
|
50
|
+
* ## 为什么不再写进传进来的 container
|
|
51
|
+
*
|
|
52
|
+
* 旧实现 `container.appendChild(panel)` 把面板塞进 `#root`,实测症状(真实反馈):
|
|
53
|
+
* 「错误页面闪现一下,然后被正常页面/白屏覆盖」。原因是双重的:
|
|
54
|
+
* 1. `#root` 归框架渲染器管,它一重渲染就清掉非托管的子节点;
|
|
55
|
+
* 2. `runNewRender` 的 catch 会 `root.innerHTML = rootHtml` 回滚快照。
|
|
56
|
+
*
|
|
57
|
+
* 所以面板必须活在**框架渲染器管不到的地方**。`container` 参数保留仅用于
|
|
58
|
+
* 「遮罩要盖在哪个区域」—— 目前是整页,忽略该参数也符合语义,保留它是为了
|
|
59
|
+
* 兼容 5 个插件现有的调用点。
|
|
60
|
+
*
|
|
61
|
+
* ## 遮罩(`options.dimBackdrop`,默认开)
|
|
62
|
+
*
|
|
63
|
+
* 保存后失败时,上一次成功的内容可能还在(`runNewRender` 会回填快照)。
|
|
64
|
+
* 面板直接用半透明遮罩铺满并阻止穿透点击,避免成功画面干扰阅读错误信息。
|
|
65
|
+
*/
|
|
66
|
+
declare function renderErrorPanel(_container: Element, options: RenderErrorPanelOptions & {
|
|
67
|
+
dimBackdrop?: boolean;
|
|
68
|
+
}): () => void;
|
|
69
|
+
/**
|
|
70
|
+
* 移除全页的错误面板(幂等)。
|
|
71
|
+
*
|
|
72
|
+
* `container` 参数保留只是为了兼容旧调用点 —— 面板现在挂在 `document.body` 下,
|
|
73
|
+
* 已不依赖具体容器。
|
|
74
|
+
*/
|
|
75
|
+
declare function clearErrorPanel(_container?: Element): void;
|
|
76
|
+
/** 当前是否正显示错误面板(供宿主判断要不要保留旧画面)。 */
|
|
77
|
+
declare function isErrorPanelVisible(): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* 把任意被 throw 的值归一成 `{ message, stack }`。
|
|
80
|
+
*
|
|
81
|
+
* 项目里见过 `throw "字符串"`、`throw { message }` 这类非 Error 值,
|
|
82
|
+
* 直接 `error.message` 会拿到 `undefined`。
|
|
83
|
+
*/
|
|
84
|
+
declare function describeError(error: unknown): {
|
|
85
|
+
message: string;
|
|
86
|
+
stack: string | undefined;
|
|
87
|
+
};
|
|
88
|
+
//#endregion
|
|
4
89
|
//#region src/previewable-id.d.ts
|
|
5
90
|
type PreviewableId = {
|
|
6
91
|
filePath: string;
|
|
@@ -54,4 +139,4 @@ type WrapperLanguage = "tsx" | "jsx" | "vue" | "svelte" | "ts" | "js";
|
|
|
54
139
|
/** 默认 wrapper 路径:`__previewjs__/Wrapper.<language>`。 */
|
|
55
140
|
declare function wrapperPathFor(language: WrapperLanguage): string;
|
|
56
141
|
//#endregion
|
|
57
|
-
export { Analyzer, AnalyzerFactory, AnalyzerOptions, BasePreviewable, BasicComponent, Component, ComponentAnalysis, FileSourcePosition, PreviewableId, SpecialTypes, Story, StoryAnalysis, StoryArgs, WrapperLanguage, decodePreviewableId, defineSpecialTypes, generatePreviewableId, mergeSpecialTypes, wrapperPathFor };
|
|
142
|
+
export { Analyzer, AnalyzerFactory, AnalyzerOptions, BasePreviewable, BasicComponent, Component, ComponentAnalysis, ERROR_PANEL_ATTRIBUTE, ERROR_PANEL_ID, FileSourcePosition, PreviewableId, RenderErrorPanelOptions, SpecialTypes, Story, StoryAnalysis, StoryArgs, WrapperLanguage, clearErrorPanel, decodePreviewableId, defineSpecialTypes, describeError, generatePreviewableId, isErrorPanelVisible, mergeSpecialTypes, renderErrorPanel, wrapperPathFor };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
function
|
|
1
|
+
const e=`data-preview-error-panel`,t=`cfp-error-panel`;function n(n,i){r();let{error:o,previewableName:c,componentStack:l}=i,{message:u,stack:d}=a(o),f=i.dimBackdrop!==!1,p=document.getElementById(`cfp-error-panel`)??document.createElement(`div`);p.id=t,p.setAttribute(e,``),p.style.cssText=[`position: fixed`,`inset: 0`,`z-index: 2147483647`,`box-sizing: border-box`,`margin: 0`,`padding: 24px`,`overflow: auto`,`display: flex`,`align-items: flex-start`,`justify-content: center`,f?`background: rgba(24, 24, 27, 0.55)`:`background: transparent`,f?`pointer-events: auto`:`pointer-events: none`].join(`;`);let m=document.createElement(`div`);m.setAttribute(e,``),m.style.cssText=[`position: relative`,`box-sizing: border-box`,`pointer-events: auto`,`width: 100%`,`max-width: 760px`,`margin: 0`,`padding: 20px 24px`,`font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif`,`font-size: 13px`,`line-height: 1.6`,`color: #c0392b`,`background: #fff5f5`,`border: 1px solid #f5b7b1`,`border-radius: 8px`,`box-shadow: 0 8px 32px rgba(0, 0, 0, 0.28)`,`text-align: left`].join(`;`);let h=document.createElement(`div`);h.style.cssText=`font-size: 14px; font-weight: 600; margin-bottom: 8px`,h.textContent=c?`${c} 渲染失败`:`组件渲染失败`;let g=document.createElement(`pre`);if(g.style.cssText=[`margin: 0 0 12px`,`padding: 0`,`font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`,`font-size: 12px`,`line-height: 1.5`,`white-space: pre-wrap`,`word-break: break-word`,`color: #922b21`].join(`;`),g.textContent=u,m.appendChild(h),m.appendChild(g),l){let e=document.createElement(`pre`);e.style.cssText=[`margin: 0`,`padding: 10px 12px`,`background: rgba(192, 57, 43, 0.06)`,`border-radius: 6px`,`font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`,`font-size: 11px`,`line-height: 1.5`,`max-height: 240px`,`overflow: auto`,`white-space: pre-wrap`,`word-break: break-word`,`color: #7b241c`].join(`;`),e.textContent=s(l,12),m.appendChild(e)}if(!l&&d){let e=document.createElement(`pre`);e.style.cssText=[`margin: 0`,`padding: 10px 12px`,`background: rgba(192, 57, 43, 0.06)`,`border-radius: 6px`,`font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`,`font-size: 11px`,`line-height: 1.5`,`max-height: 200px`,`overflow: auto`,`white-space: pre-wrap`,`word-break: break-word`,`color: #7b241c`].join(`;`),e.textContent=s(d,6),m.appendChild(e)}return p.appendChild(m),p.isConnected||document.body.appendChild(p),()=>{p.remove()}}function r(n){document.getElementById(t)?.remove(),document.querySelectorAll(`[${e}]`).forEach(e=>e.remove())}function i(){return document.getElementById(t)!==null}function a(e){if(e instanceof Error)return{message:e.message||e.name,stack:e.stack};if(typeof e==`string`)return{message:e,stack:void 0};if(e&&typeof e==`object`){let t=e;return{message:typeof t.message==`string`?t.message:o(e),stack:typeof t.stack==`string`?t.stack:void 0}}return{message:String(e),stack:void 0}}function o(e){try{return JSON.stringify(e)}catch{return String(e)}}function s(e,t){let n=e.split(`
|
|
2
|
+
`);return n.length>t?`${n.slice(0,t).join(`
|
|
3
|
+
`)}\n…`:e}function c(e){return`${e.filePath.replace(/\\/g,`/`)}:${e.name}`}function l(e){if(e.lastIndexOf(`:`)===-1)throw Error(`无效的 ID: "${e}"`);let[t,n]=e.split(`:`);return{filePath:t,name:n}}function u(e){return e}function d(...e){let t={};for(let n of e)n&&Object.assign(t,n);return t}function f(e){return`__previewjs__/Wrapper.${e}`}export{e as ERROR_PANEL_ATTRIBUTE,t as ERROR_PANEL_ID,r as clearErrorPanel,l as decodePreviewableId,u as defineSpecialTypes,a as describeError,c as generatePreviewableId,i as isErrorPanelVisible,d as mergeSpecialTypes,n as renderErrorPanel,f as wrapperPathFor};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfp-studio/analyzer-api",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"pino": "9.11.0",
|
|
26
26
|
"pino-pretty": "13.1.1",
|
|
27
|
-
"@cfp-studio/
|
|
28
|
-
"@cfp-studio/vfs": "2.1.
|
|
29
|
-
"@cfp-studio/
|
|
27
|
+
"@cfp-studio/serializable-values": "9.0.8",
|
|
28
|
+
"@cfp-studio/vfs": "2.1.8",
|
|
29
|
+
"@cfp-studio/type-analyzer": "10.1.8"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"typescript": "5.9.3",
|