index.ts 5.4 KB

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