vite-tres-types-plugin.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { createUnplugin } from 'unplugin'
  2. import * as THREE from 'three'
  3. import fs from 'fs'
  4. import { join } from 'pathe'
  5. export const unplugin = createUnplugin(() => {
  6. return {
  7. name: 'unplugin-tres',
  8. apply: 'build',
  9. configResolved(config) {
  10. // Check if the output directory exists, if not create it
  11. const outputDir = join(config.root, 'src/types')
  12. if (!fs.existsSync(outputDir)) {
  13. fs.mkdirSync(outputDir)
  14. }
  15. const typeDefs = `
  16. import type { DefineComponent } from 'vue'
  17. import type { Vector2, Vector3, Color } from 'three'
  18. export type TresVectorProp = Vector2 | Vector3 | number[] | number
  19. export type TresColor = string | number | Color | number[]
  20. import type {
  21. Mesh,
  22. ${Object.keys(THREE)
  23. .filter(
  24. key =>
  25. key.endsWith('Geometry') ||
  26. key.endsWith('Material') ||
  27. key.endsWith('Helper') ||
  28. key.endsWith('Light') ||
  29. key.endsWith('Camera') ||
  30. key.endsWith('Fog'),
  31. )
  32. .join(',\n')}
  33. } from 'three';
  34. type OverwrittenProps = 'position' | 'rotation' | 'scale' | 'color'
  35. type TresModifiedObject = {
  36. /**
  37. * A Vector3 | Array | scalar representing the object's local position. Default is (0, 0, 0).
  38. *
  39. * @type {TresVectorProp}
  40. */
  41. position: TresVectorProp
  42. /**
  43. * A Vector3 | Array | scalar representing the object's local rotation. Default is (0, 0, 0).
  44. *
  45. * @type {TresVectorProp}
  46. */
  47. rotation: TresVectorProp
  48. /**
  49. * A Vector3 | Array | scalar representing the object's local scale. Default is (0, 0, 0).
  50. *
  51. * @type {TresVectorProp}
  52. */
  53. scale: TresVectorProp
  54. /**
  55. * Color of the material, by default set to white (0xffffff).
  56. *
  57. * @type {TresColor}
  58. */
  59. color: TresColor,
  60. /**
  61. * Arguments of the THREE instance, by default set to empty array.
  62. *
  63. * @type {any[]}
  64. *
  65. * @example
  66. *
  67. * \`\`\`html
  68. * <TresBoxGeometry args="[1, 3, 4]" /> // BoxGeometry(1, 3, 4)
  69. * \`\`\`
  70. */
  71. args?: any[],
  72. }
  73. declare module 'vue' {
  74. export interface GlobalComponents {
  75. TresMesh: DefineComponent<Partial<Omit<Mesh, OverwrittenProps> & TresModifiedObject>>
  76. ${Object.keys(THREE)
  77. .filter(key => key.endsWith('Geometry'))
  78. .map(
  79. key =>
  80. `Tres${key}: DefineComponent<Partial<Omit<${key}['parameters'], OverwrittenProps> & TresModifiedObject>>`,
  81. )
  82. .join('\n')}
  83. ${Object.keys(THREE)
  84. .filter(
  85. key =>
  86. key.endsWith('Geometry') ||
  87. key.endsWith('Material') ||
  88. key.endsWith('Helper') ||
  89. key.endsWith('Light') ||
  90. key.endsWith('Camera') ||
  91. key.endsWith('Fog'),
  92. )
  93. .map(key => `Tres${key}: DefineComponent<Partial<Omit<${key}, OverwrittenProps > & TresModifiedObject>>`)
  94. .join('\n')}
  95. }
  96. }
  97. `
  98. fs.writeFileSync(join(outputDir, 'tres-components.d.ts'), typeDefs)
  99. },
  100. }
  101. })
  102. export const ViteTresPlugin = unplugin.vite