-
Notifications
You must be signed in to change notification settings - Fork 20
/
plugin-theme-less.ts
87 lines (83 loc) · 2.67 KB
/
plugin-theme-less.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import fs from 'fs';
import type { IApi } from 'dumi';
import { isNil, omitBy } from 'lodash';
// umi 插件只能 import 支持 CommonJS 语法库和文件,因此需要使用 lib 产物
import formatToken from 'antd/lib/theme/util/alias';
// @ts-ignore
import theme from './.dumi/tmp/plugin-theme-less/index.js';
// @ts-ignore
import defaultTheme from './.dumi/tmp/plugin-theme-less/default.js';
// @ts-ignore
import darkTheme from './.dumi/tmp/plugin-theme-less/dark.js';
// @ts-ignore
import aliyunTheme from './.dumi/tmp/plugin-theme-less/aliyun.js';
export default (api: IApi) => {
// 生成 default.less、dark.less 和 compact.less 主题文件
api.onGenerateFiles(() => {
const { defaultAlgorithm, darkAlgorithm, compactAlgorithm, defaultSeed } = theme;
const themeList = [
{
theme: 'default',
algorithm: defaultAlgorithm,
token: defaultTheme.token,
},
{
theme: 'dark',
algorithm: darkAlgorithm,
token: darkTheme.token,
},
{
theme: 'compact',
algorithm: compactAlgorithm,
// 使用 defaultTheme token
token: defaultTheme.token,
},
{
theme: 'aliyun',
algorithm: defaultAlgorithm,
token: aliyunTheme.token,
},
];
themeList.forEach(item => {
let mapToken =
item.theme === 'dark'
? {
// 对于暗色主题,优先级: 算法生成的 Token > 自定义 token
...item.token,
...item.algorithm(defaultSeed),
}
: {
// 对于非暗色主题,优先级: 自定义 token > 算法生成的 Token
...item.algorithm(defaultSeed),
...item.token,
};
mapToken = {
...mapToken,
// 以下四种预设颜色和语义色保持一致
blue: mapToken.colorInfo,
green: mapToken.colorSuccess,
yellow: mapToken.colorWarning,
red: mapToken.colorError,
// override token
override:
item.theme === 'dark'
? {}
: // 对于非暗色主题,需要覆盖部分 Alias Token 的值
omitBy(
{
boxShadow: item.token.boxShadow,
boxShadowSecondary: item.token.boxShadowSecondary,
},
isNil
),
};
const aliasToken = formatToken(mapToken);
let lessString = '';
Object.keys(aliasToken).forEach(key => {
const value = aliasToken[key];
lessString += `@${key}: ${value};\n`;
});
fs.writeFileSync(`packages/design/src/theme/style/${item.theme}.less`, lessString);
});
});
};