-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-css.cjs
46 lines (39 loc) · 1.68 KB
/
build-css.cjs
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
// build-css.js
const fs = require('fs');
const path = require('path');
const postcss = require('postcss');
const cssnano = require('cssnano');
const autoprefixer = require('autoprefixer');
async function buildCss() {
const cssDir = path.resolve('src/css');
const distDir = path.resolve('dist/css');
// Create dist directory if it doesn't exist
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir, { recursive: true });
}
const cssFiles = fs.readdirSync(cssDir).filter(file => file.endsWith('.css'));
for (const file of cssFiles) {
const filePath = path.join(cssDir, file);
const css = fs.readFileSync(filePath, 'utf8');
// Process original CSS
const originalOutput = await postcss([autoprefixer]).process(css, {
from: filePath,
to: path.join(distDir, file),
map: { inline: false }, // Generate source maps
});
fs.writeFileSync(path.join(distDir, file), originalOutput.css);
fs.writeFileSync(path.join(distDir, `${file}.map`), originalOutput.map.toString());
console.log(`Processed and moved original ${file} to ${distDir}`);
// Process minified CSS
const minifiedFile = file.replace('.css', '.min.css');
const minifiedOutput = await postcss([autoprefixer, cssnano]).process(css, {
from: filePath,
to: path.join(distDir, minifiedFile),
map: { inline: false }, // Generate source maps
});
fs.writeFileSync(path.join(distDir, minifiedFile), minifiedOutput.css);
fs.writeFileSync(path.join(distDir, `${minifiedFile}.map`), minifiedOutput.map.toString());
console.log(`Processed and moved minified ${minifiedFile} to ${distDir}`);
}
}
buildCss().catch(err => console.error('Error processing CSS:', err));