import chokidar from 'chokidar'; import fs from 'fs' import fse from 'fs-extra'; import fg from 'fast-glob'; import postcss from 'postcss'; import tailwindcss from '@tailwindcss/postcss'; import autoprefixer from 'autoprefixer'; // ==================================================================== // | // ==================================================================== function log(message) { const options = { encoding: "utf-8", flag: "a+", }; const datetime = new Date().toISOString() message = `[${datetime}] ${message}\n` fse.outputFileSync('/logs/builder/tailwind.log', message, options); console.log(message) } // ==================================================================== // | // ==================================================================== async function build() { log('-----------------------'); const tailwindoptions = { content: [], theme: { extend: {}, }, plugins: [], }; try { // Input CSS with Tailwind directives const inputCSS = fs.readFileSync('/app/css/tailwind/input.css', 'utf8'); // Process with PostCSS const result = await postcss([ tailwindcss(tailwindoptions), autoprefixer ]).process(inputCSS, { from: undefined }); // Write output CSS fse.outputFileSync("/www/css/tailwind.css", result.css); console.log("Tailwind CSS compiled to /www/css/tailwind.css"); } catch (err) { console.error("Error building CSS:", err); } } // ==================================================================== // | // ==================================================================== function startWatcher(watchFolders) { // https://github.com/paulmillr/chokidar const options = { awaitFinish: true, ignoreInitial: true, usePolling: true, interval: 100 } const watcher = chokidar.watch(watchFolders, options).on('all', (event, path) => { log(`${path} was ${event}`); }); watcher .on('add', path => build()) .on('change', path => build()) .on('unlink', path => build()); watcher .on('ready', () => log('********** Watch is running **********')); } // ==================================================================== // | MAIN // ==================================================================== async function go() { const watchFolders = [ '/www/**/*.{html,js}' ] if (process.argv[2] == 'compile-and-watch') { await build(); startWatcher(watchFolders); } else if(process.argv[2] == 'compile-only') { await build(); } else { startWatcher(watchFolders); } } go()