2023-11-13 22:04:41 +00:00
|
|
|
import type { Plugin } from "rollup";
|
2023-12-24 17:39:43 +00:00
|
|
|
// eslint-disable-next-line import/no-named-as-default
|
2023-11-06 02:22:33 +00:00
|
|
|
import glob from "fast-glob";
|
|
|
|
import { readFile } from "fs/promises";
|
|
|
|
import { createRequire } from "module";
|
2023-12-24 17:39:43 +00:00
|
|
|
import { dirname } from "path";
|
|
|
|
import { fileURLToPath } from "url";
|
2023-04-07 16:39:59 +00:00
|
|
|
|
|
|
|
// https://stackoverflow.com/a/62499498/242365
|
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
|
|
|
|
export default function iconPlugin(): Plugin {
|
|
|
|
return {
|
2023-11-13 22:04:41 +00:00
|
|
|
name: 'virtual:icons',
|
2023-04-07 16:39:59 +00:00
|
|
|
resolveId: (id) => {
|
2023-11-13 22:04:41 +00:00
|
|
|
if (id === 'virtual:icons') {
|
2023-04-07 16:39:59 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
load: async (id) => {
|
2023-11-13 22:04:41 +00:00
|
|
|
if (id === 'virtual:icons') {
|
|
|
|
const icons: Record<string, Record<string, string>> = {};
|
2023-12-24 17:39:43 +00:00
|
|
|
for (const path of await glob(`${dirname(fileURLToPath(import.meta.url))}/assets/icons/*/*.svg`)) {
|
2023-04-07 16:39:59 +00:00
|
|
|
const [set, fname] = path.split("/").slice(-2);
|
|
|
|
|
|
|
|
if (!icons[set])
|
|
|
|
icons[set] = {};
|
|
|
|
|
|
|
|
icons[set][fname.replace(/\.svg$/, "")] = (await readFile(path)).toString();
|
|
|
|
}
|
|
|
|
icons["fontawesome"] = {};
|
|
|
|
for (const name of ["arrow-left", "arrow-right", "person-biking", "car", "chart-line", "copy", "circle-info", "slash", "person-walking"]) {
|
|
|
|
icons["fontawesome"][name] = (await readFile(require.resolve(`@fortawesome/fontawesome-free/svgs/solid/${name}.svg`))).toString();
|
|
|
|
}
|
|
|
|
return `export default ${JSON.stringify(icons, undefined, '\t')}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|