UNPKG

flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

107 lines (104 loc) 4.18 kB
import fs__default from 'fs/promises'; import { basename } from 'path'; import chokidar from 'chokidar'; import { isEqual } from '../../helpers/is-equal.js'; import { automaticClassGenerationMessage, classListFilePath, excludeDirs, allowedExtensions, configFilePath, gitIgnoreFilePath, initFilePath, initJsxFilePath } from '../consts.js'; import { buildClassList } from '../utils/build-class-list.js'; import { createInitLogger } from '../utils/create-init-logger.js'; import { extractComponentImports } from '../utils/extract-component-imports.js'; import { findFiles } from '../utils/find-files.js'; import { getClassList } from '../utils/get-class-list.js'; import { getConfig } from '../utils/get-config.js'; import { setupGitIgnore } from './setup-gitignore.js'; import { setupInit } from './setup-init.js'; import { setupOutputDirectory } from './setup-output-directory.js'; async function dev() { await setupOutputDirectory(); let config = await getConfig(); await setupInit(config); const initLogger = createInitLogger(config); if (config.components.length) { console.warn(automaticClassGenerationMessage); } const importedComponentsMap = {}; let classList = await getClassList(); const files = await findFiles({ patterns: allowedExtensions.map((ext) => `**/*${ext}`), excludeDirs }); for (const file of files) { const content = await fs__default.readFile(file, "utf-8"); const componentImports = extractComponentImports(content); initLogger.check(file, content); if (componentImports.length) { importedComponentsMap[file] = componentImports; } } initLogger.log(); const newImportedComponents = [...new Set(Object.values(importedComponentsMap).flat())]; const newClassList = buildClassList({ components: config.components.length ? config.components : newImportedComponents, dark: config.dark, prefix: config.prefix, version: config.version }); if (!isEqual(classList, newClassList)) { classList = newClassList; console.log(`Generating ${classListFilePath} file...`); await fs__default.writeFile(classListFilePath, JSON.stringify(classList, null, 2)); } async function handleChange(path, eventName) { if ([configFilePath, initFilePath, initJsxFilePath].includes(path)) { config = await getConfig(); await setupInit(config); initLogger.config = config; } if (path === gitIgnoreFilePath) { await setupGitIgnore(); } if (eventName === "change") { const content = await fs__default.readFile(path, "utf-8"); const componentImports = extractComponentImports(content); initLogger.check(path, content); if (componentImports.length) { importedComponentsMap[path] = componentImports; } else { delete importedComponentsMap[path]; } } if (eventName === "unlink") { delete importedComponentsMap[path]; initLogger.checkedMap.delete(path); } initLogger.log(); const newImportedComponents2 = [...new Set(Object.values(importedComponentsMap).flat())]; const newClassList2 = buildClassList({ components: config.components.length ? config.components : newImportedComponents2, dark: config.dark, prefix: config.prefix, version: config.version }); if (!isEqual(classList, newClassList2)) { classList = newClassList2; console.log(`Generating ${classListFilePath} file...`); await fs__default.writeFile(classListFilePath, JSON.stringify(classList, null, 2)); } } const watcher = chokidar.watch(".", { ignored: (path, stats) => { if (stats?.isDirectory()) { return excludeDirs.includes(basename(path)); } if (stats?.isFile()) { return !allowedExtensions.concat(configFilePath, gitIgnoreFilePath, initFilePath, initJsxFilePath).some((ext) => path.endsWith(ext)); } return false; }, ignoreInitial: true }); 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