普兆健康管家后端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB

4 days ago
  1. import { Plugin } from 'vite'
  2. import fs from 'node:fs'
  3. import path from 'node:path'
  4. export function generateComponentTypes(): Plugin {
  5. return {
  6. name: 'generate-component-types',
  7. async buildStart() {
  8. const componentsDir = path.resolve(process.cwd(), 'src/components')
  9. const dtsPath = path.resolve(process.cwd(), 'src/components/components.d.ts')
  10. // Get immediate subdirectories only
  11. const directories = fs
  12. .readdirSync(componentsDir, { withFileTypes: true })
  13. .filter((dirent) => dirent.isDirectory())
  14. .map((dirent) => dirent.name)
  15. // Generate type definitions only for valid components
  16. const typeDefinitions = directories
  17. .filter((dir) => {
  18. // Check if ComponentName/ComponentName.vue exists
  19. const componentFile = path.join(componentsDir, dir, `${dir}.vue`)
  20. return fs.existsSync(componentFile)
  21. })
  22. .map((dir) => {
  23. return `${dir}: typeof import('./${dir}/${dir}.vue')['default']`
  24. })
  25. // Create the type definition file content
  26. const content = `declare module 'vue' {
  27. export interface GlobalComponents {
  28. ${typeDefinitions.join('\n')}
  29. }
  30. }
  31. export {}`
  32. // Write the type definition file
  33. fs.writeFileSync(dtsPath, content, 'utf-8')
  34. },
  35. }
  36. }