generated from yunsii/starter-vite-react-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-icon-sets.ts
81 lines (69 loc) · 2.15 KB
/
local-icon-sets.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
import { importDirectorySync } from '@iconify/tools'
import pathe from 'pathe'
import type { IconifyJSON } from '@iconify/types'
import { loadIconifyJsonPath, optimizeIconSet } from '../helpers/icon-set'
import type { PreserveColorsFn } from '../helpers/icon-set'
export interface GetLocalIconSetsOptions {
/**
* if value type is `string`, it will import all icons from the directory
*
* ref: https://iconify.design/docs/libraries/tools/import/directory.html
*/
define: Record<
string,
| string
| {
/**
* Import all icons from directory
*
* ref: https://iconify.design/docs/libraries/tools/import/directory.html
*/
path: string
options?: Parameters<typeof importDirectorySync>[1]
/** Custom colors should preserved, Do not transform to `currentColor` */
preserveColors?: PreserveColorsFn
}
| {
/**
* Importing Iconify icon set by `IconifyJSON`
*/
iconifyJsonPath: string
}
>
}
/** ref: https://iconify.design/docs/libraries/tools/import/directory.html */
export function getLocalIconSets(options: GetLocalIconSetsOptions) {
const { define: iconSetMaps } = options
return Object.keys(iconSetMaps).reduce((prev, current) => {
const iconSetConfig = iconSetMaps[current]
if (
typeof iconSetConfig !== 'string'
&& 'iconifyJsonPath' in iconSetConfig
) {
const iconSet = loadIconifyJsonPath(iconSetConfig.iconifyJsonPath)
if (iconSet) {
return {
...prev,
[current]: iconSet.export(),
}
}
return prev
}
const _path
= typeof iconSetConfig === 'string' ? iconSetConfig : iconSetConfig.path
const options
= typeof iconSetConfig === 'string' ? undefined : iconSetConfig.options
const preserveColors
= typeof iconSetConfig === 'string'
? undefined
: iconSetConfig.preserveColors
const customIconSet = importDirectorySync(pathe.normalize(_path), options)
optimizeIconSet(customIconSet, {
preserveColors,
})
return {
...prev,
[current]: customIconSet.export(),
}
}, {} as Record<string, IconifyJSON>)
}