theme.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Vite plugin for website theme color switching
  3. * https://github.com/anncwb/vite-plugin-theme
  4. */
  5. import type { Plugin } from 'vite';
  6. import path from 'path';
  7. import {
  8. viteThemePlugin,
  9. antdDarkThemePlugin,
  10. mixLighten,
  11. mixDarken,
  12. tinycolor,
  13. } from 'vite-plugin-theme';
  14. import { getThemeColors, generateColors } from '../../config/themeConfig';
  15. import { generateModifyVars } from '../../generate/generateModifyVars';
  16. export function configThemePlugin(isBuild: boolean): Plugin[] {
  17. const colors = generateColors({
  18. mixDarken,
  19. mixLighten,
  20. tinycolor,
  21. });
  22. const plugin = [
  23. viteThemePlugin({
  24. resolveSelector: (s) => {
  25. s = s.trim();
  26. switch (s) {
  27. case '.ant-steps-item-process .ant-steps-item-icon > .ant-steps-icon':
  28. return '.ant-steps-item-icon > .ant-steps-icon';
  29. case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)':
  30. case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled):hover':
  31. case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled):active':
  32. return s;
  33. case '.ant-steps-item-icon > .ant-steps-icon':
  34. return s;
  35. case '.ant-select-item-option-selected:not(.ant-select-item-option-disabled)':
  36. return s;
  37. default:
  38. if (s.indexOf('.ant-btn') >= -1) {
  39. // 按钮被重新定制过,需要过滤掉class防止覆盖
  40. return s;
  41. }
  42. }
  43. return s.startsWith('[data-theme') ? s : `[data-theme] ${s}`;
  44. },
  45. colorVariables: [...getThemeColors(), ...colors],
  46. }),
  47. antdDarkThemePlugin({
  48. preloadFiles: [
  49. path.resolve(process.cwd(), 'node_modules/ant-design-vue/dist/antd.less'),
  50. //path.resolve(process.cwd(), 'node_modules/ant-design-vue/dist/antd.dark.less'),
  51. path.resolve(process.cwd(), 'src/design/index.less'),
  52. ],
  53. filter: (id) => (isBuild ? !id.endsWith('antd.less') : true),
  54. // extractCss: false,
  55. darkModifyVars: {
  56. ...generateModifyVars(true),
  57. 'text-color': '#c9d1d9',
  58. 'primary-1': 'rgb(255 255 255 / 8%)',
  59. 'text-color-base': '#c9d1d9',
  60. 'component-background': '#151515',
  61. 'heading-color': 'rgb(255 255 255 / 65%)',
  62. // black: '#0e1117',
  63. // #8b949e
  64. 'text-color-secondary': '#8b949e',
  65. 'border-color-base': '#303030',
  66. // 'border-color-split': '#30363d',
  67. 'item-active-bg': '#111b26',
  68. 'app-content-background': 'rgb(255 255 255 / 4%)',
  69. 'tree-node-selected-bg': '#11263c',
  70. 'alert-success-border-color': '#274916',
  71. 'alert-success-bg-color': '#162312',
  72. 'alert-success-icon-color': '#49aa19',
  73. 'alert-info-border-color': '#153450',
  74. 'alert-info-bg-color': '#111b26',
  75. 'alert-info-icon-color': '#177ddc',
  76. 'alert-warning-border-color': '#594214',
  77. 'alert-warning-bg-color': '#2b2111',
  78. 'alert-warning-icon-color': '#d89614',
  79. 'alert-error-border-color': '#58181c',
  80. 'alert-error-bg-color': '#2a1215',
  81. 'alert-error-icon-color': '#a61d24',
  82. },
  83. }),
  84. ];
  85. return plugin as unknown as Plugin[];
  86. }