Skip to content

Commit 337456e

Browse files
committed
feat(type): 新增 @echarts-component/type 包用于类型扁平化处理
新增一个专门用于对 echarts 组件类型进行扁平化处理的包。该包通过 `type-flat` 工具读取 echarts 的类型定义,并按需生成简化后的类型文件。
1 parent 0b9f373 commit 337456e

10 files changed

Lines changed: 190 additions & 78 deletions

File tree

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
"echarts": "catalog:",
2828
"less": "catalog:",
2929
"terser": "catalog:",
30-
"ts-morph": "catalog:",
3130
"tsx": "catalog:",
32-
"type-fest": "catalog:",
3331
"typescript": "catalog:",
3432
"unocss": "catalog:",
3533
"vite": "catalog:",
@@ -40,6 +38,7 @@
4038
"pnpm": {
4139
"patchedDependencies": {
4240
"echarts": ".pnpm_patches/echarts.patch"
43-
}
41+
},
42+
"overrides": {}
4443
}
4544
}

packages/type/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
# `@echarts-component/type`

packages/type/flatten.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import fs from "node:fs";
2+
import path from "path";
3+
import { ComponentsType } from "./meta.ts";
4+
import tf from "type-flat";
5+
6+
const __dirname = path.dirname(new URL(import.meta.url).pathname);
7+
const Root_Dir = path.resolve(__dirname, ".");
8+
// 存放生成的类型
9+
const Type_Output_Dir = path.join(Root_Dir, "./types");
10+
// 读取echarts的类型定义文件位置
11+
const Echarts_Type_Dir = path.join(
12+
Root_Dir,
13+
"node_modules/echarts/types/dist/shared.d.ts"
14+
);
15+
16+
// async function run() {
17+
// // 清理types目录
18+
// if (fs.existsSync(Type_Output_Dir)) {
19+
// fs.rmSync(Type_Output_Dir, { recursive: true, force: true });
20+
// }
21+
// fs.mkdirSync(Type_Output_Dir);
22+
// // 初始化 ts-morph 项目
23+
// const tsMorph = new Project({
24+
// tsConfigFilePath: path.resolve(Root_Dir, "tsconfig.json"),
25+
// skipFileDependencyResolution: true,
26+
// });
27+
// // 读取所有的echarts类型定义
28+
// let sourceFile = tsMorph.addSourceFileAtPath(Echarts_Type_Dir);
29+
30+
// // 按照需要生成的组件类型进行扁平化处理
31+
// for (const comp of ComponentsType) {
32+
// // 生成类型文件
33+
// const file_path = path.join(Type_Output_Dir, comp.name);
34+
// output_file = tsMorph.createSourceFile(file_path + ".ts", "");
35+
// visited_types.clear();
36+
37+
// const type_define =
38+
// sourceFile.getInterface(comp.name) ||
39+
// sourceFile.getTypeAlias(comp.name) ||
40+
// sourceFile.getEnum(comp.name);
41+
42+
// if (!type_define) {
43+
// console.log(`❌ 组件类型 ${comp.name} 未找到`);
44+
// continue;
45+
// }
46+
// // 处理类型
47+
// const result = flattenTypeDefine(type_define);
48+
// // 写入文件
49+
50+
// output_file.addStatements(result);
51+
// // 格式化代码
52+
// output_file.formatText({
53+
// ensureNewLineAtEndOfFile: true,
54+
// indentSize: 2,
55+
// convertTabsToSpaces: true,
56+
// });
57+
58+
// await output_file.saveSync();
59+
// }
60+
61+
// console.log(`✅ 类型已扁平化生成到: ${Type_Output_Dir}`);
62+
// }
63+
64+
async function run() {
65+
// 清理types目录
66+
if (fs.existsSync(Type_Output_Dir)) {
67+
fs.rmSync(Type_Output_Dir, { recursive: true, force: true });
68+
}
69+
fs.mkdirSync(Type_Output_Dir);
70+
71+
// 读取所有的echarts类型定义
72+
let sourceFile = fs.readFileSync(Echarts_Type_Dir, { encoding: "utf-8" });
73+
74+
// 按照需要生成的组件类型进行扁平化处理
75+
for (const comp of ComponentsType) {
76+
// 生成类型文件
77+
const file_path = path.join(Type_Output_Dir, comp.name);
78+
79+
// 处理类型
80+
const result = tf.flatten(sourceFile, comp.name);
81+
// 写入文件
82+
fs.writeFileSync(file_path + ".ts", result);
83+
}
84+
85+
console.log(`✅ 类型已扁平化生成到: ${Type_Output_Dir}`);
86+
}
87+
88+
run();

packages/type/index.ts

Whitespace-only changes.

packages/type/meta.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export interface ComponentType {
2+
name: string;
3+
description?: string;
4+
}
5+
6+
/**
7+
* 需要扁平处理的组件类型定义
8+
*/
9+
export const ComponentsType: ComponentType[] = [
10+
// {
11+
// name: "GraphicComponentGroupOption",
12+
// },
13+
// {
14+
// name: "GraphicComponentImageOption",
15+
// },
16+
// {
17+
// name: "GraphicComponentTextOption",
18+
// },
19+
// {
20+
// name: "GraphicComponentZRPathOption",
21+
// },
22+
{
23+
name: "GeoJSONGeometryMultiPolygonCompressed",
24+
},
25+
];

packages/type/package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@echarts-component/type",
3+
"version": "0.10.2",
4+
"description": "Echarts component flatten type.",
5+
"type": "module",
6+
"sideEffects": false,
7+
"private": true,
8+
"scripts": {
9+
"flatten": "tsx flatten.ts ",
10+
"build": "pnpm run flatten && vite build"
11+
},
12+
"author": "hboot <bobolity@163.com>",
13+
"license": "MIT",
14+
"packageManager": "pnpm@10.4.1",
15+
"keywords": [
16+
"echarts",
17+
"components",
18+
"type"
19+
],
20+
"module": "./dist/index.js",
21+
"types": "./dist/index.d.ts",
22+
"files": [
23+
"dist",
24+
"package.json",
25+
"README.md",
26+
"LICENSE"
27+
],
28+
"repository": {
29+
"type": "git",
30+
"url": "https://github.com/ngd-b/echarts-component"
31+
},
32+
"homepage": "http://echarts-component.hboot.fun/",
33+
"bugs": {
34+
"url": "https://github.com/ngd-b/echarts-component/issues"
35+
},
36+
"devDependencies": {
37+
"@types/node": "catalog:",
38+
"echarts": "catalog:",
39+
"tsx": "catalog:",
40+
"typescript": "catalog:",
41+
"vite": "catalog:",
42+
"vite-plugin-dts": "catalog:"
43+
}
44+
}

packages/type/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "nodenext",
5+
"declaration": true,
6+
"emitDeclarationOnly": true,
7+
"declarationDir": "dist",
8+
"outDir": "dist",
9+
"esModuleInterop": true,
10+
"skipLibCheck": true,
11+
"moduleResolution": "nodenext",
12+
"allowImportingTsExtensions": true
13+
},
14+
}

packages/type/vite.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineConfig } from "vite";
2+
import dts from "vite-plugin-dts";
3+
4+
// https://vite.dev/config/
5+
export default defineConfig({
6+
plugins: [
7+
dts({
8+
insertTypesEntry: true,
9+
}),
10+
],
11+
build: {
12+
outDir: "dist",
13+
emptyOutDir: true,
14+
},
15+
});

0 commit comments

Comments
 (0)