-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
65 lines (55 loc) · 1.7 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const postcss = require('postcss');
const postcssImport = require('postcss-import');
const postcssNested = require('postcss-nested');
const tailwindcss = require('tailwindcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const fs = require('fs');
const chokidar = require('chokidar');
const inputFile = './assets/css/style.css';
const outputFile = './assets/css/style.min.css';
const templatesFolder = './templates'; // Update with your templates folder path
const plugins = [
postcssImport,
postcssNested,
tailwindcss,
autoprefixer,
cssnano
];
const processCSS = async () => {
try {
const css = fs.readFileSync(inputFile, 'utf8');
const result = await postcss(plugins).process(css, {
from: inputFile,
to: outputFile,
map: { inline: false },
});
fs.writeFileSync(outputFile, result.css);
console.log('CSS build completed successfully.');
} catch (error) {
console.error('Error building CSS:', error);
}
};
// Function to watch for CSS file changes
const watchCSS = () => {
console.log('Watching for CSS file changes...');
chokidar.watch(inputFile).on('change', () => {
console.log('CSS file changed. Rebuilding...');
processCSS();
});
};
// Function to watch for template file changes
const watchTemplates = () => {
console.log('Watching for template file changes...');
chokidar.watch(templatesFolder).on('change', () => {
console.log('Template file changed. Rebuilding CSS...');
processCSS();
});
};
// Start the initial CSS build
processCSS();
// Start watching for changes in watch mode
if (process.argv.includes('--watch')) {
watchCSS();
watchTemplates(); // Add this line to watch templates folder
}