import chokidar from 'chokidar'; import fs from 'fs' import fse from 'fs-extra'; import fg from 'fast-glob'; const tailwindcss = require('@tailwindcss/postcss'); const autoprefixer = require('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) } // ==================================================================== // | // ==================================================================== function build() { log('-----------------------'); const options = { logger: { warn(message, options) { if (options.span) { log('====================================================================================================================') log(`Warning when compiling: ${options.span.url.pathname}:`) log(` ${message}\n`) } }, debug(message, options) { if (options.span) { log('====================================================================================================================') log(`Debug message compiling: ${options.span.url.pathname}:`) log(` ${message}\n`) } } } } const tailwindoptions = { content: ["/app/views/**/*.jade"], theme: { extend: {}, }, plugins: [], }; try { // Input CSS with Tailwind directives const inputCSS = ` @tailwind base; @tailwind components; @tailwind utilities; `; // 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); //fs.writeFileSync("dist/styles.css", result.css); console.log("Tailwind CSS compiled to dist/styles.css"); } catch (err) { console.error("Error building CSS:", err); } try { const result = sass.compile(path, options); var publicPath = path publicPath = publicPath.replace('/app/scss','/www/css') publicPath = publicPath.replace('.scss','.css') fse.outputFileSync(publicPath, result.css); } catch (err) { log(`[ERROR] SASS Compile: ${path}`) log(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() { if (process.argv[2] == 'compile-and-watch') { await build(); startWatcher(); } else if(process.argv[2] == 'compile-only') { await build(); } else { startWatcher(); } } go()