offline.icon.ts
1.7 KB
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
import { IconifyJSON } from '@iconify/iconify';
import { camelCase } from 'lodash-es';
interface IconType {
prefix: string;
icon: string;
alias?: string;
}
export function getCacheIcons() {
const storageKeys = Object.keys(localStorage);
const iconifyKeys = storageKeys.filter((key) => key.startsWith('iconify'));
const allIcons: IconType[] = [];
for (const key of iconifyKeys) {
try {
const res: Record<'data', IconifyJSON> = JSON.parse(localStorage.getItem(key)!);
const {
data: { prefix, icons, aliases },
} = res;
allIcons.push(...Object.keys(icons).map((icon) => ({ icon, prefix })));
allIcons.push(
...Object.entries(aliases || {}).map(([key, value]) => ({
icon: key,
alias: value.parent,
prefix,
}))
);
} catch {
continue;
}
}
const collect = allIcons.reduce((prev, next) => {
if (Reflect.has(prev, next.prefix)) {
prev[next.prefix].push(next);
} else {
prev[next.prefix] = [next];
}
return prev;
}, {} as Record<string, IconType[]>);
const icons = Object.entries(collect).reduce(
(prev, [key, value]) => {
for (const { icon, alias } of value) {
const importName = camelCase(`${key}-${alias || icon}`);
prev.imports.push(
`import ${importName} from 'virtual:@iconify/json/${key}/${alias || icon}';`
);
prev.registerIcons.push(`addIcon('${key}:${icon}', ${importName});`);
}
return prev;
},
{ imports: [], registerIcons: [] } as Record<'imports' | 'registerIcons', string[]>
);
return `${[...new Set(icons.imports)].join('\n')}\n${[...new Set(icons.registerIcons)].join(
'\n'
)}`;
}