Skip to content

Commit 75a2657

Browse files
committed
feat(vite-material-symbols-plugin): track new icons
1 parent ff3f981 commit 75a2657

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

.changeset/quick-bees-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-md/vite-material-symbols-plugin": minor
3+
---
4+
5+
Automatically add any new icons while running the dev server by reloading the page. This used to require a full dev server restart to track new icons.

packages/vite-material-symbols-plugin/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
],
4646
"license": "MIT",
4747
"packageManager": "pnpm@10.28.0",
48+
"dependencies": {
49+
"minimatch": "^10.2.4"
50+
},
4851
"devDependencies": {
4952
"@react-md/core": "workspace:*",
5053
"@react-md/eslint-config": "workspace:*",

packages/vite-material-symbols-plugin/src/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getMaterialSymbolsUrl } from "@react-md/core/icon/getMaterialSymbolsUrl";
22
import { type MaterialSymbolName } from "@react-md/core/icon/material";
33
import { DEFAULT_MATERIAL_SYMBOL_NAMES } from "@react-md/core/icon/symbols";
4+
import { minimatch } from "minimatch";
45
import { glob, readFile } from "node:fs/promises";
56
import { join } from "node:path";
67
import { type HtmlTagDescriptor, type Plugin } from "vite";
@@ -45,6 +46,33 @@ export function materialSymbolsPlugin(
4546
}
4647
},
4748

49+
configureServer: (server) => {
50+
const { root } = server.config;
51+
const sliceFrom = root.length + 1;
52+
const handler = async (file: string): Promise<void> => {
53+
const relativeFile = file.slice(sliceFrom);
54+
if (!minimatch(relativeFile, pattern)) {
55+
return;
56+
}
57+
58+
const fileSymbolNames = new Set<MaterialSymbolName>();
59+
const contents = await readFile(file, "utf8");
60+
addMaterialSymbolNames(contents, fileSymbolNames);
61+
const additions = fileSymbolNames.difference(symbolNames);
62+
if (additions.size === 0) {
63+
return;
64+
}
65+
66+
for (const name of fileSymbolNames) {
67+
symbolNames.add(name);
68+
}
69+
server.ws.send({ type: "full-reload" });
70+
};
71+
72+
server.watcher.on("add", handler);
73+
server.watcher.on("change", handler);
74+
},
75+
4876
transformIndexHtml() {
4977
if (symbolNames.size === 0) {
5078
return [];

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)