theme.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Vite plugin for website theme color switching
  3. * https://github.com/anncwb/vite-plugin-theme
  4. */
  5. import type { PluginOption } from 'vite';
  6. import path from 'path';
  7. import { viteThemePlugin, antdDarkThemePlugin, mixLighten, mixDarken, tinycolor } from '@rys-fe/vite-plugin-theme';
  8. import { getThemeColors, generateColors } from '../../config/themeConfig';
  9. import { generateModifyVars } from '../../generate/generateModifyVars';
  10. export function configThemePlugin(isBuild: boolean): PluginOption[] {
  11. const colors = generateColors({
  12. mixDarken,
  13. mixLighten,
  14. tinycolor,
  15. });
  16. // update-begin-修复编译后主题色切换不生效黑屏的问题-----------------------
  17. // https://github.com/vbenjs/vue-vben-admin/issues/1445
  18. // 抽取出viteThemePlugin插件,下方会根据不同环境设置enforce
  19. const vite_theme_plugin = viteThemePlugin({
  20. resolveSelector: (s) => {
  21. s = s.trim();
  22. switch (s) {
  23. case '.ant-steps-item-process .ant-steps-item-icon > .ant-steps-icon':
  24. return '.ant-steps-item-icon > .ant-steps-icon';
  25. case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)':
  26. case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled):hover':
  27. case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled):active':
  28. return s;
  29. case '.ant-steps-item-icon > .ant-steps-icon':
  30. return s;
  31. case '.ant-select-item-option-selected:not(.ant-select-item-option-disabled)':
  32. return s;
  33. default:
  34. if (s.indexOf('.ant-btn') >= -1) {
  35. // 按钮被重新定制过,需要过滤掉class防止覆盖
  36. return s;
  37. }
  38. }
  39. return s.startsWith('[data-theme') ? s : `[data-theme] ${s}`;
  40. },
  41. colorVariables: [...getThemeColors(), ...colors],
  42. });
  43. vite_theme_plugin.forEach(function (item) {
  44. //对vite:theme插件特殊配置
  45. if ('vite:theme' === item.name) {
  46. // 打包时去除enforce: "post",vite 2.6.x适配,否则生成app-theme-style为空,因为async transform(code, id) {的code没有正确获取
  47. if (isBuild) {
  48. delete item.enforce;
  49. }
  50. }
  51. });
  52. // update-end-修复编译后主题色切换不生效黑屏的问题-----------------------
  53. const plugin = [
  54. vite_theme_plugin,
  55. antdDarkThemePlugin({
  56. preloadFiles: [
  57. // path.resolve(process.cwd(), 'node_modules/ant-design-vue/dist/reset.css'),
  58. //path.resolve(process.cwd(), 'node_modules/ant-design-vue/dist/antd.dark.less'),
  59. path.resolve(process.cwd(), 'src/design/index.less'),
  60. ],
  61. filter: (id) => (isBuild ? !id.endsWith('antd.less') : true),
  62. // extractCss: false,
  63. darkModifyVars: {
  64. ...generateModifyVars(true),
  65. 'text-color': '#c9d1d9',
  66. 'primary-1': 'rgb(255 255 255 / 8%)',
  67. 'text-color-base': '#c9d1d9',
  68. 'component-background': '#151515',
  69. 'heading-color': 'rgb(255 255 255 / 65%)',
  70. // black: '#0e1117',
  71. // #8b949e
  72. 'text-color-secondary': '#8b949e',
  73. 'border-color-base': '#303030',
  74. 'header-light-bottom-border-color': '#303030',
  75. // 'border-color-split': '#30363d',
  76. 'item-active-bg': '#111b26',
  77. 'app-content-background': '#1e1e1e',
  78. 'tree-node-selected-bg': '#11263c',
  79. 'alert-success-border-color': '#274916',
  80. 'alert-success-bg-color': '#162312',
  81. 'alert-success-icon-color': '#49aa19',
  82. 'alert-info-border-color': '#153450',
  83. 'alert-info-bg-color': '#111b26',
  84. 'alert-info-icon-color': '#177ddc',
  85. 'alert-warning-border-color': '#594214',
  86. 'alert-warning-bg-color': '#2b2111',
  87. 'alert-warning-icon-color': '#d89614',
  88. 'alert-error-border-color': '#58181c',
  89. 'alert-error-bg-color': '#2a1215',
  90. 'alert-error-icon-color': '#a61d24',
  91. },
  92. }),
  93. ];
  94. return plugin as unknown as PluginOption[];
  95. }