CLI for UnoCSS that enables CSS generation from utility classes in traditional backends and static environments.
—
Development-focused file watching system for automatic CSS regeneration when source files change, providing hot reload functionality for UnoCSS development workflows.
Creates or returns existing chokidar file system watcher for monitoring file changes during development.
/**
* Creates or returns existing chokidar file system watcher
* @param options - CLI options containing patterns and working directory
* @returns Promise resolving to FSWatcher instance for file monitoring
*/
function getWatcher(options?: CliOptions): Promise<FSWatcher>;Usage Examples:
// Note: getWatcher may not be directly exported from main package entry
import { getWatcher } from "@unocss/cli";
// Create watcher for HTML files
const watcher = await getWatcher({
patterns: ["src/**/*.html"],
cwd: process.cwd(),
});
// Listen for file changes
watcher.on('change', (path) => {
console.log(`File changed: ${path}`);
});
watcher.on('add', (path) => {
console.log(`File added: ${path}`);
});
watcher.on('unlink', (path) => {
console.log(`File removed: ${path}`);
});
// Close watcher when done
process.on('SIGINT', () => {
watcher.close();
});The watcher is configured with sensible defaults for web development:
node_modules and .git directoriesignoreInitial: true) to avoid triggering on startupWhen watch mode is enabled in the build function, the following workflow occurs:
import { build } from "@unocss/cli";
// Basic watch mode
await build({
patterns: ["src/**/*.{html,js,ts}"],
outFile: "dist/uno.css",
watch: true,
});
// Watch with custom configuration
await build({
patterns: ["src/**/*.html", "components/**/*.jsx"],
outFile: "public/styles.css",
watch: true,
config: "uno.config.js",
preflights: true,
});For advanced use cases, you can create and manage watchers manually:
import { getWatcher } from "@unocss/cli";
import { debounce } from "perfect-debounce";
// Create custom watcher setup
const watcher = await getWatcher({
patterns: ["src/**/*.{html,js,vue}"],
cwd: "/path/to/project",
});
// Custom debounced rebuild function
const debouncedRebuild = debounce(async () => {
console.log("Rebuilding CSS...");
// Your custom rebuild logic here
}, 100);
// Handle different file events
watcher.on('all', (event, path) => {
console.log(`${event}: ${path}`);
if (event === 'change' || event === 'add') {
debouncedRebuild();
}
});
// Graceful shutdown
process.on('SIGTERM', () => {
watcher.close();
});The watch system handles different types of file changes appropriately:
The watch system provides detailed console feedback:
UnoCSS v66.5.0
✨ UnoCSS in watch mode...
ℹ Watching for changes in src/**/*.html, src/**/*.js
change src/index.html
✅ 15 utilities generated to dist/uno.css
add src/components/button.html
✅ 23 utilities generated to dist/uno.css
uno.config.js changed, setting new config
✅ 23 utilities generated to dist/uno.cssInstall with Tessl CLI
npx tessl i tessl/npm-unocss--cli