UNPKG

flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

62 lines (59 loc) 2.33 kB
import fs__default from 'fs/promises'; import chokidar from 'chokidar'; import { isEqual } from '../../helpers/is-equal.js'; import { automaticClassGenerationMessage, excludeDirs, configFilePath, classListFilePath } from '../consts.js'; import { buildClassList } from '../utils/build-class-list.js'; import { extractComponentImports } from '../utils/extract-component-imports.js'; import { getClassList } from '../utils/get-class-list.js'; import { getConfig } from '../utils/get-config.js'; async function dev() { const config = await getConfig(); if (config.components.length) { console.warn(automaticClassGenerationMessage); } const importedComponentsMap = {}; let classList = await getClassList(); async function handleChange(path, eventName) { if (eventName === "change") { const content = await fs__default.readFile(path, "utf-8"); const componentImports = extractComponentImports(content); if (componentImports.length) { importedComponentsMap[path] = componentImports; } else { delete importedComponentsMap[path]; } } if (eventName === "unlink") { delete importedComponentsMap[path]; } const newImportedComponents = [...new Set(Object.values(importedComponentsMap).flat())]; const config2 = await getConfig(); const newClassList = buildClassList({ components: config2.components.length ? config2.components : newImportedComponents, dark: config2.dark, prefix: config2.prefix }); if (!isEqual(classList, newClassList)) { classList = newClassList; await fs__default.writeFile(classListFilePath, JSON.stringify(classList, null, 2), { flag: "w" }); } } const watcher = chokidar.watch(".", { ignored: (path, stats) => { if (stats?.isDirectory()) { return excludeDirs.some((dir) => path.endsWith(dir)); } if (stats?.isFile()) { return ![".astro", ".js", ".jsx", ".md", ".mdx", ".ts", ".tsx", configFilePath].some( (type) => path.endsWith(type) ); } return false; } }); watcher.on("add", (path) => handleChange(path, "change")); watcher.on("change", (path) => handleChange(path, "change")); watcher.on("unlink", (path) => handleChange(path, "unlink")); } export { dev }; //# sourceMappingURL=dev.js.map