UNPKG

flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

64 lines (60 loc) 2.36 kB
'use strict'; var fs = require('fs/promises'); var chokidar = require('chokidar'); var isEqual = require('../../helpers/is-equal.cjs'); var consts = require('../consts.cjs'); var buildClassList = require('../utils/build-class-list.cjs'); var extractComponentImports = require('../utils/extract-component-imports.cjs'); var getClassList = require('../utils/get-class-list.cjs'); var getConfig = require('../utils/get-config.cjs'); async function dev() { const config = await getConfig.getConfig(); if (config.components.length) { console.warn(consts.automaticClassGenerationMessage); } const importedComponentsMap = {}; let classList = await getClassList.getClassList(); async function handleChange(path, eventName) { if (eventName === "change") { const content = await fs.readFile(path, "utf-8"); const componentImports = extractComponentImports.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.getConfig(); const newClassList = buildClassList.buildClassList({ components: config2.components.length ? config2.components : newImportedComponents, dark: config2.dark, prefix: config2.prefix }); if (!isEqual.isEqual(classList, newClassList)) { classList = newClassList; await fs.writeFile(consts.classListFilePath, JSON.stringify(classList, null, 2), { flag: "w" }); } } const watcher = chokidar.watch(".", { ignored: (path, stats) => { if (stats?.isDirectory()) { return consts.excludeDirs.some((dir) => path.endsWith(dir)); } if (stats?.isFile()) { return ![".astro", ".js", ".jsx", ".md", ".mdx", ".ts", ".tsx", consts.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")); } exports.dev = dev; //# sourceMappingURL=dev.cjs.map