theme.ts 4.1 KB

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