-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuno.config.ts
126 lines (112 loc) · 2.84 KB
/
uno.config.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { promises as fs } from "fs";
import {
defineConfig,
presetUno,
// presetAttributify,
presetTypography,
} from "unocss";
import type { IconifyJSON } from "@iconify/types";
import presetIcons from "@unocss/preset-icons";
import { compareColors, stringToColor } from "@iconify/utils/lib/colors";
import {
importDirectory,
parseColors,
runSVGO,
deOptimisePaths,
} from "@iconify/tools";
import transformerDirectives from "@unocss/transformer-directives";
/**
* Load custom icon set
*/
async function loadCustomIconSet(): Promise<IconifyJSON> {
// Load icons
const iconSet = await importDirectory("assets/svg", {
prefix: "svg",
});
// Clean up each icon
await iconSet.forEach(async (name) => {
const svg = iconSet.toSVG(name)!;
// Change color to `currentColor`
const blackColor = stringToColor("black")!;
await parseColors(svg, {
defaultColor: "currentColor",
callback: (attr, colorStr, color) => {
// console.log('Color:', colorStr, color);
// Change black to 'currentColor'
if (color && compareColors(color, blackColor)) {
return "currentColor";
}
switch (color?.type) {
case "none":
case "current":
return color;
}
throw new Error(`Unexpected color "${colorStr}" in attribute ${attr}`);
},
});
// Optimise
runSVGO(svg);
// Update paths for compatibility with old software
await deOptimisePaths(svg);
// Update icon in icon set
iconSet.fromSVG(name, svg);
});
// Export as IconifyJSON
return iconSet.export();
}
/**
* Create UnoCSS config
*/
export function createConfig({ strict = true, dev = true } = {}): any {
return defineConfig({
envMode: dev ? "dev" : "build",
theme: {
fontFamily: {
sans: "'Inter', sans-serif",
mono: "'Fira Code', monospace",
},
},
presets: [
presetIcons({
autoInstall: false,
collections: {
// Loading IconifyJSON data
test: async () => {
const content = await fs.readFile("assets/test.json", "utf8");
return JSON.parse(content);
},
// Loading icon set
// Moved to a separate function to make it easier to understand and reuse it
"custom-svg": loadCustomIconSet,
},
}),
presetUno(),
// presetAttributify(),
presetTypography(),
],
rules: [
[
"inline-icon",
{
"vertical-align": "-0.125em",
},
],
[
"icon16",
{
"font-size": "16px",
"line-height": "1em",
},
],
[
"icon24",
{
"font-size": "24px",
"line-height": "1em",
},
],
],
transformers: [transformerDirectives()],
});
}
export default createConfig();