Skip to content

Commit 17cfe84

Browse files
committed
feat: 优化HomePage首页组件,支持同步加载表格数据
1 parent 67cca5e commit 17cfe84

3 files changed

Lines changed: 55 additions & 82 deletions

File tree

.vitepress/sidebar.ts

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { SidebarConfig, VipPackageJSON, VipProject } from '@142vip/vitepress'
2-
import { defineVipSidebarConfig } from '@142vip/vitepress'
1+
import { defineVipSidebarConfig, SidebarConfig } from '@142vip/vitepress'
32

4-
enum ProjectId {
3+
export enum ProjectId {
54
BUSINESS = '商业模块',
65
TOOLS = '通用工具',
76
EGG = 'Egg.js框架',
@@ -86,73 +85,6 @@ export const sidebarConfig = defineVipSidebarConfig([
8685
},
8786
])
8887

89-
/**
90-
* 获取基本包信息
91-
* - 注意目录格式,例如:@packages/utils
92-
* @vite-ignore
93-
*/
94-
async function getBasePkgJSON(pkgDirName: string): Promise<VipPackageJSON> {
95-
// 参考格式:@packages/xxx @apps/xxx
96-
return await import(`@packages/${pkgDirName}/package.json`)
97-
}
98-
99-
/**
100-
* 获取apps目录下的模块
101-
* - @apps/vitepress-demo
102-
*/
103-
async function getAppsPkgJSON(pkgDirName: string): Promise<VipPackageJSON> {
104-
// 参考格式:@packages/xxx @apps/xxx
105-
return await import(`@apps/${pkgDirName}/package.json`)
106-
}
107-
108-
/**
109-
* 动态获取模块信息
110-
* - 注意:遍历侧边栏
111-
*/
112-
export async function getCoreProjectData(): Promise<VipProject[]> {
113-
const coreProjects: VipProject[] = []
114-
for (const { items = [], text = '' } of sidebarConfig) {
115-
// 过滤掉apps下的模块
116-
if (text?.includes(ProjectId.DEMO)) {
117-
continue
118-
}
119-
for (const { text: pkgName = '' } of items) {
120-
const pkgDirName = pkgName.split('@142vip/')[1]
121-
const basePkg = await getBasePkgJSON(`${pkgDirName}`)
122-
coreProjects.push({
123-
...basePkg,
124-
// 约定:图标+文字
125-
id: text.split(' ')[0],
126-
changelog: `../changelogs/${pkgDirName}/changelog.html`,
127-
readme: `../packages/${pkgDirName}/index.html`,
128-
sourceCode: `https://github.com/142vip/core-x/tree/main/packages/${pkgDirName}/`,
129-
})
130-
}
131-
}
132-
return coreProjects
133-
}
134-
135-
/**
136-
* demo项目,用于项目展示
137-
*/
138-
export async function getExampleDemoTableData(): Promise<VipProject[]> {
139-
const pkgNames = ['egg-demo', 'nest-demo', 'vuepress-demo', 'vitepress-demo']
140-
141-
const exampleDemos: VipProject[] = []
142-
for (const pkgDirName of pkgNames) {
143-
const pkg = await getAppsPkgJSON(`${pkgDirName}`)
144-
exampleDemos.push({
145-
...pkg,
146-
private: true,
147-
id: '🤡',
148-
changelog: `../changelogs/${pkgDirName}/changelog.html`,
149-
readme: `../apps/${pkgDirName}/index.html`,
150-
sourceCode: `https://github.com/142vip/core-x/tree/main/apps/${pkgDirName}/`,
151-
})
152-
}
153-
return exampleDemos
154-
}
155-
15688
/**
15789
* 根据侧边栏获取变更日志侧边栏
15890
*/

.vitepress/theme/components/HomePage.vue

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@ import {
66
VipProjectTable,
77
VipTeam,
88
} from '@142vip/vitepress/components'
9-
import { onMounted, ref } from 'vue'
10-
import { getCoreProjectData, getExampleDemoTableData } from '../../sidebar'
9+
import { getCoreProjectData, getExampleDemoTableData } from './project-data'
1110
12-
const coreProjectTableData = ref<any[]>([])
13-
const exampleDemoTableData = ref()
14-
15-
/**
16-
* 异步加载表格数据
17-
*/
18-
onMounted(async () => {
19-
coreProjectTableData.value = await getCoreProjectData()
20-
exampleDemoTableData.value = await getExampleDemoTableData()
21-
})
11+
const exampleDemoTableData = getExampleDemoTableData()
12+
const coreProjectTableData = getCoreProjectData()
2213
</script>
2314

2415
<!-- 首页 -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { VipProject } from '@142vip/vitepress'
2+
import { resolveWorkspacePackageMaps } from '@142vip/vitepress/workspace'
3+
import { ProjectId, sidebarConfig } from '../../sidebar'
4+
5+
const { packagesByDir, appsByDir } = resolveWorkspacePackageMaps(
6+
import.meta.glob('../../../packages/*/package.json', { eager: true }),
7+
import.meta.glob('../../../apps/*/package.json', { eager: true }),
8+
)
9+
10+
const DEMO_APP_DIRS = ['egg-demo', 'nest-demo', 'vuepress-demo', 'vitepress-demo'] as const
11+
12+
export function getCoreProjectData(): VipProject[] {
13+
const rows: VipProject[] = []
14+
for (const { items = [], text = '' } of sidebarConfig) {
15+
if (text.includes(ProjectId.DEMO))
16+
continue
17+
for (const { text: pkgName = '' } of items) {
18+
const dir = pkgName.split('@142vip/')[1]
19+
const base = dir ? packagesByDir[dir] : undefined
20+
if (!base)
21+
continue
22+
rows.push({
23+
...base,
24+
id: text.split(' ')[0],
25+
changelog: `../changelogs/${dir}/changelog.html`,
26+
readme: `../packages/${dir}/index.html`,
27+
sourceCode: `https://github.com/142vip/core-x/tree/main/packages/${dir}/`,
28+
})
29+
}
30+
}
31+
return rows
32+
}
33+
34+
export function getExampleDemoTableData(): VipProject[] {
35+
const rows: VipProject[] = []
36+
for (const dir of DEMO_APP_DIRS) {
37+
const pkg = appsByDir[dir]
38+
if (!pkg)
39+
continue
40+
rows.push({
41+
...pkg,
42+
private: true,
43+
id: '🤡',
44+
changelog: `../changelogs/${dir}/changelog.html`,
45+
readme: `../apps/${dir}/index.html`,
46+
sourceCode: `https://github.com/142vip/core-x/tree/main/apps/${dir}/`,
47+
})
48+
}
49+
return rows
50+
}

0 commit comments

Comments
 (0)