Skip to content

Commit

Permalink
Fix icon reloading logic to only trigger for relevant file changes (#258
Browse files Browse the repository at this point in the history
)

* fix: icon reloading issue

* chore: add changeset
  • Loading branch information
maxchang3 authored Dec 26, 2024
1 parent 60ea42f commit 61d5da6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/twelve-students-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-icon": patch
---

Fix icon reloading logic to only trigger for relevant file changes
8 changes: 7 additions & 1 deletion packages/core/src/vite-plugin-astro-icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import loadLocalCollection from "./loaders/loadLocalCollection.js";
import loadIconifyCollections from "./loaders/loadIconifyCollections.js";
import { createHash } from "node:crypto";
import Path from 'node:path'

interface PluginContext extends Pick<AstroConfig, "root" | "output"> {
logger: AstroIntegrationLogger;
Expand Down Expand Up @@ -49,7 +50,12 @@ export function createPlugin(
},
configureServer({ watcher, moduleGraph }) {
watcher.add(`${iconDir}/**/*.svg`);
watcher.on("change", async () => {
watcher.on("all", async (_, filepath: string) => {
const parsedPath = Path.parse(filepath)
const resolvedIconDir = Path.resolve(root.pathname, iconDir)
const isSvgFileInIconDir = parsedPath.dir.startsWith(resolvedIconDir) && parsedPath.ext === ".svg";
const isAstroConfig = parsedPath.name === "astro.config";
if (!isSvgFileInIconDir && !isAstroConfig) return;
console.log(`Local icons changed, reloading`);
try {
if (!collections) {
Expand Down

0 comments on commit 61d5da6

Please sign in to comment.