styleImport.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * 【样式按需加载插件 ——主要处理antd的样式】
  3. * Introduces component library styles on demand.
  4. * https://github.com/anncwb/vite-plugin-style-import
  5. */
  6. import { createStyleImportPlugin } from 'vite-plugin-style-import';
  7. export function configStyleImportPlugin(_isBuild: boolean) {
  8. if (!_isBuild) {
  9. return [];
  10. }
  11. const styleImportPlugin = createStyleImportPlugin({
  12. libs: [
  13. {
  14. libraryName: 'ant-design-vue',
  15. esModule: true,
  16. resolveStyle: (name) => {
  17. // 这里是无需额外引入样式文件的“子组件”列表
  18. const ignoreList = [
  19. 'anchor-link',
  20. 'sub-menu',
  21. 'menu-item',
  22. 'menu-divider',
  23. 'menu-item-group',
  24. 'breadcrumb-item',
  25. 'breadcrumb-separator',
  26. 'form-item',
  27. 'step',
  28. 'select-option',
  29. 'select-opt-group',
  30. 'card-grid',
  31. 'card-meta',
  32. 'collapse-panel',
  33. 'descriptions-item',
  34. 'list-item',
  35. 'list-item-meta',
  36. 'table-column',
  37. 'table-column-group',
  38. 'tab-pane',
  39. 'tab-content',
  40. 'timeline-item',
  41. 'tree-node',
  42. 'skeleton-input',
  43. 'skeleton-avatar',
  44. 'skeleton-title',
  45. 'skeleton-paragraph',
  46. 'skeleton-image',
  47. 'skeleton-button',
  48. ];
  49. // 这里是需要额外引入样式的子组件列表
  50. // 单独引入子组件时需引入组件样式,否则会在打包后导致子组件样式丢失
  51. const replaceList = {
  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. }