-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcompress-css.js
34 lines (29 loc) · 1.06 KB
/
compress-css.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
const CleanCSS = require('clean-css');
const path = require('path');
const fs = require('fs');
// 指定需要压缩的 CSS 文件
const cssFiles = [
path.join(__dirname, 'Public', 'Theme', 'assets', 'css', 'create.css'),
path.join(__dirname, 'Public', 'Theme', 'assets', 'css', 'main.css'),
];
cssFiles.forEach(filePath => {
const minFilePath = filePath.replace('.css', '.min.css');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file: ${err.message}`);
return;
}
const output = new CleanCSS().minify(data);
if (output.errors.length > 0) {
console.error(`Error compressing file: ${output.errors.join(', ')}`);
return;
}
fs.writeFile(minFilePath, output.styles, err => {
if (err) {
console.error(`Error writing minified file: ${err.message}`);
return;
}
console.log(`Compressed ${path.basename(filePath)} to ${path.basename(minFilePath)}`);
});
});
});