@cfp-studio/analyzer-api 0.1.12 → 0.1.14

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Zenc Labs Pty Ltd
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Zenc Labs Pty Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,56 +1,57 @@
1
- # @cfp-studio/analyzer-api
2
-
3
- 核心分析 API,用于组件/Story/TS 类型的静态分析、可预览 ID 生成与解析、分析器工厂等。为 CFP Studio 设计器和相关工具提供统一的类型分析与组件元数据抽象。
4
-
5
- ## 特性
6
-
7
- - 提供统一的 `Analyzer` 接口,支持 TS 组件/Story 批量分析、属性/类型收集、资源释放
8
- - 支持可预览 ID 的生成与解析,便于组件/Story 的唯一定位
9
- - 支持分析器工厂模式,可自定义 reader/logger
10
- - 类型定义清晰,便于二次开发和类型推断
11
- - 内置 Vitest 测试用例
12
-
13
- ## 主要 API/模块
14
-
15
- - `Analyzer`/`AnalyzerFactory`:分析器主接口与工厂
16
- - `Component`/`Story`/`BasePreviewable`:可预览对象抽象
17
- - `generatePreviewableId`/`decodePreviewableId`:可预览 ID 生成与解析
18
- - `factoryWithDefaultOptions`:分析器工厂默认参数包装
19
-
20
- ## 用法示例
21
-
22
- ```ts
23
- import { AnalyzerFactory } from '@cfp-studio/analyzer-api';
24
-
25
- const factory: AnalyzerFactory = ...;
26
- const analyzer = factory({ rootDir: '/project/root' });
27
- const { components, stories } = await analyzer.crawlFiles(['src/App.tsx']);
28
- await components[0].analyze();
29
- analyzer.dispose();
30
- ```
31
-
32
- 可预览 ID:
33
- ```ts
34
- import { generatePreviewableId, decodePreviewableId } from '@cfp-studio/analyzer-api';
35
- const id = generatePreviewableId({ filePath: '/src/App.tsx', name: 'App' });
36
- const { filePath, name } = decodePreviewableId(id);
37
- ```
38
-
39
- ## 依赖
40
- - `@cfp-studio/jsx-processor`:JSX 结构分析
41
- - `@cfp-studio/serializable-values`:可序列化值类型
42
- - `@cfp-studio/type-analyzer`:TS 类型分析
43
- - `@cfp-studio/vfs`:虚拟文件系统
44
- - `pino`:日志
45
- - `unbuild`:打包
46
- - `vitest`:测试
47
-
48
- ## 构建
49
-
50
- ```sh
51
- pnpm build
52
- ```
53
-
54
- ## 许可证
55
-
56
- MIT
1
+ # @cfp-studio/analyzer-api
2
+
3
+ 核心分析 API,用于组件/Story/TS 类型的静态分析、可预览 ID 生成与解析、分析器工厂等。为 CFP Studio 设计器和相关工具提供统一的类型分析与组件元数据抽象。
4
+
5
+ ## 特性
6
+
7
+ - 提供统一的 `Analyzer` 接口,支持 TS 组件/Story 批量分析、属性/类型收集、资源释放
8
+ - 支持可预览 ID 的生成与解析,便于组件/Story 的唯一定位
9
+ - 支持分析器工厂模式,可自定义 reader/logger
10
+ - 类型定义清晰,便于二次开发和类型推断
11
+ - 内置 Vitest 测试用例
12
+
13
+ ## 主要 API/模块
14
+
15
+ - `Analyzer`/`AnalyzerFactory`:分析器主接口与工厂
16
+ - `Component`/`Story`/`BasePreviewable`:可预览对象抽象
17
+ - `generatePreviewableId`/`decodePreviewableId`:可预览 ID 生成与解析
18
+ - `factoryWithDefaultOptions`:分析器工厂默认参数包装
19
+
20
+ ## 用法示例
21
+
22
+ ```ts
23
+ import { AnalyzerFactory } from '@cfp-studio/analyzer-api';
24
+
25
+ const factory: AnalyzerFactory = ...;
26
+ const analyzer = factory({ rootDir: '/project/root' });
27
+ const { components, stories } = await analyzer.crawlFiles(['src/App.tsx']);
28
+ await components[0].analyze();
29
+ analyzer.dispose();
30
+ ```
31
+
32
+ 可预览 ID:
33
+
34
+ ```ts
35
+ import { generatePreviewableId, decodePreviewableId } from "@cfp-studio/analyzer-api";
36
+ const id = generatePreviewableId({ filePath: "/src/App.tsx", name: "App" });
37
+ const { filePath, name } = decodePreviewableId(id);
38
+ ```
39
+
40
+ ## 依赖
41
+
42
+ - `@cfp-studio/serializable-values`:可序列化值类型
43
+ - `@cfp-studio/type-analyzer`:TS 类型分析
44
+ - `@cfp-studio/vfs`:虚拟文件系统
45
+ - `pino`:日志
46
+ - `tsdown`:打包
47
+ - `vitest`:测试
48
+
49
+ ## 构建
50
+
51
+ ```sh
52
+ pnpm build
53
+ ```
54
+
55
+ ## 许可证
56
+
57
+ MIT
@@ -0,0 +1,39 @@
1
+ import { l as Story, n as AnalyzerFactory, o as Component } from "./api-3X9rm2RT.mjs";
2
+ import { Reader } from "@cfp-studio/vfs";
3
+ import { Logger } from "pino";
4
+ import { TypeResolver, createTypeAnalyzer } from "@cfp-studio/type-analyzer";
5
+
6
+ //#region src/analyzer.d.ts
7
+ type CreateTypeAnalyzerOptions = Parameters<typeof createTypeAnalyzer>[0];
8
+ interface AnalyzerAdapterContext {
9
+ rootDir: string;
10
+ /** 已含骨架默认值的 reader(文件系统 reader)。需要额外映射时在其上堆叠。 */
11
+ reader: Reader;
12
+ logger: Logger;
13
+ }
14
+ interface CrawlFileContext extends AnalyzerAdapterContext {
15
+ resolver: TypeResolver;
16
+ absoluteFilePath: string;
17
+ }
18
+ interface AnalyzerAdapter {
19
+ /**
20
+ * `createTypeAnalyzer` 的选项。
21
+ *
22
+ * 骨架会补 `rootDir` / `warn`;这里只需要返回 `reader`(通常是 `context.reader` 或在其上
23
+ * 堆叠映射)、`specialTypes`、`tsCompilerOptions` 等框架相关部分。
24
+ */
25
+ typeAnalyzerOptions(context: AnalyzerAdapterContext): Omit<CreateTypeAnalyzerOptions, "rootDir" | "warn">;
26
+ /** 真实文件 → 虚拟 TypeScript 路径(如 `App.vue` → `App.vue.ts`)。默认恒等。 */
27
+ toTypeScriptPath?(absoluteFilePath: string): string;
28
+ /** 爬取单个文件里的可预览对象。 */
29
+ crawlFile(context: CrawlFileContext): Promise<Array<Component | Story>>;
30
+ }
31
+ /**
32
+ * 用 adapter 构造 `AnalyzerFactory`。
33
+ *
34
+ * 返回的 Analyzer 具备:`typeAnalyzer`、`crawlFiles`(含虚拟路径映射与 components/stories
35
+ * 分类)、`dispose`(透传到 typeAnalyzer)。
36
+ */
37
+ declare function createAnalyzer(adapter: AnalyzerAdapter): AnalyzerFactory;
38
+ //#endregion
39
+ export { AnalyzerAdapter, AnalyzerAdapterContext, CrawlFileContext, createAnalyzer };
@@ -0,0 +1 @@
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};
@@ -0,0 +1,140 @@
1
+ import { Reader } from "@cfp-studio/vfs";
2
+ import { Logger } from "pino";
3
+ import { CollectedTypes, TypeAnalyzer, ValueType } from "@cfp-studio/type-analyzer";
4
+ import { SerializableValue } from "@cfp-studio/serializable-values";
5
+
6
+ //#region src/api.d.ts
7
+ /**
8
+ * 定义 FileSourcePosition 类型,包含文件路径和位置信息
9
+ */
10
+ /**
11
+ * 定义 Analyzer 类型,包含类型分析器、文件爬取方法和资源释放方法
12
+ */
13
+ type Analyzer = {
14
+ /**
15
+ * 类型分析器,去除了 dispose 方法
16
+ */
17
+ typeAnalyzer: Omit<TypeAnalyzer, "dispose">;
18
+ /**
19
+ * 爬取指定文件路径的文件,返回组件和故事的集合
20
+ * @param filePaths - 要爬取的文件路径数组
21
+ * @returns 包含组件和故事数组的 Promise
22
+ */
23
+ crawlFiles: (filePaths: string[]) => Promise<{
24
+ components: Component[];
25
+ stories: Story[];
26
+ }>;
27
+ /**
28
+ * 释放 Analyzer 占用的资源
29
+ */
30
+ dispose: () => void;
31
+ };
32
+ type AnalyzerOptions = {
33
+ rootDir: string;
34
+ reader?: Reader;
35
+ logger?: Logger;
36
+ };
37
+ /**
38
+ * 定义 AnalyzerFactory 类型,是一个函数,用于创建 Analyzer 实例
39
+ * @param options - 创建 Analyzer 所需的选项
40
+ * @param options.rootDir - 根目录路径
41
+ * @param options.reader - 可选的读取器
42
+ * @param options.logger - 可选的日志记录器
43
+ * @returns 新创建的 Analyzer 实例
44
+ */
45
+ type AnalyzerFactory = (options: AnalyzerOptions) => Analyzer;
46
+ /**
47
+ * 定义 BasePreviewable 接口,作为可预览对象的基础接口
48
+ */
49
+ interface BasePreviewable {
50
+ /**
51
+ * 可预览对象的唯一标识符
52
+ */
53
+ id: string;
54
+ /**
55
+ * 可预览对象在文件中的源位置
56
+ */
57
+ sourcePosition: FileSourcePosition;
58
+ }
59
+ /**
60
+ * 定义 Component 接口,继承自 BasePreviewable 接口,代表一个组件
61
+ */
62
+ interface Component extends BasePreviewable {
63
+ /**
64
+ * 指示组件是否被导出
65
+ */
66
+ exported: boolean;
67
+ /**
68
+ * 分析组件,返回组件分析结果的 Promise
69
+ * @returns 包含组件属性和类型信息的 ComponentAnalysis 对象的 Promise
70
+ */
71
+ analyze: () => Promise<ComponentAnalysis>;
72
+ }
73
+ /**
74
+ * 定义 ComponentAnalysis 接口,包含组件分析的结果
75
+ */
76
+ interface ComponentAnalysis {
77
+ /**
78
+ * 组件的属性类型
79
+ */
80
+ props: ValueType;
81
+ /**
82
+ * 收集到的组件类型信息
83
+ */
84
+ types: CollectedTypes;
85
+ }
86
+ /**
87
+ * 定义 Story 接口,继承自 BasePreviewable 接口,代表一个故事
88
+ */
89
+ interface Story extends BasePreviewable {
90
+ /**
91
+ * 分析故事,返回故事分析结果的 Promise
92
+ * @returns 包含故事参数信息的 StoryAnalysis 对象的 Promise
93
+ */
94
+ analyze: () => Promise<StoryAnalysis>;
95
+ /**
96
+ * 与故事关联的基本组件,可能为 null
97
+ */
98
+ associatedComponent: BasicComponent | null;
99
+ }
100
+ /**
101
+ * 定义 StoryAnalysis 接口,包含故事分析的结果
102
+ */
103
+ interface StoryAnalysis {
104
+ /**
105
+ * 故事的参数,可能为 null
106
+ */
107
+ args: StoryArgs | null;
108
+ }
109
+ /**
110
+ * 定义 StoryArgs 类型,包含故事参数的源位置和值
111
+ */
112
+ type StoryArgs = {
113
+ /**
114
+ * 故事参数在文件中的源位置
115
+ */
116
+ sourcePosition: FileSourcePosition;
117
+ /**
118
+ * 故事参数的值
119
+ */
120
+ value: SerializableValue;
121
+ };
122
+ /**
123
+ * 定义 FileSourcePosition 类型,包含文件中某个位置的起始和结束索引
124
+ */
125
+ type FileSourcePosition = {
126
+ /**
127
+ * 起始索引
128
+ */
129
+ start: number;
130
+ /**
131
+ * 结束索引
132
+ */
133
+ end: number;
134
+ };
135
+ /**
136
+ * 定义 BasicComponent 类型,是 Component 类型的部分属性集合
137
+ */
138
+ type BasicComponent = Pick<Component, "id" | "analyze">;
139
+ //#endregion
140
+ export { BasicComponent as a, FileSourcePosition as c, StoryArgs as d, BasePreviewable as i, Story as l, AnalyzerFactory as n, Component as o, AnalyzerOptions as r, ComponentAnalysis as s, Analyzer as t, StoryAnalysis as u };
@@ -1,15 +1,11 @@
1
- import { a as AnalyzerOptions, A as Analyzer, b as AnalyzerFactory } from './shared/analyzer-api.40adf6f7.mjs';
2
- import '@cfp-studio/serializable-values';
3
- import '@cfp-studio/type-analyzer';
4
- import '@cfp-studio/vfs';
5
- import 'pino';
6
- import '@cfp-studio/jsx-processor';
1
+ import { n as AnalyzerFactory, r as AnalyzerOptions, t as Analyzer } from "./api-3X9rm2RT.mjs";
7
2
 
3
+ //#region src/factory.d.ts
8
4
  /**
9
5
  * 给 AnalyzerFactory 工厂函数进行默认选项填充
10
6
  * @param factory - 工厂函数名,接受包含默认选项的 AnalyzerFactoryOptions 并返回一个 Analyzer 实例
11
7
  * @returns 一个新的 AnalyzerFactory 函数,该函数会在调用时填充默认选项
12
8
  */
13
9
  declare function factoryWithDefaultOptions(factory: (options: Required<AnalyzerOptions>) => Analyzer): AnalyzerFactory;
14
-
15
- export { factoryWithDefaultOptions };
10
+ //#endregion
11
+ export { factoryWithDefaultOptions };
package/dist/factory.mjs CHANGED
@@ -1 +1 @@
1
- import{createFileSystemReader as o}from"@cfp-studio/vfs";import t from"pino";import i from"pino-pretty";const{pino:a}=t,l=i;function m(r){return e=>r({...e,reader:e.reader||o(),logger:e.logger||a({level:"debug"},l({colorize:!0}))})}export{m as factoryWithDefaultOptions};
1
+ import{createFileSystemReader as e}from"@cfp-studio/vfs";import t from"pino";import n from"pino-pretty";const{pino:r}=t,i=n;function a(t){return n=>t({...n,reader:n.reader||e(),logger:n.logger||r({level:`debug`},i({colorize:!0}))})}export{a as factoryWithDefaultOptions};
package/dist/index.d.mts CHANGED
@@ -1,13 +1,62 @@
1
- export { A as Analyzer, b as AnalyzerFactory, a as AnalyzerOptions, B as BasePreviewable, f as BasicComponent, C as Component, c as ComponentAnalysis, F as FileSourcePosition, S as Story, d as StoryAnalysis, e as StoryArgs } from './shared/analyzer-api.40adf6f7.mjs';
2
- import '@cfp-studio/serializable-values';
3
- import '@cfp-studio/type-analyzer';
4
- import '@cfp-studio/vfs';
5
- import 'pino';
6
- import '@cfp-studio/jsx-processor';
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
+ import { ValueType } from "@cfp-studio/type-analyzer";
7
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. **内联样式**:面板渲染在用户页面的 `#root` 里,会继承用户的全局 CSS
19
+ * (Umi / antd 的 reset 等)。用内联样式避免被覆盖。
20
+ * 2. **不污染 Runtime Tree**:面板元素带 `data-preview-error-panel`,
21
+ * `iframe` 的 `runtime-bridge.walk()` 会跳过它 —— 否则错误面板会出现在组件树里。
22
+ * 3. **不写源码、不发网络**:纯本地渲染。
23
+ */
24
+ interface RenderErrorPanelOptions {
25
+ /** 错误本体(`Error` 或任何被 throw 的值)。 */
26
+ error: unknown;
27
+ /** 被预览的可预览项名称(如 `Button`、`Default`),用于标题。 */
28
+ previewableName?: string;
29
+ /**
30
+ * React / Preact 的 `componentStack`,能指出**哪个组件**抛的错。
31
+ * 其它框架可为空。
32
+ */
33
+ componentStack?: string | null;
34
+ }
35
+ /** 面板元素的标记属性:`runtime-bridge.walk()` 据此跳过。 */
36
+ declare const ERROR_PANEL_ATTRIBUTE = "data-preview-error-panel";
37
+ /**
38
+ * 把错误面板渲染到 `container`(通常是 `#root`)。
39
+ *
40
+ * 返回一个清理函数,移除面板。重复调用会先移除旧面板(同一个 container 只留一个)。
41
+ */
42
+ declare function renderErrorPanel(container: Element, options: RenderErrorPanelOptions): () => void;
43
+ /** 移除 container 里的错误面板(幂等)。 */
44
+ declare function clearErrorPanel(container: Element): void;
45
+ /**
46
+ * 把任意被 throw 的值归一成 `{ message, stack }`。
47
+ *
48
+ * 项目里见过 `throw "字符串"`、`throw { message }` 这类非 Error 值,
49
+ * 直接 `error.message` 会拿到 `undefined`。
50
+ */
51
+ declare function describeError(error: unknown): {
52
+ message: string;
53
+ stack: string | undefined;
54
+ };
55
+ //#endregion
56
+ //#region src/previewable-id.d.ts
8
57
  type PreviewableId = {
9
- filePath: string;
10
- name: string;
58
+ filePath: string;
59
+ name: string;
11
60
  };
12
61
  /**
13
62
  * 生成可预览 ID。
@@ -18,8 +67,8 @@ type PreviewableId = {
18
67
  * @returns 生成的可预览 ID,格式为 "文件路径:名称"。
19
68
  */
20
69
  declare function generatePreviewableId(options: {
21
- filePath: string;
22
- name: string;
70
+ filePath: string;
71
+ name: string;
23
72
  }): string;
24
73
  /**
25
74
  * 解码可预览 ID。
@@ -29,5 +78,32 @@ declare function generatePreviewableId(options: {
29
78
  * @throws 如果 ID 格式无效(不包含冒号),则抛出错误。
30
79
  */
31
80
  declare function decodePreviewableId(id: string): PreviewableId;
32
-
33
- export { type PreviewableId, decodePreviewableId, generatePreviewableId };
81
+ //#endregion
82
+ //#region src/special-types.d.ts
83
+ type SpecialTypes = Record<string, ValueType>;
84
+ /**
85
+ * 声明一组框架专属类型映射。
86
+ *
87
+ * 只是带类型的恒等函数:用于让各插件的 `special-types.ts` 形状一致,
88
+ * 并在合并处拿到 `SpecialTypes` 的类型约束。
89
+ */
90
+ declare function defineSpecialTypes(types: SpecialTypes): SpecialTypes;
91
+ /**
92
+ * 合并多组类型映射(后者覆盖前者)。
93
+ *
94
+ * 用途:默认映射(如 `Component → NODE_TYPE`)+ 框架专属映射。
95
+ */
96
+ declare function mergeSpecialTypes(...sets: Array<SpecialTypes | undefined>): SpecialTypes;
97
+ //#endregion
98
+ //#region src/wrapper.d.ts
99
+ /**
100
+ * 默认 wrapper 的路径约定。
101
+ *
102
+ * 5 个插件此前各自硬编码 `__previewjs__/Wrapper.<ext>`,差异只有扩展名。
103
+ * 收敛成一处后,新增框架只需给出语言扩展名。
104
+ */
105
+ type WrapperLanguage = "tsx" | "jsx" | "vue" | "svelte" | "ts" | "js";
106
+ /** 默认 wrapper 路径:`__previewjs__/Wrapper.<language>`。 */
107
+ declare function wrapperPathFor(language: WrapperLanguage): string;
108
+ //#endregion
109
+ export { Analyzer, AnalyzerFactory, AnalyzerOptions, BasePreviewable, BasicComponent, Component, ComponentAnalysis, ERROR_PANEL_ATTRIBUTE, FileSourcePosition, PreviewableId, RenderErrorPanelOptions, SpecialTypes, Story, StoryAnalysis, StoryArgs, WrapperLanguage, clearErrorPanel, decodePreviewableId, defineSpecialTypes, describeError, generatePreviewableId, mergeSpecialTypes, renderErrorPanel, wrapperPathFor };
package/dist/index.mjs CHANGED
@@ -1 +1,3 @@
1
- function t(e){return`${e.filePath.replace(/\\/g,"/")}:${e.name}`}function a(e){if(e.lastIndexOf(":")===-1)throw new Error(`\u65E0\u6548\u7684 ID: "${e}"`);const[n,r]=e.split(":");return{filePath:n,name:r}}export{a as decodePreviewableId,t as generatePreviewableId};
1
+ const e=`data-preview-error-panel`;function t(t,i){n(t);let{error:o,previewableName:s,componentStack:c}=i,{message:l,stack:u}=r(o),d=document.createElement(`div`);d.setAttribute(e,``),d.style.cssText=[`position: relative`,`box-sizing: border-box`,`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`,`text-align: left`].join(`;`);let f=document.createElement(`div`);f.style.cssText=`font-size: 14px; font-weight: 600; margin-bottom: 8px`,f.textContent=s?`${s} 渲染失败`:`组件渲染失败`;let p=document.createElement(`pre`);if(p.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(`;`),p.textContent=l,d.appendChild(f),d.appendChild(p),c){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=a(c,12),d.appendChild(e)}if(!c&&u){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=a(u,6),d.appendChild(e)}return t.appendChild(d),()=>{d.remove()}}function n(t){t.querySelectorAll(`[${e}]`).forEach(e=>e.remove())}function r(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:i(e),stack:typeof t.stack==`string`?t.stack:void 0}}return{message:String(e),stack:void 0}}function i(e){try{return JSON.stringify(e)}catch{return String(e)}}function a(e,t){let n=e.split(`
2
+ `);return n.length>t?`${n.slice(0,t).join(`
3
+ `)}\n…`:e}function o(e){return`${e.filePath.replace(/\\/g,`/`)}:${e.name}`}function s(e){if(e.lastIndexOf(`:`)===-1)throw Error(`无效的 ID: "${e}"`);let[t,n]=e.split(`:`);return{filePath:t,name:n}}function c(e){return e}function l(...e){let t={};for(let n of e)n&&Object.assign(t,n);return t}function u(e){return`__previewjs__/Wrapper.${e}`}export{e as ERROR_PANEL_ATTRIBUTE,n as clearErrorPanel,s as decodePreviewableId,c as defineSpecialTypes,r as describeError,o as generatePreviewableId,l as mergeSpecialTypes,t as renderErrorPanel,u as wrapperPathFor};
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@cfp-studio/analyzer-api",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "license": "MIT",
5
+ "files": [
6
+ "dist"
7
+ ],
5
8
  "type": "module",
6
9
  "main": "./dist/index.mjs",
7
10
  "exports": {
@@ -12,28 +15,35 @@
12
15
  "./factory": {
13
16
  "import": "./dist/factory.mjs",
14
17
  "types": "./dist/factory.d.mts"
18
+ },
19
+ "./analyzer": {
20
+ "import": "./dist/analyzer.mjs",
21
+ "types": "./dist/analyzer.d.mts"
15
22
  }
16
23
  },
17
- "files": [
18
- "dist"
19
- ],
20
24
  "dependencies": {
21
- "@cfp-studio/jsx-processor": "^0.1.0",
22
- "@cfp-studio/serializable-values": "^9.0.7",
23
- "@cfp-studio/type-analyzer": "^10.1.8",
24
- "@cfp-studio/vfs": "^2.1.6",
25
- "pino": "^9.10.0",
26
- "pino-pretty": "^13.1.1"
25
+ "pino": "9.11.0",
26
+ "pino-pretty": "13.1.1",
27
+ "@cfp-studio/vfs": "2.1.8",
28
+ "@cfp-studio/serializable-values": "9.0.8",
29
+ "@cfp-studio/type-analyzer": "10.1.8"
27
30
  },
28
31
  "devDependencies": {
29
- "typescript": "5.7.3",
30
- "unbuild": "2.0.0",
31
- "vitest": "2.1.9"
32
+ "typescript": "5.9.3",
33
+ "vitest": "5.0.5"
34
+ },
35
+ "cfp": {
36
+ "domain": "web-preview",
37
+ "layer": "analysis"
32
38
  },
33
39
  "scripts": {
34
40
  "prepublish": "pnpm release",
35
- "build": "tsc && unbuild",
36
- "release": "tsc && unbuild --minify",
37
- "test": "vitest"
41
+ "build": "tsdown",
42
+ "typecheck": "tsc --noEmit",
43
+ "release": "tsdown --minify",
44
+ "test": "vitest run",
45
+ "test:watch": "vitest watch",
46
+ "lint": "oxlint",
47
+ "format": "oxfmt --write ."
38
48
  }
39
49
  }
package/dist/factory.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import { a as AnalyzerOptions, A as Analyzer, b as AnalyzerFactory } from './shared/analyzer-api.40adf6f7.js';
2
- import '@cfp-studio/serializable-values';
3
- import '@cfp-studio/type-analyzer';
4
- import '@cfp-studio/vfs';
5
- import 'pino';
6
- import '@cfp-studio/jsx-processor';
7
-
8
- /**
9
- * 给 AnalyzerFactory 工厂函数进行默认选项填充
10
- * @param factory - 工厂函数名,接受包含默认选项的 AnalyzerFactoryOptions 并返回一个 Analyzer 实例
11
- * @returns 一个新的 AnalyzerFactory 函数,该函数会在调用时填充默认选项
12
- */
13
- declare function factoryWithDefaultOptions(factory: (options: Required<AnalyzerOptions>) => Analyzer): AnalyzerFactory;
14
-
15
- export { factoryWithDefaultOptions };
package/dist/index.d.ts DELETED
@@ -1,33 +0,0 @@
1
- export { A as Analyzer, b as AnalyzerFactory, a as AnalyzerOptions, B as BasePreviewable, f as BasicComponent, C as Component, c as ComponentAnalysis, F as FileSourcePosition, S as Story, d as StoryAnalysis, e as StoryArgs } from './shared/analyzer-api.40adf6f7.js';
2
- import '@cfp-studio/serializable-values';
3
- import '@cfp-studio/type-analyzer';
4
- import '@cfp-studio/vfs';
5
- import 'pino';
6
- import '@cfp-studio/jsx-processor';
7
-
8
- type PreviewableId = {
9
- filePath: string;
10
- name: string;
11
- };
12
- /**
13
- * 生成可预览 ID。
14
- *
15
- * @param options - 包含文件路径和名称的选项对象。
16
- * @param options.filePath - 文件的路径。
17
- * @param options.name - 与文件相关的名称。
18
- * @returns 生成的可预览 ID,格式为 "文件路径:名称"。
19
- */
20
- declare function generatePreviewableId(options: {
21
- filePath: string;
22
- name: string;
23
- }): string;
24
- /**
25
- * 解码可预览 ID。
26
- *
27
- * @param id - 要解码的可预览 ID。
28
- * @returns 包含文件路径和名称的对象。
29
- * @throws 如果 ID 格式无效(不包含冒号),则抛出错误。
30
- */
31
- declare function decodePreviewableId(id: string): PreviewableId;
32
-
33
- export { type PreviewableId, decodePreviewableId, generatePreviewableId };
@@ -1,141 +0,0 @@
1
- import { SerializableValue } from '@cfp-studio/serializable-values';
2
- import { TypeAnalyzer, ValueType, CollectedTypes } from '@cfp-studio/type-analyzer';
3
- import { Reader } from '@cfp-studio/vfs';
4
- import { Logger } from 'pino';
5
- import { JSXNode } from '@cfp-studio/jsx-processor';
6
-
7
- /**
8
- * 定义 FileSourcePosition 类型,包含文件路径和位置信息
9
- */
10
- /**
11
- * 定义 Analyzer 类型,包含类型分析器、文件爬取方法和资源释放方法
12
- */
13
- type Analyzer = {
14
- /**
15
- * 类型分析器,去除了 dispose 方法
16
- */
17
- typeAnalyzer: Omit<TypeAnalyzer, "dispose">;
18
- /**
19
- * 爬取指定文件路径的文件,返回组件和故事的集合
20
- * @param filePaths - 要爬取的文件路径数组
21
- * @returns 包含组件和故事数组的 Promise
22
- */
23
- crawlFiles: (filePaths: string[]) => Promise<{
24
- components: Component[];
25
- stories: Story[];
26
- }>;
27
- /**
28
- * 释放 Analyzer 占用的资源
29
- */
30
- dispose: () => void;
31
- };
32
- type AnalyzerOptions = {
33
- rootDir: string;
34
- reader?: Reader;
35
- logger?: Logger;
36
- };
37
- /**
38
- * 定义 AnalyzerFactory 类型,是一个函数,用于创建 Analyzer 实例
39
- * @param options - 创建 Analyzer 所需的选项
40
- * @param options.rootDir - 根目录路径
41
- * @param options.reader - 可选的读取器
42
- * @param options.logger - 可选的日志记录器
43
- * @returns 新创建的 Analyzer 实例
44
- */
45
- type AnalyzerFactory = (options: AnalyzerOptions) => Analyzer;
46
- /**
47
- * 定义 BasePreviewable 接口,作为可预览对象的基础接口
48
- */
49
- interface BasePreviewable {
50
- /**
51
- * 可预览对象的唯一标识符
52
- */
53
- id: string;
54
- /**
55
- * 可预览对象在文件中的源位置
56
- */
57
- sourcePosition: FileSourcePosition;
58
- }
59
- /**
60
- * 定义 Component 接口,继承自 BasePreviewable 接口,代表一个组件
61
- */
62
- interface Component extends BasePreviewable {
63
- /**
64
- * 指示组件是否被导出
65
- */
66
- exported: boolean;
67
- /**
68
- * 分析组件,返回组件分析结果的 Promise
69
- * @returns 包含组件属性和类型信息的 ComponentAnalysis 对象的 Promise
70
- */
71
- analyze: (withTreeData?: boolean) => Promise<ComponentAnalysis>;
72
- }
73
- /**
74
- * 定义 ComponentAnalysis 接口,包含组件分析的结果
75
- */
76
- interface ComponentAnalysis {
77
- /**
78
- * 组件的属性类型
79
- */
80
- props: ValueType;
81
- /**
82
- * 收集到的组件类型信息
83
- */
84
- types: CollectedTypes;
85
- jsxTree?: JSXNode;
86
- }
87
- /**
88
- * 定义 Story 接口,继承自 BasePreviewable 接口,代表一个故事
89
- */
90
- interface Story extends BasePreviewable {
91
- /**
92
- * 分析故事,返回故事分析结果的 Promise
93
- * @returns 包含故事参数信息的 StoryAnalysis 对象的 Promise
94
- */
95
- analyze: () => Promise<StoryAnalysis>;
96
- /**
97
- * 与故事关联的基本组件,可能为 null
98
- */
99
- associatedComponent: BasicComponent | null;
100
- }
101
- /**
102
- * 定义 StoryAnalysis 接口,包含故事分析的结果
103
- */
104
- interface StoryAnalysis {
105
- /**
106
- * 故事的参数,可能为 null
107
- */
108
- args: StoryArgs | null;
109
- }
110
- /**
111
- * 定义 StoryArgs 类型,包含故事参数的源位置和值
112
- */
113
- type StoryArgs = {
114
- /**
115
- * 故事参数在文件中的源位置
116
- */
117
- sourcePosition: FileSourcePosition;
118
- /**
119
- * 故事参数的值
120
- */
121
- value: SerializableValue;
122
- };
123
- /**
124
- * 定义 FileSourcePosition 类型,包含文件中某个位置的起始和结束索引
125
- */
126
- type FileSourcePosition = {
127
- /**
128
- * 起始索引
129
- */
130
- start: number;
131
- /**
132
- * 结束索引
133
- */
134
- end: number;
135
- };
136
- /**
137
- * 定义 BasicComponent 类型,是 Component 类型的部分属性集合
138
- */
139
- type BasicComponent = Pick<Component, "id" | "analyze">;
140
-
141
- export type { Analyzer as A, BasePreviewable as B, Component as C, FileSourcePosition as F, Story as S, AnalyzerOptions as a, AnalyzerFactory as b, ComponentAnalysis as c, StoryAnalysis as d, StoryArgs as e, BasicComponent as f };
@@ -1,141 +0,0 @@
1
- import { SerializableValue } from '@cfp-studio/serializable-values';
2
- import { TypeAnalyzer, ValueType, CollectedTypes } from '@cfp-studio/type-analyzer';
3
- import { Reader } from '@cfp-studio/vfs';
4
- import { Logger } from 'pino';
5
- import { JSXNode } from '@cfp-studio/jsx-processor';
6
-
7
- /**
8
- * 定义 FileSourcePosition 类型,包含文件路径和位置信息
9
- */
10
- /**
11
- * 定义 Analyzer 类型,包含类型分析器、文件爬取方法和资源释放方法
12
- */
13
- type Analyzer = {
14
- /**
15
- * 类型分析器,去除了 dispose 方法
16
- */
17
- typeAnalyzer: Omit<TypeAnalyzer, "dispose">;
18
- /**
19
- * 爬取指定文件路径的文件,返回组件和故事的集合
20
- * @param filePaths - 要爬取的文件路径数组
21
- * @returns 包含组件和故事数组的 Promise
22
- */
23
- crawlFiles: (filePaths: string[]) => Promise<{
24
- components: Component[];
25
- stories: Story[];
26
- }>;
27
- /**
28
- * 释放 Analyzer 占用的资源
29
- */
30
- dispose: () => void;
31
- };
32
- type AnalyzerOptions = {
33
- rootDir: string;
34
- reader?: Reader;
35
- logger?: Logger;
36
- };
37
- /**
38
- * 定义 AnalyzerFactory 类型,是一个函数,用于创建 Analyzer 实例
39
- * @param options - 创建 Analyzer 所需的选项
40
- * @param options.rootDir - 根目录路径
41
- * @param options.reader - 可选的读取器
42
- * @param options.logger - 可选的日志记录器
43
- * @returns 新创建的 Analyzer 实例
44
- */
45
- type AnalyzerFactory = (options: AnalyzerOptions) => Analyzer;
46
- /**
47
- * 定义 BasePreviewable 接口,作为可预览对象的基础接口
48
- */
49
- interface BasePreviewable {
50
- /**
51
- * 可预览对象的唯一标识符
52
- */
53
- id: string;
54
- /**
55
- * 可预览对象在文件中的源位置
56
- */
57
- sourcePosition: FileSourcePosition;
58
- }
59
- /**
60
- * 定义 Component 接口,继承自 BasePreviewable 接口,代表一个组件
61
- */
62
- interface Component extends BasePreviewable {
63
- /**
64
- * 指示组件是否被导出
65
- */
66
- exported: boolean;
67
- /**
68
- * 分析组件,返回组件分析结果的 Promise
69
- * @returns 包含组件属性和类型信息的 ComponentAnalysis 对象的 Promise
70
- */
71
- analyze: (withTreeData?: boolean) => Promise<ComponentAnalysis>;
72
- }
73
- /**
74
- * 定义 ComponentAnalysis 接口,包含组件分析的结果
75
- */
76
- interface ComponentAnalysis {
77
- /**
78
- * 组件的属性类型
79
- */
80
- props: ValueType;
81
- /**
82
- * 收集到的组件类型信息
83
- */
84
- types: CollectedTypes;
85
- jsxTree?: JSXNode;
86
- }
87
- /**
88
- * 定义 Story 接口,继承自 BasePreviewable 接口,代表一个故事
89
- */
90
- interface Story extends BasePreviewable {
91
- /**
92
- * 分析故事,返回故事分析结果的 Promise
93
- * @returns 包含故事参数信息的 StoryAnalysis 对象的 Promise
94
- */
95
- analyze: () => Promise<StoryAnalysis>;
96
- /**
97
- * 与故事关联的基本组件,可能为 null
98
- */
99
- associatedComponent: BasicComponent | null;
100
- }
101
- /**
102
- * 定义 StoryAnalysis 接口,包含故事分析的结果
103
- */
104
- interface StoryAnalysis {
105
- /**
106
- * 故事的参数,可能为 null
107
- */
108
- args: StoryArgs | null;
109
- }
110
- /**
111
- * 定义 StoryArgs 类型,包含故事参数的源位置和值
112
- */
113
- type StoryArgs = {
114
- /**
115
- * 故事参数在文件中的源位置
116
- */
117
- sourcePosition: FileSourcePosition;
118
- /**
119
- * 故事参数的值
120
- */
121
- value: SerializableValue;
122
- };
123
- /**
124
- * 定义 FileSourcePosition 类型,包含文件中某个位置的起始和结束索引
125
- */
126
- type FileSourcePosition = {
127
- /**
128
- * 起始索引
129
- */
130
- start: number;
131
- /**
132
- * 结束索引
133
- */
134
- end: number;
135
- };
136
- /**
137
- * 定义 BasicComponent 类型,是 Component 类型的部分属性集合
138
- */
139
- type BasicComponent = Pick<Component, "id" | "analyze">;
140
-
141
- export type { Analyzer as A, BasePreviewable as B, Component as C, FileSourcePosition as F, Story as S, AnalyzerOptions as a, AnalyzerFactory as b, ComponentAnalysis as c, StoryAnalysis as d, StoryArgs as e, BasicComponent as f };