Skip to content

Commit

Permalink
添加组件文档索引插件
Browse files Browse the repository at this point in the history
  • Loading branch information
Yin-Jinlong committed Apr 15, 2024
1 parent 8600d59 commit 7060875
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 29 deletions.
25 changes: 0 additions & 25 deletions docs/gen-components-indexes.ts

This file was deleted.

15 changes: 15 additions & 0 deletions docs/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BundledLanguage} from 'shiki'
import type {Component} from 'vue'

declare global {

Expand All @@ -17,6 +18,20 @@ declare global {
export default src
}

declare module 'indexes~' {
import type {Component} from 'vue'

interface Page {
name: string
path: string
component: () => Promise<{ default: Component }>
}

const components: Page[]
export default components

export {}
}
}

export {}
5 changes: 2 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
"author": "Yin-Jinlong",
"type": "module",
"scripts": {
"dev": "pnpm run gen:components-indexes && vite",
"build": "pnpm run gen:components-indexes && vite build",
"gen:components-indexes": "esno gen-components-indexes.ts",
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"license": "LGPL-3.0-or-later",
Expand Down
3 changes: 2 additions & 1 deletion docs/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ import {HButton, vLoading} from '@yin-jinlong/h-ui'
import {PageHeader, SideBar} from '@components'
import components from '../components-indexes'
// @ts-ignore
import components from 'indexes~'
const app = shallowRef<Component | null>()
const loading = shallowRef(false)
Expand Down
1 change: 1 addition & 0 deletions docs/src/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './indexes'
41 changes: 41 additions & 0 deletions docs/src/plugin/indexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from 'fs'

import {Plugin} from 'vite'

export declare interface IndexesOptions {
dir?: string
}

function genIndexes(dir: string): string {
let pages = ''
fs.readdirSync(dir, {
withFileTypes: true
}).forEach((dir) => {
if (dir.isDirectory()) {
let name = dir.name
pages += `{
name: '${name}',
path: '/${name}',
component: () => import('./components/${name}')
},`
}
})
return pages
}

export function indexesPlugin(options?: IndexesOptions): Plugin {
return {
enforce: 'pre',
name: 'gen-indexes',
resolveId(source) {
if (source.endsWith('~')) {
return source
}
},
load(id) {
if (id !== 'indexes~')
return
return `export default [${genIndexes(options?.dir ?? 'components')}]`
}
}
}
3 changes: 3 additions & 0 deletions docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {visualizer} from 'rollup-plugin-visualizer'
import vue from '@vitejs/plugin-vue'
import viteCompression from 'vite-plugin-compression'

import {indexesPlugin} from './src/plugin'

function getSuffix(name: string) {
const i = name.lastIndexOf('.')
return i >= 0 ? name.substring(i + 1) : name
Expand Down Expand Up @@ -61,6 +63,7 @@ export default defineConfig((env) => {
entries: ['@yin-jinlong/h-ui']
},
plugins: [
indexesPlugin(),
vue({
isProduction: prod
}),
Expand Down

0 comments on commit 7060875

Please sign in to comment.