flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
61 lines (58 loc) • 2.32 kB
JavaScript
import fs__default from 'fs/promises';
import { basename } from 'path';
import chokidar from 'chokidar';
import { isEqual } from '../../helpers/is-equal.js';
import { automaticClassGenerationMessage, excludeDirs, allowedExtensions, 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.includes(basename(path));
}
if (stats?.isFile()) {
return !allowedExtensions.concat(configFilePath).some((ext) => path.endsWith(ext));
}
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