index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. export interface AntDesignXResolverOptions {
  2. /**
  3. * exclude components that do not require automatic import
  4. *
  5. * @default []
  6. */
  7. exclude?: string[]
  8. /**
  9. * rename package
  10. *
  11. * @default 'ant-design-x-vue'
  12. */
  13. packageName?: string
  14. /**
  15. * customizable prefix for resolving components
  16. *
  17. * @default 'AX'
  18. */
  19. prefix?: string
  20. }
  21. /**
  22. * set of components that are contained in the package
  23. */
  24. const primitiveNames = new Set<string>([
  25. 'Attachments',
  26. 'Bubble',
  27. 'Conversations',
  28. 'Prompts',
  29. 'Sender',
  30. 'Suggestion',
  31. 'Theme',
  32. 'ThoughtChain',
  33. 'Welcome',
  34. ])
  35. function isAntdXVueComponent(name: string) {
  36. return primitiveNames.has(name)
  37. }
  38. // currently unnecessary to add side effects
  39. // function getSideEffects(
  40. // componentName: string,
  41. // options: AntDesignXResolverOptions = {}
  42. // ) {
  43. // const { importStyle = true, packageName = 'ant-design-x-vue' } = options
  44. // if (!importStyle) return
  45. // return
  46. // }
  47. export function AntDesignXVueResolver(
  48. options: AntDesignXResolverOptions = {}
  49. ) {
  50. const {
  51. prefix = 'AX',
  52. packageName = 'ant-design-x-vue',
  53. exclude = []
  54. } = options
  55. const resolverInfo = {
  56. type: 'component',
  57. resolve: (name: string) => {
  58. if (!name.startsWith(prefix)) return
  59. const componentName = name.slice(prefix.length)
  60. if (
  61. !isAntdXVueComponent(componentName) || exclude.includes(componentName)
  62. ) return
  63. return {
  64. name: componentName,
  65. from: packageName,
  66. as: `${prefix}${componentName}`
  67. }
  68. }
  69. }
  70. return resolverInfo.resolve
  71. }