Blame view

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

        'alert-success-border-color': '#274916',
        'alert-success-bg-color': '#162312',
        'alert-success-icon-color': '#49aa19',
        'alert-info-border-color': '#153450',
        'alert-info-bg-color': '#111b26',
        'alert-info-icon-color': '#177ddc',
        'alert-warning-border-color': '#594214',
        'alert-warning-bg-color': '#2b2111',
        'alert-warning-icon-color': '#d89614',
        'alert-error-border-color': '#58181c',
        'alert-error-bg-color': '#2a1215',
        'alert-error-icon-color': '#a61d24',
Vben authored
84
85
86
87
      },
    }),
  ];
vben authored
88
  return plugin as unknown as PluginOption[];
vben authored
89
}