|
1 | 1 | import { getMaterialSymbolsUrl } from "@react-md/core/icon/getMaterialSymbolsUrl"; |
2 | 2 | import { type MaterialSymbolName } from "@react-md/core/icon/material"; |
3 | 3 | import { DEFAULT_MATERIAL_SYMBOL_NAMES } from "@react-md/core/icon/symbols"; |
| 4 | +import { minimatch } from "minimatch"; |
4 | 5 | import { glob, readFile } from "node:fs/promises"; |
5 | 6 | import { join } from "node:path"; |
6 | 7 | import { type HtmlTagDescriptor, type Plugin } from "vite"; |
@@ -45,6 +46,33 @@ export function materialSymbolsPlugin( |
45 | 46 | } |
46 | 47 | }, |
47 | 48 |
|
| 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 | + |
48 | 76 | transformIndexHtml() { |
49 | 77 | if (symbolNames.size === 0) { |
50 | 78 | return []; |
|
0 commit comments