vite-tres-types-plugin.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. )
  31. .join(',\n')}
  32. } from 'three';
  33. type OverwrittenProps = 'position' | 'rotation' | 'scale' | 'color'
  34. type TresModifiedObject = {
  35. /**
  36. * A Vector3 | Array | scalar representing the object's local position. Default is (0, 0, 0).
  37. *
  38. * @type {TresVectorProp}
  39. */
  40. position: TresVectorProp
  41. /**
  42. * A Vector3 | Array | scalar representing the object's local rotation. Default is (0, 0, 0).
  43. *
  44. * @type {TresVectorProp}
  45. */
  46. rotation: TresVectorProp
  47. /**
  48. * A Vector3 | Array | scalar representing the object's local scale. Default is (0, 0, 0).
  49. *
  50. * @type {TresVectorProp}
  51. */
  52. scale: TresVectorProp
  53. /**
  54. * Color of the material, by default set to white (0xffffff).
  55. *
  56. * @type {TresColor}
  57. */
  58. color: TresColor,
  59. /**
  60. * Arguments of the THREE instance, by default set to empty array.
  61. *
  62. * @type {any[]}
  63. *
  64. * @example
  65. *
  66. * \`\`\`html
  67. * <TresBoxGeometry args="[1, 3, 4]" /> // BoxGeometry(1, 3, 4)
  68. * \`\`\`
  69. */
  70. args?: any[],
  71. }
  72. declare module 'vue' {
  73. export interface GlobalComponents {
  74. TresMesh: DefineComponent<Partial<Omit<Mesh, OverwrittenProps> & TresModifiedObject>>
  75. ${Object.keys(THREE)
  76. .filter(key => key.endsWith('Geometry'))
  77. .map(
  78. key =>
  79. `Tres${key}: DefineComponent<Partial<Omit<${key}['parameters'], OverwrittenProps> & TresModifiedObject>>`,
  80. )
  81. .join('\n')}
  82. ${Object.keys(THREE)
  83. .filter(
  84. key =>
  85. key.endsWith('Geometry') ||
  86. key.endsWith('Material') ||
  87. key.endsWith('Helper') ||
  88. key.endsWith('Light') ||
  89. key.endsWith('Camera'),
  90. )
  91. .map(key => `Tres${key}: DefineComponent<Partial<Omit<${key}, OverwrittenProps > & TresModifiedObject>>`)
  92. .join('\n')}
  93. }
  94. }
  95. `
  96. fs.writeFileSync(join(outputDir, 'tres-components.d.ts'), typeDefs)
  97. },
  98. }
  99. })
  100. export const ViteTresPlugin = unplugin.vite