Skip to content

Commit 21d80e5

Browse files
committed
refactor: make iconify tree-shakeable
1 parent 106f049 commit 21d80e5

3 files changed

Lines changed: 61 additions & 3 deletions

File tree

scripts/iconify-plugin.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import data from "@iconify-json/simple-icons/icons.json" with { type: "json" };
2+
3+
const ID = "virtual:iconify-simple-icons";
4+
5+
/**
6+
* Vite plugin to make iconify simple icons data is tree-shakeable
7+
* @param {Array<string>} icons Icons you want to include.
8+
*/
9+
export function iconify(icons) {
10+
return /** @type {import("vite").Plugin} */ ({
11+
name: "iconify",
12+
resolveId(id) {
13+
if (id === ID) {
14+
return ID;
15+
}
16+
},
17+
load(id) {
18+
if (id === ID) {
19+
let code = "export default {\n";
20+
21+
for (const icon of icons) {
22+
if (!(icon in data.icons)) {
23+
this.info(`Icon \`${icon}\` is not available.`);
24+
continue;
25+
}
26+
code += `"${icon}": ${JSON.stringify(data.icons[icon].body)},\n`;
27+
}
28+
29+
code += "}\n";
30+
return code;
31+
}
32+
},
33+
});
34+
}

src/components/Icon.svelte

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
<script>
2-
import { icons } from "@iconify-json/simple-icons/icons.json";
2+
import data from "virtual:iconify-simple-icons";
33
import { height } from "@iconify-json/simple-icons/info.json";
44
55
let { icon, class: classes, ...rest } = $props();
6+
7+
const icons = new Proxy(data, {
8+
get(o, p) {
9+
if (p in o) {
10+
return o[p];
11+
} else {
12+
throw new Error(`Icon \`${p}\` is not available.`);
13+
}
14+
},
15+
});
616
</script>
717

818
<svg
@@ -12,4 +22,4 @@
1222
fill="currentColor"
1323
viewBox="0 0 {height} {height}"
1424
class={classes}
15-
{...rest}>{@html icons[icon]?.body ?? ""}</svg>
25+
{...rest}>{@html icons[icon] ?? ""}</svg>

vite.config.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import { sveltekit } from "@sveltejs/kit/vite";
22
import { defineConfig } from "vite";
33
import tailwindcss from "@tailwindcss/vite";
4+
import { iconify } from "./scripts/iconify-plugin.js";
5+
import dataForHome from "./src/routes/data-for-home.json" with { type: "json" };
46

57
export default defineConfig({
6-
plugins: [sveltekit(), tailwindcss()],
8+
plugins: [
9+
sveltekit(),
10+
tailwindcss(),
11+
/* prettier-ignore */
12+
iconify([
13+
...dataForHome.languages.map((data) => data.icon),
14+
...dataForHome.tools.map((data) => data.icon),
15+
...dataForHome.frameworkLibrary.map((data) => data.icon),
16+
"bookstack",
17+
"github",
18+
"googlemaps"
19+
]),
20+
],
721
json: {
822
namedExports: true,
923
},

0 commit comments

Comments
 (0)