Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(createIconPack): improve local icon resolution #149

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/young-chicken-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-icon": patch
---

perf(createIconPack): improve local icon resolution
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 5 additions & 11 deletions packages/core/lib/createIconPack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { statSync, promises as fs } from "node:fs";
import * as fs from "node:fs/promises";
import { fileURLToPath, pathToFileURL } from "node:url";
import resolvePackage from "resolve-pkg";

Expand All @@ -19,13 +19,14 @@ export function createIconPack({
const path = fileURLToPath(
new URL(dir ? `${dir}/${name}.svg` : `${name}.svg`, baseUrl)
);
if (!exists(path)) {
try {
const svg = await fs.readFile(path, "utf8");
return svg;
} catch {
throw new Error(
`[astro-icon] Unable to load "${path}"! Does the file exist?"`
);
}
const svg = await fs.readFile(path).then((res) => res.toString());
return svg;
};
}

Expand All @@ -48,10 +49,3 @@ export function createIconPack({
};
}
}

const exists = (path: string): boolean => {
try {
return statSync(path).isFile();
} catch (e) {}
return false;
};