index.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright (c) 2013-Now https://jeesite.com All rights reserved.
  3. * No deletion without permission, or be held responsible to law.
  4. * @author ThinkGem
  5. */
  6. import { type PluginOption } from 'vite';
  7. import path from 'path';
  8. import { viteThemePlugin, antdDarkThemePlugin } from 'vite-plugin-theme-vite3';
  9. import { mixLighten, mixDarken, tinycolor } from 'vite-plugin-theme-vite3/es/colorUtils';
  10. import { getThemeColors, generateColors } from './themeConfig';
  11. import { generateModifyVars } from './modifyVars';
  12. /**
  13. * Vite plugin for website theme color switching
  14. * https://gitee.com/thinkgem/vite-plugin-theme-vite3
  15. */
  16. export function configThemePlugin(isBuild: boolean): PluginOption[] {
  17. const colors = generateColors({
  18. mixDarken,
  19. mixLighten,
  20. tinycolor,
  21. });
  22. // console.log(
  23. // [...getThemeColors(), ...colors]
  24. // .map((e) => `<a style="background:${e}">${e}</a>`)
  25. // .join('&nbsp;'),
  26. // );
  27. // 抽取出viteThemePlugin插件,下方会根据不同环境设置enforce
  28. const vite_theme_plugin = viteThemePlugin({
  29. injectClientPath: JSON.stringify(path.resolve(__dirname, '../node_modules/vite-plugin-theme-vite3/es/client')),
  30. resolveSelector: (s) => {
  31. s = s.trim();
  32. if (s.includes('const __vite__css = "')) {
  33. return s.replace(/(.*)const __vite__css = "/g, '');
  34. }
  35. return s;
  36. // switch (s) {
  37. // case '.ant-steps-item-process .ant-steps-item-icon > .ant-steps-icon':
  38. // return '.ant-steps-item-icon > .ant-steps-icon';
  39. // case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)':
  40. // case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled):hover':
  41. // case '.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled):active':
  42. // case '.ant-steps-item-icon > .ant-steps-icon':
  43. // case '.ant-select-item-option-selected:not(.ant-select-item-option-disabled)':
  44. // case '.ant-menu-item-selected':
  45. // case '.ant-drawer .jeesite-basic-drawer .ant-drawer-body':
  46. // case '.jeesite-basic-drawer-footer':
  47. // return s;
  48. // default:
  49. // // 按钮被重新定制过,需要过滤掉class防止覆盖
  50. // if (s.indexOf('.ant-btn') >= 0) {
  51. // return s;
  52. // }
  53. // }
  54. // return s.startsWith('[data-theme') ? s : `[data-theme] ${s}`;
  55. },
  56. colorVariables: [...getThemeColors(), ...colors],
  57. });
  58. //console.log('vite_theme_plugin:'+JSON.stringify(vite_theme_plugin));
  59. ((vite_theme_plugin || []) as any[]).forEach(function (item) {
  60. //对vite:theme插件特殊配置
  61. if ('vite:theme' === item.name) {
  62. //console.log(item);
  63. // 打包时去除enforce: "post",vite 2.6.x适配,否则生成app-theme-style为空,
  64. // 因为 async transform(code, id) { 的code没有正确获取
  65. if (isBuild) {
  66. delete item.enforce;
  67. }
  68. //console.log(item);
  69. }
  70. });
  71. //console.log('vite_theme_plugin后:'+JSON.stringify(vite_theme_plugin));
  72. const plugin = [
  73. vite_theme_plugin,
  74. antdDarkThemePlugin({
  75. // preloadFiles: [
  76. // // path.resolve(process.cwd(), 'node_modules/ant-design-vue/dist/antd.less'),
  77. // // path.resolve(process.cwd(), 'node_modules/ant-design-vue/dist/antd.dark.less'),
  78. // path.resolve(process.cwd(), 'src/design/index.less'),
  79. // ],
  80. // filter: (id) => (isBuild ? !id.endsWith('antd.less') : true),
  81. // extractCss: false,
  82. darkModifyVars: {
  83. ...generateModifyVars(true),
  84. // 'text-color': '#c9d1d9',
  85. // 'primary-1': 'rgb(255 255 255 / 8%)',
  86. // 'text-color-base': '#c9d1d9',
  87. // 'component-background': '#151515',
  88. // 'heading-color': 'rgb(255 255 255 / 65%)',
  89. // // black: '#0e1117',
  90. // // #8b949e
  91. // 'text-color-secondary': '#8b949e',
  92. // 'border-color-base': '#303030',
  93. // // 'border-color-split': '#30363d',
  94. // 'item-active-bg': '#111b26',
  95. // // 'tree-node-selected-bg': '#11263c',
  96. // 'alert-success-border-color': '#274916',
  97. // 'alert-success-bg-color': '#162312',
  98. // 'alert-success-icon-color': '#49aa19',
  99. // 'alert-info-border-color': '#153450',
  100. // 'alert-info-bg-color': '#111b26',
  101. // 'alert-info-icon-color': '#177ddc',
  102. // 'alert-warning-border-color': '#594214',
  103. // 'alert-warning-bg-color': '#2b2111',
  104. // 'alert-warning-icon-color': '#d89614',
  105. // 'alert-error-border-color': '#58181c',
  106. // 'alert-error-bg-color': '#2a1215',
  107. // 'alert-error-icon-color': '#a61d24',
  108. 'content-bg': '#141414',
  109. 'content-bg-striped': '#1e1e1e',
  110. 'button-cancel-color': '#c9d1d9',
  111. 'button-cancel-bg-color': '#2d2d2d',
  112. 'button-cancel-border-color': '#303030',
  113. 'button-cancel-hover-color': '#c9d1d9',
  114. 'button-cancel-hover-bg-color': '#4a4a4a',
  115. 'button-cancel-hover-border-color': '#4a4a4a',
  116. 'header-light-bottom-border-color': '#303030',
  117. },
  118. } as any),
  119. ];
  120. return plugin as unknown as PluginOption[];
  121. }