styleImport.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Introduces component library styles on demand.
  3. * https://github.com/xingyuv/vite-vue-plugin-style-import
  4. */
  5. import { createStyleImportPlugin } from 'vite-plugin-style-import';
  6. export function configStyleImportPlugin(_isBuild: boolean) {
  7. if (!_isBuild) {
  8. return [];
  9. }
  10. const styleImportPlugin = createStyleImportPlugin({
  11. libs: [
  12. {
  13. libraryName: 'ant-design-vue',
  14. esModule: true,
  15. resolveStyle: (name) => {
  16. // 这里是无需额外引入样式文件的“子组件”列表
  17. const ignoreList = [
  18. 'anchor-link',
  19. 'sub-menu',
  20. 'menu-item',
  21. 'menu-divider',
  22. 'menu-item-group',
  23. 'breadcrumb-item',
  24. 'breadcrumb-separator',
  25. 'form-item',
  26. 'step',
  27. 'select-option',
  28. 'select-opt-group',
  29. 'card-grid',
  30. 'card-meta',
  31. 'collapse-panel',
  32. 'descriptions-item',
  33. 'list-item',
  34. 'list-item-meta',
  35. 'table-column',
  36. 'table-column-group',
  37. 'tab-pane',
  38. 'tab-content',
  39. 'timeline-item',
  40. 'tree-node',
  41. 'skeleton-input',
  42. 'skeleton-avatar',
  43. 'skeleton-title',
  44. 'skeleton-paragraph',
  45. 'skeleton-image',
  46. 'skeleton-button',
  47. ];
  48. // 这里是需要额外引入样式的子组件列表
  49. // 单独引入子组件时需引入组件样式,否则会在打包后导致子组件样式丢失
  50. const replaceList = {
  51. textarea: 'input',
  52. 'typography-text': 'typography',
  53. 'typography-title': 'typography',
  54. 'typography-paragraph': 'typography',
  55. 'typography-link': 'typography',
  56. 'dropdown-button': 'dropdown',
  57. 'input-password': 'input',
  58. 'input-search': 'input',
  59. 'input-group': 'input',
  60. 'radio-group': 'radio',
  61. 'checkbox-group': 'checkbox',
  62. 'layout-sider': 'layout',
  63. 'layout-content': 'layout',
  64. 'layout-footer': 'layout',
  65. 'layout-header': 'layout',
  66. 'month-picker': 'date-picker',
  67. 'range-picker': 'date-picker',
  68. 'image-preview-group': 'image',
  69. };
  70. return ignoreList.includes(name)
  71. ? ''
  72. : replaceList.hasOwnProperty(name)
  73. ? `ant-design-vue/es/${replaceList[name]}/style/index`
  74. : `ant-design-vue/es/${name}/style/index`;
  75. },
  76. },
  77. ],
  78. });
  79. return styleImportPlugin;
  80. }